read_png.c 2.7 KB

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