read_ppm.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*!
  2. \file lib/pngdriver/read_ppm.c
  3. \brief GRASS png display driver - read image (lower level functions)
  4. (C) 2007-2014 by Glynn Clements and the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author Glynn Clements
  8. */
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <grass/gis.h>
  13. #include "pngdriver.h"
  14. void read_ppm(void)
  15. {
  16. FILE *input;
  17. int x, y;
  18. int i_width, i_height, maxval;
  19. unsigned int rgb_mask = png_get_color(255, 255, 255, 0);
  20. unsigned int *p;
  21. if (!png.true_color)
  22. G_fatal_error("PNG: cannot use PPM/PGM with indexed color");
  23. input = fopen(png.file_name, "rb");
  24. if (!input)
  25. G_fatal_error("PNG: couldn't open input file %s", png.file_name);
  26. if (fscanf(input, "P6 %d %d %d", &i_width, &i_height, &maxval) != 3)
  27. G_fatal_error("PNG: invalid input file %s", png.file_name);
  28. fgetc(input);
  29. if (i_width != png.width || i_height != png.height)
  30. G_fatal_error
  31. ("PNG: input file has incorrect dimensions: expected: %dx%d got: %dx%d",
  32. png.width, png.height, i_width, i_height);
  33. for (y = 0, p = png.grid; y < png.height; y++) {
  34. for (x = 0; x < png.width; x++, p++) {
  35. unsigned int c = *p;
  36. int r = fgetc(input);
  37. int g = fgetc(input);
  38. int b = fgetc(input);
  39. r = r * 255 / maxval;
  40. g = g * 255 / maxval;
  41. b = b * 255 / maxval;
  42. c &= ~rgb_mask;
  43. c |= png_get_color(r, g, b, 0);
  44. *p = c;
  45. }
  46. }
  47. fclose(input);
  48. }
  49. void read_pgm(void)
  50. {
  51. char *mask_name = G_store(png.file_name);
  52. FILE *input;
  53. int x, y;
  54. int i_width, i_height, maxval;
  55. unsigned int rgb_mask = png_get_color(255, 255, 255, 0);
  56. unsigned int *p;
  57. if (!png.true_color)
  58. G_fatal_error("PNG: cannot use PPM/PGM with indexed color");
  59. mask_name[strlen(mask_name) - 2] = 'g';
  60. input = fopen(mask_name, "rb");
  61. if (!input)
  62. G_fatal_error("PNG: couldn't open input mask file %s", mask_name);
  63. if (fscanf(input, "P5 %d %d %d", &i_width, &i_height, &maxval) != 3)
  64. G_fatal_error("PNG: invalid input mask file %s", mask_name);
  65. fgetc(input);
  66. if (i_width != png.width || i_height != png.height)
  67. G_fatal_error
  68. ("PNG: input mask file has incorrect dimensions: expected: %dx%d got: %dx%d",
  69. png.width, png.height, i_width, i_height);
  70. G_free(mask_name);
  71. for (y = 0, p = png.grid; y < png.height; y++) {
  72. for (x = 0; x < png.width; x++, p++) {
  73. unsigned int c = *p;
  74. int k = fgetc(input);
  75. k = k * 255 / maxval;
  76. c &= rgb_mask;
  77. c |= png_get_color(0, 0, 0, 255 - k);
  78. *p = c;
  79. }
  80. }
  81. fclose(input);
  82. }