jbig2_image_pbm.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 <ctype.h>
  21. #include "jbig2.h"
  22. #include "jbig2_priv.h"
  23. #include "jbig2_image.h"
  24. #include "jbig2_image_rw.h"
  25. /* take an image structure and write it to a file in pbm format */
  26. int
  27. jbig2_image_write_pbm_file(Jbig2Image *image, char *filename)
  28. {
  29. FILE *out;
  30. int code;
  31. if ((out = fopen(filename, "wb")) == NULL) {
  32. fprintf(stderr, "unable to open '%s' for writing", filename);
  33. return 1;
  34. }
  35. code = jbig2_image_write_pbm(image, out);
  36. fclose(out);
  37. return (code);
  38. }
  39. /* write out an image struct as a pbm stream to an open file pointer */
  40. int
  41. jbig2_image_write_pbm(Jbig2Image *image, FILE *out)
  42. {
  43. /* pbm header */
  44. fprintf(out, "P4\n%d %d\n", image->width, image->height);
  45. /* pbm format pads to a byte boundary, so we can
  46. just write out the whole data buffer
  47. NB: this assumes minimal stride for the width */
  48. fwrite(image->data, 1, image->height * image->stride, out);
  49. /* success */
  50. return 0;
  51. }
  52. /* take an image from a file in pbm format */
  53. Jbig2Image *
  54. jbig2_image_read_pbm_file(Jbig2Ctx *ctx, char *filename)
  55. {
  56. FILE *in;
  57. Jbig2Image *image;
  58. if ((in = fopen(filename, "rb")) == NULL) {
  59. fprintf(stderr, "unable to open '%s' for reading\n", filename);
  60. return NULL;
  61. }
  62. image = jbig2_image_read_pbm(ctx, in);
  63. fclose(in);
  64. return (image);
  65. }
  66. /* FIXME: should handle multi-image files */
  67. Jbig2Image *
  68. jbig2_image_read_pbm(Jbig2Ctx *ctx, FILE *in)
  69. {
  70. int i, dim[2];
  71. int done;
  72. Jbig2Image *image;
  73. int c;
  74. char buf[32];
  75. /* look for 'P4' magic */
  76. while ((c = fgetc(in)) != 'P') {
  77. if (feof(in))
  78. return NULL;
  79. }
  80. if ((c = fgetc(in)) != '4') {
  81. fprintf(stderr, "not a binary pbm file.\n");
  82. return NULL;
  83. }
  84. /* read size. we must find two decimal numbers representing
  85. the image dimensions. 'done' will index whether we're
  86. looking for the width or the height and 'i' will be our
  87. array index for copying strings into our buffer */
  88. done = 0;
  89. i = 0;
  90. while (done < 2) {
  91. c = fgetc(in);
  92. /* skip whitespace */
  93. if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
  94. continue;
  95. /* skip comments */
  96. if (c == '#') {
  97. while ((c = fgetc(in)) != '\n');
  98. continue;
  99. }
  100. /* report unexpected eof */
  101. if (c == EOF) {
  102. fprintf(stderr, "end-of-file parsing pbm header\n");
  103. return NULL;
  104. }
  105. if (isdigit(c)) {
  106. buf[i++] = c;
  107. while (isdigit(c = fgetc(in))) {
  108. if (i >= 32) {
  109. fprintf(stderr, "pbm parsing error\n");
  110. return NULL;
  111. }
  112. buf[i++] = c;
  113. }
  114. buf[i] = '\0';
  115. if (sscanf(buf, "%d", &dim[done]) != 1) {
  116. fprintf(stderr, "failed to read pbm image dimensions\n");
  117. return NULL;
  118. }
  119. i = 0;
  120. done++;
  121. }
  122. }
  123. /* allocate image structure */
  124. image = jbig2_image_new(ctx, dim[0], dim[1]);
  125. if (image == NULL) {
  126. fprintf(stderr, "failed to allocate %dx%d image for pbm file\n", dim[0], dim[1]);
  127. return NULL;
  128. }
  129. /* the pbm data is byte-aligned, so we can
  130. do a simple block read */
  131. (void)fread(image->data, 1, image->height * image->stride, in);
  132. if (feof(in)) {
  133. fprintf(stderr, "unexpected end of pbm file.\n");
  134. jbig2_image_release(ctx, image);
  135. return NULL;
  136. }
  137. /* success */
  138. return image;
  139. }