iccjpeg.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. /*
  5. * iccjpeg.c
  6. *
  7. * This file provides code to read and write International Color Consortium
  8. * (ICC) device profiles embedded in JFIF JPEG image files. The ICC has
  9. * defined a standard format for including such data in JPEG "APP2" markers.
  10. * The code given here does not know anything about the internal structure
  11. * of the ICC profile data; it just knows how to put the profile data into
  12. * a JPEG file being written, or get it back out when reading.
  13. *
  14. * This code depends on new features added to the IJG JPEG library as of
  15. * IJG release 6b; it will not compile or work with older IJG versions.
  16. *
  17. * NOTE: this code would need surgery to work on 16-bit-int machines
  18. * with ICC profiles exceeding 64K bytes in size. If you need to do that,
  19. * change all the "unsigned int" variables to "INT32". You'll also need
  20. * to find a malloc() replacement that can allocate more than 64K.
  21. */
  22. #include "iccjpeg.h"
  23. #include <stdlib.h> /* define malloc() */
  24. /*
  25. * Since an ICC profile can be larger than the maximum size of a JPEG marker
  26. * (64K), we need provisions to split it into multiple markers. The format
  27. * defined by the ICC specifies one or more APP2 markers containing the
  28. * following data:
  29. * Identifying string ASCII "ICC_PROFILE\0" (12 bytes)
  30. * Marker sequence number 1 for first APP2, 2 for next, etc (1 byte)
  31. * Number of markers Total number of APP2's used (1 byte)
  32. * Profile data (remainder of APP2 data)
  33. * Decoders should use the marker sequence numbers to reassemble the profile,
  34. * rather than assuming that the APP2 markers appear in the correct sequence.
  35. */
  36. #define ICC_MARKER (JPEG_APP0 + 2) /* JPEG marker code for ICC */
  37. #define ICC_OVERHEAD_LEN 14 /* size of non-profile data in APP2 */
  38. #define MAX_BYTES_IN_MARKER 65533 /* maximum data len of a JPEG marker */
  39. #define MAX_DATA_BYTES_IN_MARKER (MAX_BYTES_IN_MARKER - ICC_OVERHEAD_LEN)
  40. /*
  41. * Prepare for reading an ICC profile
  42. */
  43. void
  44. setup_read_icc_profile (j_decompress_ptr cinfo)
  45. {
  46. /* Tell the library to keep any APP2 data it may find */
  47. jpeg_save_markers(cinfo, ICC_MARKER, 0xFFFF);
  48. }
  49. /*
  50. * Handy subroutine to test whether a saved marker is an ICC profile marker.
  51. */
  52. static boolean
  53. marker_is_icc (jpeg_saved_marker_ptr marker)
  54. {
  55. return
  56. marker->marker == ICC_MARKER &&
  57. marker->data_length >= ICC_OVERHEAD_LEN &&
  58. /* verify the identifying string */
  59. GETJOCTET(marker->data[0]) == 0x49 &&
  60. GETJOCTET(marker->data[1]) == 0x43 &&
  61. GETJOCTET(marker->data[2]) == 0x43 &&
  62. GETJOCTET(marker->data[3]) == 0x5F &&
  63. GETJOCTET(marker->data[4]) == 0x50 &&
  64. GETJOCTET(marker->data[5]) == 0x52 &&
  65. GETJOCTET(marker->data[6]) == 0x4F &&
  66. GETJOCTET(marker->data[7]) == 0x46 &&
  67. GETJOCTET(marker->data[8]) == 0x49 &&
  68. GETJOCTET(marker->data[9]) == 0x4C &&
  69. GETJOCTET(marker->data[10]) == 0x45 &&
  70. GETJOCTET(marker->data[11]) == 0x0;
  71. }
  72. /*
  73. * See if there was an ICC profile in the JPEG file being read;
  74. * if so, reassemble and return the profile data.
  75. *
  76. * TRUE is returned if an ICC profile was found, FALSE if not.
  77. * If TRUE is returned, *icc_data_ptr is set to point to the
  78. * returned data, and *icc_data_len is set to its length.
  79. *
  80. * IMPORTANT: the data at **icc_data_ptr has been allocated with malloc()
  81. * and must be freed by the caller with free() when the caller no longer
  82. * needs it. (Alternatively, we could write this routine to use the
  83. * IJG library's memory allocator, so that the data would be freed implicitly
  84. * at jpeg_finish_decompress() time. But it seems likely that many apps
  85. * will prefer to have the data stick around after decompression finishes.)
  86. *
  87. * NOTE: if the file contains invalid ICC APP2 markers, we just silently
  88. * return FALSE. You might want to issue an error message instead.
  89. */
  90. boolean
  91. read_icc_profile (j_decompress_ptr cinfo,
  92. JOCTET** icc_data_ptr,
  93. unsigned int* icc_data_len)
  94. {
  95. jpeg_saved_marker_ptr marker;
  96. int num_markers = 0;
  97. int seq_no;
  98. JOCTET* icc_data;
  99. unsigned int total_length;
  100. #define MAX_SEQ_NO 255 /* sufficient since marker numbers are bytes */
  101. char marker_present[MAX_SEQ_NO+1]; /* 1 if marker found */
  102. unsigned int data_length[MAX_SEQ_NO+1]; /* size of profile data in marker */
  103. unsigned int data_offset[MAX_SEQ_NO+1]; /* offset for data in marker */
  104. *icc_data_ptr = NULL; /* avoid confusion if FALSE return */
  105. *icc_data_len = 0;
  106. /* This first pass over the saved markers discovers whether there are
  107. * any ICC markers and verifies the consistency of the marker numbering.
  108. */
  109. for (seq_no = 1; seq_no <= MAX_SEQ_NO; seq_no++) {
  110. marker_present[seq_no] = 0;
  111. }
  112. for (marker = cinfo->marker_list; marker != NULL; marker = marker->next) {
  113. if (marker_is_icc(marker)) {
  114. if (num_markers == 0) {
  115. num_markers = GETJOCTET(marker->data[13]);
  116. } else if (num_markers != GETJOCTET(marker->data[13])) {
  117. return FALSE; /* inconsistent num_markers fields */
  118. }
  119. seq_no = GETJOCTET(marker->data[12]);
  120. if (seq_no <= 0 || seq_no > num_markers) {
  121. return FALSE; /* bogus sequence number */
  122. }
  123. if (marker_present[seq_no]) {
  124. return FALSE; /* duplicate sequence numbers */
  125. }
  126. marker_present[seq_no] = 1;
  127. data_length[seq_no] = marker->data_length - ICC_OVERHEAD_LEN;
  128. }
  129. }
  130. if (num_markers == 0) {
  131. return FALSE;
  132. }
  133. /* Check for missing markers, count total space needed,
  134. * compute offset of each marker's part of the data.
  135. */
  136. total_length = 0;
  137. for (seq_no = 1; seq_no <= num_markers; seq_no++) {
  138. if (marker_present[seq_no] == 0) {
  139. return FALSE; /* missing sequence number */
  140. }
  141. data_offset[seq_no] = total_length;
  142. total_length += data_length[seq_no];
  143. }
  144. if (total_length <= 0) {
  145. return FALSE; /* found only empty markers? */
  146. }
  147. /* Allocate space for assembled data */
  148. icc_data = (JOCTET*) malloc(total_length * sizeof(JOCTET));
  149. if (icc_data == NULL) {
  150. return FALSE; /* oops, out of memory */
  151. }
  152. /* and fill it in */
  153. for (marker = cinfo->marker_list; marker != NULL; marker = marker->next) {
  154. if (marker_is_icc(marker)) {
  155. JOCTET FAR* src_ptr;
  156. JOCTET* dst_ptr;
  157. unsigned int length;
  158. seq_no = GETJOCTET(marker->data[12]);
  159. dst_ptr = icc_data + data_offset[seq_no];
  160. src_ptr = marker->data + ICC_OVERHEAD_LEN;
  161. length = data_length[seq_no];
  162. while (length--) {
  163. *dst_ptr++ = *src_ptr++;
  164. }
  165. }
  166. }
  167. *icc_data_ptr = icc_data;
  168. *icc_data_len = total_length;
  169. return TRUE;
  170. }