iccjpeg.c 8.6 KB

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