GRASS Programmer's Manual  6.4.2(2012)
cairodriver/write_ppm.c
Go to the documentation of this file.
00001 #include "cairodriver.h"
00002 
00003 void write_ppm(void)
00004 {
00005     char *mask_name = G_store(file_name);
00006     FILE *output, *mask;
00007     int x, y;
00008 
00009     output = fopen(file_name, "wb");
00010     if (!output)
00011         G_fatal_error("cairo: couldn't open output file %s", file_name);
00012 
00013     mask_name[strlen(mask_name) - 2] = 'g';
00014 
00015     mask = fopen(mask_name, "wb");
00016     if (!mask)
00017         G_fatal_error("cairo: couldn't open mask file %s", mask_name);
00018 
00019     G_free(mask_name);
00020 
00021     fprintf(output, "P6\n%d %d\n255\n", width, height);
00022     fprintf(mask, "P5\n%d %d\n255\n", width, height);
00023 
00024     for (y = 0; y < height; y++) {
00025         const unsigned int *row = (const unsigned int *)(grid + y * stride);
00026 
00027         for (x = 0; x < width; x++) {
00028             unsigned int c = row[x];
00029             int a = (c >> 24) & 0xFF;
00030             int r = (c >> 16) & 0xFF;
00031             int g = (c >> 8) & 0xFF;
00032             int b = (c >> 0) & 0xFF;
00033 
00034             if (a > 0 && a < 0xFF) {
00035                 r = r * 0xFF / a;
00036                 g = g * 0xFF / a;
00037                 b = b * 0xFF / a;
00038             }
00039 
00040             fputc((unsigned char)r, output);
00041             fputc((unsigned char)g, output);
00042             fputc((unsigned char)b, output);
00043             fputc((unsigned char)a, mask);
00044         }
00045     }
00046 
00047     fclose(output);
00048     fclose(mask);
00049 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines