GRASS Programmer's Manual  6.4.2(2012)
cairodriver/read_ppm.c
Go to the documentation of this file.
00001 #include "cairodriver.h"
00002 
00003 void read_ppm(void)
00004 {
00005     char *mask_name = G_store(file_name);
00006     FILE *input, *mask;
00007     int x, y;
00008     int i_width, i_height, maxval;
00009 
00010     input = fopen(file_name, "rb");
00011     if (!input)
00012         G_fatal_error("cairo: couldn't open input file %s", file_name);
00013 
00014     if (fscanf(input, "P6 %d %d %d", &i_width, &i_height, &maxval) != 3)
00015         G_fatal_error("cairo: invalid input file %s", file_name);
00016 
00017     fgetc(input);
00018 
00019     if (i_width != width || i_height != height)
00020         G_fatal_error
00021             ("cairo: input file has incorrect dimensions: expected: %dx%d got: %dx%d",
00022              width, height, i_width, i_height);
00023 
00024     mask_name[strlen(mask_name) - 2] = 'g';
00025 
00026     mask = fopen(mask_name, "rb");
00027     if (!mask)
00028         G_fatal_error("cairo: couldn't open input mask file %s", mask_name);
00029 
00030     if (fscanf(mask, "P5 %d %d %d", &i_width, &i_height, &maxval) != 3)
00031         G_fatal_error("cairo: invalid input mask file %s", mask_name);
00032 
00033     fgetc(mask);
00034 
00035     if (i_width != width || i_height != height)
00036         G_fatal_error
00037             ("cairo: input mask file has incorrect dimensions: expected: %dx%d got: %dx%d",
00038              width, height, i_width, i_height);
00039 
00040     G_free(mask_name);
00041 
00042     for (y = 0; y < height; y++) {
00043         unsigned int *row = (unsigned int *)(grid + y * stride);
00044 
00045         for (x = 0; x < width; x++) {
00046             int r = fgetc(input);
00047             int g = fgetc(input);
00048             int b = fgetc(input);
00049             int a = fgetc(mask);
00050 
00051             r = r * 255 / maxval;
00052             g = g * 255 / maxval;
00053             b = b * 255 / maxval;
00054             a = a * 255 / maxval;
00055 
00056             if (a > 0 && a < 0xFF) {
00057                 r = r * a / 0xFF;
00058                 g = g * a / 0xFF;
00059                 b = b * a / 0xFF;
00060             }
00061 
00062             row[x] = (a << 24) | (r << 16) | (g << 8) | (b << 0);
00063         }
00064     }
00065 
00066     fclose(input);
00067     fclose(mask);
00068 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines