jbig2_image_png.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* Copyright (C) 2001-2023 Artifex Software, Inc.
  2. All Rights Reserved.
  3. This software is provided AS-IS with no warranty, either express or
  4. implied.
  5. This software is distributed under license and may not be copied,
  6. modified or distributed except as expressly authorized under the terms
  7. of the license contained in the file LICENSE in this distribution.
  8. Refer to licensing information at http://www.artifex.com or contact
  9. Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  10. CA 94129, USA, for further information.
  11. */
  12. /*
  13. jbig2dec
  14. */
  15. #ifdef HAVE_CONFIG_H
  16. #include "config.h"
  17. #endif
  18. #include "os_types.h"
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <png.h>
  22. #ifndef OLD_LIB_PNG
  23. # if PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR < 2
  24. # define OLD_LIB_PNG 1
  25. # else
  26. # define OLD_LIB_PNG 0
  27. # endif
  28. #endif
  29. #if OLD_LIB_PNG
  30. #include <pngstruct.h>
  31. #endif
  32. #define CVT_PTR(ptr) (ptr)
  33. #include "jbig2.h"
  34. #include "jbig2_priv.h"
  35. #include "jbig2_image.h"
  36. /* take an image structure and write it out in png format */
  37. static void
  38. jbig2_png_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
  39. {
  40. png_size_t check;
  41. #if OLD_LIB_PNG
  42. png_FILE_p f = (png_FILE_p) png_ptr->io_ptr;
  43. #else
  44. png_FILE_p f = (png_FILE_p) png_get_io_ptr(png_ptr);
  45. #endif
  46. check = fwrite(data, 1, length, f);
  47. if (check != length) {
  48. png_error(png_ptr, "write error");
  49. }
  50. }
  51. static void
  52. jbig2_png_flush(png_structp png_ptr)
  53. {
  54. #if OLD_LIB_PNG
  55. png_FILE_p f = (png_FILE_p) png_ptr->io_ptr;
  56. #else
  57. png_FILE_p f = (png_FILE_p) png_get_io_ptr(png_ptr);
  58. #endif
  59. if (f != NULL)
  60. fflush(f);
  61. }
  62. /* write out an image struct in png format to an open file pointer */
  63. int
  64. jbig2_image_write_png(Jbig2Image *image, FILE *out)
  65. {
  66. uint32_t i;
  67. png_structp png;
  68. png_infop info;
  69. png_bytep rowpointer;
  70. png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
  71. if (png == NULL) {
  72. fprintf(stderr, "unable to create png structure\n");
  73. return 2;
  74. }
  75. info = png_create_info_struct(png);
  76. if (info == NULL) {
  77. fprintf(stderr, "unable to create png info structure\n");
  78. png_destroy_write_struct(&png, (png_infopp) NULL);
  79. return 3;
  80. }
  81. /* set/check error handling */
  82. if (setjmp(png_jmpbuf(png))) {
  83. /* we've returned here after an internal error */
  84. fprintf(stderr, "internal error in libpng saving file\n");
  85. png_destroy_write_struct(&png, &info);
  86. return 4;
  87. }
  88. /* png_init_io() doesn't work linking dynamically to libpng on win32
  89. one has to either link statically or use callbacks because of runtime
  90. variations */
  91. /* png_init_io(png, out); */
  92. png_set_write_fn(png, (png_voidp) out, jbig2_png_write_data, jbig2_png_flush);
  93. /* now we fill out the info structure with our format data */
  94. png_set_IHDR(png, info, image->width, image->height, 1, PNG_COLOR_TYPE_GRAY, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
  95. png_write_info(png, info);
  96. /* png natively treats 0 as black. This will convert for us */
  97. png_set_invert_mono(png);
  98. /* write out each row in turn */
  99. rowpointer = (png_bytep) image->data;
  100. for (i = 0; i < image->height; i++) {
  101. png_write_row(png, rowpointer);
  102. rowpointer += image->stride;
  103. }
  104. /* finish and clean up */
  105. png_write_end(png, info);
  106. png_destroy_write_struct(&png, &info);
  107. return 0;
  108. }
  109. int
  110. jbig2_image_write_png_file(Jbig2Image *image, char *filename)
  111. {
  112. FILE *out;
  113. int code;
  114. if ((out = fopen(filename, "wb")) == NULL) {
  115. fprintf(stderr, "unable to open '%s' for writing\n", filename);
  116. return 1;
  117. }
  118. code = jbig2_image_write_png(image, out);
  119. fclose(out);
  120. return (code);
  121. }