jdicc.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * jdicc.c
  3. *
  4. * Copyright (C) 1997-1998, Thomas G. Lane, Todd Newman.
  5. * Copyright (C) 2017, D. R. Commander.
  6. * For conditions of distribution and use, see the accompanying README.ijg
  7. * file.
  8. *
  9. * This file provides code to read International Color Consortium (ICC) device
  10. * profiles embedded in JFIF JPEG image files. The ICC has defined a standard
  11. * for including such data in JPEG "APP2" markers. The code given here does
  12. * not know anything about the internal structure of the ICC profile data; it
  13. * just knows how to get the profile data from a JPEG file while reading it.
  14. */
  15. #define JPEG_INTERNALS
  16. #include "jinclude.h"
  17. #include "jpeglib.h"
  18. #include "jerror.h"
  19. #define ICC_MARKER (JPEG_APP0 + 2) /* JPEG marker code for ICC */
  20. #define ICC_OVERHEAD_LEN 14 /* size of non-profile data in APP2 */
  21. /*
  22. * Handy subroutine to test whether a saved marker is an ICC profile marker.
  23. */
  24. LOCAL(boolean)
  25. marker_is_icc(jpeg_saved_marker_ptr marker)
  26. {
  27. return
  28. marker->marker == ICC_MARKER &&
  29. marker->data_length >= ICC_OVERHEAD_LEN &&
  30. /* verify the identifying string */
  31. marker->data[0] == 0x49 &&
  32. marker->data[1] == 0x43 &&
  33. marker->data[2] == 0x43 &&
  34. marker->data[3] == 0x5F &&
  35. marker->data[4] == 0x50 &&
  36. marker->data[5] == 0x52 &&
  37. marker->data[6] == 0x4F &&
  38. marker->data[7] == 0x46 &&
  39. marker->data[8] == 0x49 &&
  40. marker->data[9] == 0x4C &&
  41. marker->data[10] == 0x45 &&
  42. marker->data[11] == 0x0;
  43. }
  44. /*
  45. * See if there was an ICC profile in the JPEG file being read; if so,
  46. * reassemble and return the profile data.
  47. *
  48. * TRUE is returned if an ICC profile was found, FALSE if not. If TRUE is
  49. * returned, *icc_data_ptr is set to point to the returned data, and
  50. * *icc_data_len is set to its length.
  51. *
  52. * IMPORTANT: the data at *icc_data_ptr is allocated with malloc() and must be
  53. * freed by the caller with free() when the caller no longer needs it.
  54. * (Alternatively, we could write this routine to use the IJG library's memory
  55. * allocator, so that the data would be freed implicitly when
  56. * jpeg_finish_decompress() is called. But it seems likely that many
  57. * applications will prefer to have the data stick around after decompression
  58. * finishes.)
  59. */
  60. GLOBAL(boolean)
  61. jpeg_read_icc_profile(j_decompress_ptr cinfo, JOCTET **icc_data_ptr,
  62. unsigned int *icc_data_len)
  63. {
  64. jpeg_saved_marker_ptr marker;
  65. int num_markers = 0;
  66. int seq_no;
  67. JOCTET *icc_data;
  68. unsigned int total_length;
  69. #define MAX_SEQ_NO 255 /* sufficient since marker numbers are bytes */
  70. char marker_present[MAX_SEQ_NO + 1]; /* 1 if marker found */
  71. unsigned int data_length[MAX_SEQ_NO + 1]; /* size of profile data in marker */
  72. unsigned int data_offset[MAX_SEQ_NO + 1]; /* offset for data in marker */
  73. if (icc_data_ptr == NULL || icc_data_len == NULL)
  74. ERREXIT(cinfo, JERR_BUFFER_SIZE);
  75. if (cinfo->global_state < DSTATE_READY)
  76. ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
  77. *icc_data_ptr = NULL; /* avoid confusion if FALSE return */
  78. *icc_data_len = 0;
  79. /* This first pass over the saved markers discovers whether there are
  80. * any ICC markers and verifies the consistency of the marker numbering.
  81. */
  82. for (seq_no = 1; seq_no <= MAX_SEQ_NO; seq_no++)
  83. marker_present[seq_no] = 0;
  84. for (marker = cinfo->marker_list; marker != NULL; marker = marker->next) {
  85. if (marker_is_icc(marker)) {
  86. if (num_markers == 0)
  87. num_markers = marker->data[13];
  88. else if (num_markers != marker->data[13]) {
  89. WARNMS(cinfo, JWRN_BOGUS_ICC); /* inconsistent num_markers fields */
  90. return FALSE;
  91. }
  92. seq_no = marker->data[12];
  93. if (seq_no <= 0 || seq_no > num_markers) {
  94. WARNMS(cinfo, JWRN_BOGUS_ICC); /* bogus sequence number */
  95. return FALSE;
  96. }
  97. if (marker_present[seq_no]) {
  98. WARNMS(cinfo, JWRN_BOGUS_ICC); /* duplicate sequence numbers */
  99. return FALSE;
  100. }
  101. marker_present[seq_no] = 1;
  102. data_length[seq_no] = marker->data_length - ICC_OVERHEAD_LEN;
  103. }
  104. }
  105. if (num_markers == 0)
  106. return FALSE;
  107. /* Check for missing markers, count total space needed,
  108. * compute offset of each marker's part of the data.
  109. */
  110. total_length = 0;
  111. for (seq_no = 1; seq_no <= num_markers; seq_no++) {
  112. if (marker_present[seq_no] == 0) {
  113. WARNMS(cinfo, JWRN_BOGUS_ICC); /* missing sequence number */
  114. return FALSE;
  115. }
  116. data_offset[seq_no] = total_length;
  117. total_length += data_length[seq_no];
  118. }
  119. if (total_length == 0) {
  120. WARNMS(cinfo, JWRN_BOGUS_ICC); /* found only empty markers? */
  121. return FALSE;
  122. }
  123. /* Allocate space for assembled data */
  124. icc_data = (JOCTET *)malloc(total_length * sizeof(JOCTET));
  125. if (icc_data == NULL)
  126. ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 11); /* oops, out of memory */
  127. /* and fill it in */
  128. for (marker = cinfo->marker_list; marker != NULL; marker = marker->next) {
  129. if (marker_is_icc(marker)) {
  130. JOCTET FAR *src_ptr;
  131. JOCTET *dst_ptr;
  132. unsigned int length;
  133. seq_no = marker->data[12];
  134. dst_ptr = icc_data + data_offset[seq_no];
  135. src_ptr = marker->data + ICC_OVERHEAD_LEN;
  136. length = data_length[seq_no];
  137. while (length--) {
  138. *dst_ptr++ = *src_ptr++;
  139. }
  140. }
  141. }
  142. *icc_data_ptr = icc_data;
  143. *icc_data_len = total_length;
  144. return TRUE;
  145. }