iccjpeg.c 8.4 KB

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