|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* jcicc.c
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* Copyright (C) 1997-1998, Thomas G. Lane, Todd Newman.
|
|
shun-iwasawa |
82a8f5 |
* Copyright (C) 2017, D. R. Commander.
|
|
shun-iwasawa |
82a8f5 |
* For conditions of distribution and use, see the accompanying README.ijg
|
|
shun-iwasawa |
82a8f5 |
* file.
|
|
shun-iwasawa |
82a8f5 |
*
|
|
shun-iwasawa |
82a8f5 |
* This file provides code to write International Color Consortium (ICC) device
|
|
shun-iwasawa |
82a8f5 |
* profiles embedded in JFIF JPEG image files. The ICC has defined a standard
|
|
shun-iwasawa |
82a8f5 |
* for including such data in JPEG "APP2" markers. The code given here does
|
|
shun-iwasawa |
82a8f5 |
* not know anything about the internal structure of the ICC profile data; it
|
|
shun-iwasawa |
82a8f5 |
* just knows how to embed the profile data in a JPEG file while writing it.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#define JPEG_INTERNALS
|
|
shun-iwasawa |
82a8f5 |
#include "jinclude.h"
|
|
shun-iwasawa |
82a8f5 |
#include "jpeglib.h"
|
|
shun-iwasawa |
82a8f5 |
#include "jerror.h"
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* Since an ICC profile can be larger than the maximum size of a JPEG marker
|
|
shun-iwasawa |
82a8f5 |
* (64K), we need provisions to split it into multiple markers. The format
|
|
shun-iwasawa |
82a8f5 |
* defined by the ICC specifies one or more APP2 markers containing the
|
|
shun-iwasawa |
82a8f5 |
* following data:
|
|
shun-iwasawa |
82a8f5 |
* Identifying string ASCII "ICC_PROFILE\0" (12 bytes)
|
|
shun-iwasawa |
82a8f5 |
* Marker sequence number 1 for first APP2, 2 for next, etc (1 byte)
|
|
shun-iwasawa |
82a8f5 |
* Number of markers Total number of APP2's used (1 byte)
|
|
shun-iwasawa |
82a8f5 |
* Profile data (remainder of APP2 data)
|
|
shun-iwasawa |
82a8f5 |
* Decoders should use the marker sequence numbers to reassemble the profile,
|
|
shun-iwasawa |
82a8f5 |
* rather than assuming that the APP2 markers appear in the correct sequence.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
#define ICC_MARKER (JPEG_APP0 + 2) /* JPEG marker code for ICC */
|
|
shun-iwasawa |
82a8f5 |
#define ICC_OVERHEAD_LEN 14 /* size of non-profile data in APP2 */
|
|
shun-iwasawa |
82a8f5 |
#define MAX_BYTES_IN_MARKER 65533 /* maximum data len of a JPEG marker */
|
|
shun-iwasawa |
82a8f5 |
#define MAX_DATA_BYTES_IN_MARKER (MAX_BYTES_IN_MARKER - ICC_OVERHEAD_LEN)
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/*
|
|
shun-iwasawa |
82a8f5 |
* This routine writes the given ICC profile data into a JPEG file. It *must*
|
|
shun-iwasawa |
82a8f5 |
* be called AFTER calling jpeg_start_compress() and BEFORE the first call to
|
|
shun-iwasawa |
82a8f5 |
* jpeg_write_scanlines(). (This ordering ensures that the APP2 marker(s) will
|
|
shun-iwasawa |
82a8f5 |
* appear after the SOI and JFIF or Adobe markers, but before all else.)
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
GLOBAL(void)
|
|
shun-iwasawa |
82a8f5 |
jpeg_write_icc_profile(j_compress_ptr cinfo, const JOCTET *icc_data_ptr,
|
|
shun-iwasawa |
82a8f5 |
unsigned int icc_data_len)
|
|
shun-iwasawa |
82a8f5 |
{
|
|
shun-iwasawa |
82a8f5 |
unsigned int num_markers; /* total number of markers we'll write */
|
|
shun-iwasawa |
82a8f5 |
int cur_marker = 1; /* per spec, counting starts at 1 */
|
|
shun-iwasawa |
82a8f5 |
unsigned int length; /* number of bytes to write in this marker */
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
if (icc_data_ptr == NULL || icc_data_len == 0)
|
|
shun-iwasawa |
82a8f5 |
ERREXIT(cinfo, JERR_BUFFER_SIZE);
|
|
shun-iwasawa |
82a8f5 |
if (cinfo->global_state < CSTATE_SCANNING)
|
|
shun-iwasawa |
82a8f5 |
ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Calculate the number of markers we'll need, rounding up of course */
|
|
shun-iwasawa |
82a8f5 |
num_markers = icc_data_len / MAX_DATA_BYTES_IN_MARKER;
|
|
shun-iwasawa |
82a8f5 |
if (num_markers * MAX_DATA_BYTES_IN_MARKER != icc_data_len)
|
|
shun-iwasawa |
82a8f5 |
num_markers++;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
while (icc_data_len > 0) {
|
|
shun-iwasawa |
82a8f5 |
/* length of profile to put in this marker */
|
|
shun-iwasawa |
82a8f5 |
length = icc_data_len;
|
|
shun-iwasawa |
82a8f5 |
if (length > MAX_DATA_BYTES_IN_MARKER)
|
|
shun-iwasawa |
82a8f5 |
length = MAX_DATA_BYTES_IN_MARKER;
|
|
shun-iwasawa |
82a8f5 |
icc_data_len -= length;
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Write the JPEG marker header (APP2 code and marker length) */
|
|
shun-iwasawa |
82a8f5 |
jpeg_write_m_header(cinfo, ICC_MARKER,
|
|
shun-iwasawa |
82a8f5 |
(unsigned int)(length + ICC_OVERHEAD_LEN));
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Write the marker identifying string "ICC_PROFILE" (null-terminated). We
|
|
shun-iwasawa |
82a8f5 |
* code it in this less-than-transparent way so that the code works even if
|
|
shun-iwasawa |
82a8f5 |
* the local character set is not ASCII.
|
|
shun-iwasawa |
82a8f5 |
*/
|
|
shun-iwasawa |
82a8f5 |
jpeg_write_m_byte(cinfo, 0x49);
|
|
shun-iwasawa |
82a8f5 |
jpeg_write_m_byte(cinfo, 0x43);
|
|
shun-iwasawa |
82a8f5 |
jpeg_write_m_byte(cinfo, 0x43);
|
|
shun-iwasawa |
82a8f5 |
jpeg_write_m_byte(cinfo, 0x5F);
|
|
shun-iwasawa |
82a8f5 |
jpeg_write_m_byte(cinfo, 0x50);
|
|
shun-iwasawa |
82a8f5 |
jpeg_write_m_byte(cinfo, 0x52);
|
|
shun-iwasawa |
82a8f5 |
jpeg_write_m_byte(cinfo, 0x4F);
|
|
shun-iwasawa |
82a8f5 |
jpeg_write_m_byte(cinfo, 0x46);
|
|
shun-iwasawa |
82a8f5 |
jpeg_write_m_byte(cinfo, 0x49);
|
|
shun-iwasawa |
82a8f5 |
jpeg_write_m_byte(cinfo, 0x4C);
|
|
shun-iwasawa |
82a8f5 |
jpeg_write_m_byte(cinfo, 0x45);
|
|
shun-iwasawa |
82a8f5 |
jpeg_write_m_byte(cinfo, 0x0);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Add the sequencing info */
|
|
shun-iwasawa |
82a8f5 |
jpeg_write_m_byte(cinfo, cur_marker);
|
|
shun-iwasawa |
82a8f5 |
jpeg_write_m_byte(cinfo, (int)num_markers);
|
|
shun-iwasawa |
82a8f5 |
|
|
shun-iwasawa |
82a8f5 |
/* Add the profile data */
|
|
shun-iwasawa |
82a8f5 |
while (length--) {
|
|
shun-iwasawa |
82a8f5 |
jpeg_write_m_byte(cinfo, *icc_data_ptr);
|
|
shun-iwasawa |
82a8f5 |
icc_data_ptr++;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
cur_marker++;
|
|
shun-iwasawa |
82a8f5 |
}
|
|
shun-iwasawa |
82a8f5 |
}
|