read_png.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <png.h>
  4. #include <grass/gis.h>
  5. #include "pngdriver.h"
  6. static void read_data(png_structp png_ptr, png_bytep data, png_size_t length)
  7. {
  8. png_size_t check;
  9. FILE *fp;
  10. if (png_ptr == NULL )
  11. return;
  12. fp = (FILE *) png_get_io_ptr(png_ptr);
  13. if ( fp == NULL )
  14. return;
  15. /* fread() returns 0 on error, so it is OK to store this in a png_size_t
  16. * instead of an int, which is what fread() actually returns.
  17. */
  18. check = fread(data, 1, length, fp);
  19. if (check != length)
  20. G_fatal_error("PNG: Read Error");
  21. }
  22. void read_png(void)
  23. {
  24. static jmp_buf jbuf;
  25. static png_struct *png_ptr;
  26. static png_info *info_ptr;
  27. FILE *input;
  28. int x, y;
  29. unsigned int *p;
  30. png_bytep line;
  31. png_uint_32 i_width, i_height;
  32. int depth, color_type;
  33. png_ptr =
  34. png_create_read_struct(PNG_LIBPNG_VER_STRING, &jbuf, NULL, NULL);
  35. if (!png_ptr)
  36. G_fatal_error("PNG: couldn't allocate PNG structure");
  37. info_ptr = png_create_info_struct(png_ptr);
  38. if (!info_ptr)
  39. G_fatal_error("PNG: couldn't allocate PNG structure");
  40. if (setjmp(png_jmpbuf(png_ptr)))
  41. G_fatal_error("error reading PNG file");
  42. input = fopen(png.file_name, "rb");
  43. if (!input)
  44. G_fatal_error("PNG: couldn't open output file %s", png.file_name);
  45. png_set_read_fn(png_ptr, input, read_data);
  46. png_read_info(png_ptr, info_ptr);
  47. png_get_IHDR(png_ptr, info_ptr, &i_width, &i_height,
  48. &depth, &color_type, NULL, NULL, NULL);
  49. if (depth != 8)
  50. G_fatal_error("PNG: input file is not 8-bit");
  51. if (i_width != png.width || i_height != png.height)
  52. G_fatal_error
  53. ("PNG: input file has incorrect dimensions: expected: %dx%d got: %lux%lu",
  54. png.width, png.height, (unsigned long) i_width, (unsigned long) i_height);
  55. if (png.true_color) {
  56. if (color_type != PNG_COLOR_TYPE_RGB_ALPHA)
  57. G_fatal_error("PNG: input file is not RGBA");
  58. }
  59. else {
  60. if (color_type != PNG_COLOR_TYPE_PALETTE)
  61. G_fatal_error("PNG: input file is not indexed color");
  62. }
  63. if (!png.true_color && png.has_alpha) {
  64. png_bytep trans;
  65. int num_trans;
  66. png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans, NULL);
  67. if (num_trans != 1 || trans[0] != 0)
  68. G_fatal_error("PNG: input file has invalid palette");
  69. }
  70. if (png.true_color)
  71. png_set_invert_alpha(png_ptr);
  72. else {
  73. png_colorp png_pal;
  74. int num_palette;
  75. int i;
  76. png_get_PLTE(png_ptr, info_ptr, &png_pal, &num_palette);
  77. if (num_palette > 256)
  78. num_palette = 256;
  79. for (i = 0; i < num_palette; i++) {
  80. png.palette[i][0] = png_pal[i].red;
  81. png.palette[i][1] = png_pal[i].green;
  82. png.palette[i][2] = png_pal[i].blue;
  83. }
  84. }
  85. line = G_malloc(png.width * 4);
  86. for (y = 0, p = png.grid; y < png.height; y++) {
  87. png_bytep q = line;
  88. png_read_row(png_ptr, q, NULL);
  89. if (png.true_color)
  90. for (x = 0; x < png.width; x++, p++) {
  91. int r = *q++;
  92. int g = *q++;
  93. int b = *q++;
  94. int a = *q++;
  95. unsigned int c = png_get_color(r, g, b, a);
  96. *p = c;
  97. }
  98. else
  99. for (x = 0; x < png.width; x++, p++, q++)
  100. *p = (png_byte) * q;
  101. }
  102. G_free(line);
  103. png_read_end(png_ptr, NULL);
  104. png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
  105. fclose(input);
  106. }