GRASS Programmer's Manual  6.4.2(2012)
pngdriver/read_ppm.c
Go to the documentation of this file.
00001 
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include <string.h>
00005 
00006 #include <grass/gis.h>
00007 #include "pngdriver.h"
00008 
00009 void read_ppm(void)
00010 {
00011     FILE *input;
00012     int x, y;
00013     int i_width, i_height, maxval;
00014     unsigned int rgb_mask = get_color(255, 255, 255, 0);
00015     unsigned int *p;
00016 
00017     if (!true_color)
00018         G_fatal_error("PNG: cannot use PPM/PGM with indexed color");
00019 
00020     input = fopen(file_name, "rb");
00021     if (!input)
00022         G_fatal_error("PNG: couldn't open input file %s", file_name);
00023 
00024     if (fscanf(input, "P6 %d %d %d", &i_width, &i_height, &maxval) != 3)
00025         G_fatal_error("PNG: invalid input file %s", file_name);
00026 
00027     fgetc(input);
00028 
00029     if (i_width != width || i_height != height)
00030         G_fatal_error
00031             ("PNG: input file has incorrect dimensions: expected: %dx%d got: %dx%d",
00032              width, height, i_width, i_height);
00033 
00034     for (y = 0, p = grid; y < height; y++) {
00035         for (x = 0; x < width; x++, p++) {
00036             unsigned int c = *p;
00037 
00038             int r = fgetc(input);
00039             int g = fgetc(input);
00040             int b = fgetc(input);
00041 
00042             r = r * 255 / maxval;
00043             g = g * 255 / maxval;
00044             b = b * 255 / maxval;
00045 
00046             c &= ~rgb_mask;
00047             c |= get_color(r, g, b, 0);
00048 
00049             *p = c;
00050         }
00051     }
00052 
00053     fclose(input);
00054 }
00055 
00056 void read_pgm(void)
00057 {
00058     char *mask_name = G_store(file_name);
00059     FILE *input;
00060     int x, y;
00061     int i_width, i_height, maxval;
00062     unsigned int rgb_mask = get_color(255, 255, 255, 0);
00063     unsigned int *p;
00064 
00065     if (!true_color)
00066         G_fatal_error("PNG: cannot use PPM/PGM with indexed color");
00067 
00068     mask_name[strlen(mask_name) - 2] = 'g';
00069 
00070     input = fopen(mask_name, "rb");
00071     if (!input)
00072         G_fatal_error("PNG: couldn't open input mask file %s", mask_name);
00073 
00074     if (fscanf(input, "P5 %d %d %d", &i_width, &i_height, &maxval) != 3)
00075         G_fatal_error("PNG: invalid input mask file %s", mask_name);
00076 
00077     fgetc(input);
00078 
00079     if (i_width != width || i_height != height)
00080         G_fatal_error
00081             ("PNG: input mask file has incorrect dimensions: expected: %dx%d got: %dx%d",
00082              width, height, i_width, i_height);
00083 
00084     G_free(mask_name);
00085 
00086     for (y = 0, p = grid; y < height; y++) {
00087         for (x = 0; x < width; x++, p++) {
00088             unsigned int c = *p;
00089 
00090             int k = fgetc(input);
00091 
00092             k = k * 255 / maxval;
00093 
00094             c &= rgb_mask;
00095             c |= get_color(0, 0, 0, 255 - k);
00096 
00097             *p = c;
00098         }
00099     }
00100 
00101     fclose(input);
00102 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines