Skip to content

Instantly share code, notes, and snippets.

@jcupitt
Created August 9, 2022 22:38
Show Gist options
  • Save jcupitt/058bf71cf0de53f8b9207c221719d21c to your computer and use it in GitHub Desktop.
Save jcupitt/058bf71cf0de53f8b9207c221719d21c to your computer and use it in GitHub Desktop.
/*
* add-profile.c ... paste an ICC profile into a JPG file, without
* decompress/recompress.
*
* Adapted from wrjpgcom.c
*
* compile with:
*
* gcc -g -Wall add-profile.c `pkg-config glib-2.0 --cflags --libs`
*/
#include <glib.h>
#include <stdio.h>
/*
* These macros are used to read the input file and write the output file.
* To reuse this code in another application, you might need to change these.
*/
static FILE * infile; /* input JPEG file */
/* Return next input byte, or EOF if no more */
#define NEXTBYTE() getc(infile)
static FILE * outfile; /* output JPEG file */
/* Emit an output byte */
#define PUTBYTE(x) putc((x), outfile)
#define ERREXIT(msg) (fprintf(stderr, "%s\n", msg), exit(1))
/* Read one byte, testing for EOF */
static int
read_1_byte (void)
{
int c;
c = NEXTBYTE();
if (c == EOF)
ERREXIT("Premature EOF in JPEG file");
return c;
}
/* Read 2 bytes, convert to unsigned int */
/* All 2-byte quantities in JPEG markers are MSB first */
static unsigned int
read_2_bytes (void)
{
int c1, c2;
c1 = NEXTBYTE();
if (c1 == EOF)
ERREXIT("Premature EOF in JPEG file");
c2 = NEXTBYTE();
if (c2 == EOF)
ERREXIT("Premature EOF in JPEG file");
return (((unsigned int) c1) << 8) + ((unsigned int) c2);
}
/* Routines to write data to output file */
static void
write_1_byte (int c)
{
PUTBYTE(c);
}
static void
write_2_bytes (unsigned int val)
{
PUTBYTE((val >> 8) & 0xFF);
PUTBYTE(val & 0xFF);
}
static void
write_marker (int marker)
{
PUTBYTE(0xFF);
PUTBYTE(marker);
}
static void
copy_rest_of_file (void)
{
int c;
printf("copy_rest_of_file:\n");
while ((c = NEXTBYTE()) != EOF)
PUTBYTE(c);
}
/*
* JPEG markers consist of one or more 0xFF bytes, followed by a marker
* code byte (which is not an FF). Here are the marker codes of interest
* in this program. (See jdmarker.c for a more complete list.)
*/
#define M_SOF0 0xC0 /* Start Of Frame N */
#define M_SOF1 0xC1 /* N indicates which compression process */
#define M_SOF2 0xC2 /* Only SOF0-SOF2 are now in common use */
#define M_SOF3 0xC3
#define M_SOF5 0xC5 /* NB: codes C4 and CC are NOT SOF markers */
#define M_SOF6 0xC6
#define M_SOF7 0xC7
#define M_SOF9 0xC9
#define M_SOF10 0xCA
#define M_SOF11 0xCB
#define M_SOF13 0xCD
#define M_SOF14 0xCE
#define M_SOF15 0xCF
#define M_SOI 0xD8 /* Start Of Image (beginning of datastream) */
#define M_EOI 0xD9 /* End Of Image (end of datastream) */
#define M_DQT 0xDB /* Quant tables */
#define M_SOS 0xDA /* Start Of Scan (begins compressed data) */
#define JPEG_APP0 0xE0 /* JFIF header */
#define JPEG_APP1 (JPEG_APP0 + 1) /* XMP or EXIF */
#define JPEG_APP2 (JPEG_APP0 + 2) /* ICC profile */
#define JPEG_APP13 (JPEG_APP0 + 13) /* IPTC */
#define JPEG_APP14 (JPEG_APP0 + 14) /* adobe block */
#define M_COM 0xFE /* COMment */
/*
* Find the next JPEG marker and return its marker code.
* We expect at least one FF byte, possibly more if the compressor used FFs
* to pad the file. (Padding FFs will NOT be replicated in the output file.)
* There could also be non-FF garbage between markers. The treatment of such
* garbage is unspecified; we choose to skip over it but emit a warning msg.
* NB: this routine must not be used after seeing SOS marker, since it will
* not deal correctly with FF/00 sequences in the compressed image data...
*/
static int
next_marker (void)
{
int c;
int discarded_bytes = 0;
/* Find 0xFF byte; count and skip any non-FFs. */
c = read_1_byte();
while (c != 0xFF) {
discarded_bytes++;
c = read_1_byte();
}
/* Get marker code byte, swallowing any duplicate FF bytes. Extra FFs
* are legal as pad bytes, so don't count them in discarded_bytes.
*/
do {
c = read_1_byte();
} while (c == 0xFF);
if (discarded_bytes != 0) {
fprintf(stderr, "Warning: garbage data found in JPEG file\n");
}
return c;
}
/*
* Read the initial marker, which should be SOI.
* For a JFIF file, the first two bytes of the file should be literally
* 0xFF M_SOI. To be more general, we could use next_marker, but if the
* input file weren't actually JPEG at all, next_marker might read the whole
* file and then return a misleading error message...
*/
static int
first_marker (void)
{
int c1, c2;
c1 = NEXTBYTE();
c2 = NEXTBYTE();
if (c1 != 0xFF || c2 != M_SOI)
ERREXIT("Not a JPEG file");
return c2;
}
/*
* Most types of marker are followed by a variable-length parameter segment.
* This routine skips over the parameters for any marker we don't otherwise
* want to process.
* Note that we MUST skip the parameter segment explicitly in order not to
* be fooled by 0xFF bytes that might appear within the parameter segment;
* such bytes do NOT introduce new markers.
*/
static void
copy_variable (void)
/* Copy an unknown or uninteresting variable-length marker */
{
unsigned int length;
/* Get the marker parameter length count */
length = read_2_bytes();
printf( "copying %d bytes\n", length);
write_2_bytes(length);
/* Length includes itself, so must be at least 2 */
if (length < 2)
ERREXIT("Erroneous JPEG marker length");
length -= 2;
/* Copy the remaining bytes */
while (length > 0) {
write_1_byte(read_1_byte());
length--;
}
}
static void
skip_variable (void)
/* Skip over an unknown or uninteresting variable-length marker */
{
unsigned int length;
/* Get the marker parameter length count */
length = read_2_bytes();
printf( "skipping %d bytes\n", length);
/* Length includes itself, so must be at least 2 */
if (length < 2)
ERREXIT("Erroneous JPEG marker length");
length -= 2;
/* Skip over the remaining bytes */
while (length > 0) {
(void) read_1_byte();
length--;
}
}
/*
* Parse the marker stream until SOFn or EOI is seen;
* copy data to output, but discard COM markers unless keep_COM is true.
*/
static int
copy_to_app2 (void)
{
int marker;
/* Expect SOI at start of file */
if (first_marker() != M_SOI)
ERREXIT("Expected SOI marker first");
write_marker(M_SOI);
for (;;) {
marker = next_marker();
printf( "marker = 0x%x\n", marker );
switch (marker) {
/* APP2 must come after APP0, APP1 and Adobe (APP14), but before
* everything else.
*/
case JPEG_APP0:
case JPEG_APP0 + 1:
case JPEG_APP0 + 14:
/* Copy and loop.
*/
write_marker(marker);
copy_variable();
break;
case JPEG_APP0 + 2: /* Existing app2, skip and don't copy */
printf( "skipping existing APP2\n" );
skip_variable();
break;
default:
/* We've reached the first thing AFTER the spot where app2
* should go.
*/
return marker;
}
} /* end loop */
}
int
main (int argc, char **argv)
{
char *profile;
size_t profile_length;
int marker;
if (!(infile = fopen(argv[1], "rb")))
ERREXIT("can't open input");
if (!(outfile = fopen(argv[2], "wb")))
ERREXIT("can't open output");
if (!g_file_get_contents(argv[3], &profile, &profile_length, NULL))
ERREXIT("can't open output");
/* APP2 must be after the SOI and JFIF or Adobe markers, but before
* everything else.
*/
/* Copy JPEG headers until SOFn marker, discarding any APP2 (icc profile).
* we will insert the new APP2 just before SOFn.
*/
marker = copy_to_app2();
/* Insert the new APP2 marker */
write_marker(JPEG_APP2);
write_2_bytes(profile_length + 2 + 14);
/* "ICC_PROFILE", null terminated */
write_1_byte(0x49);
write_1_byte(0x43);
write_1_byte(0x43);
write_1_byte(0x5F);
write_1_byte(0x50);
write_1_byte(0x52);
write_1_byte(0x4F);
write_1_byte(0x46);
write_1_byte(0x49);
write_1_byte(0x4C);
write_1_byte(0x45);
write_1_byte(0x0);
/* profile marker number ... chunk 1 (numbering from 1) or 1 chunks in total
*/
write_1_byte(0x1);
write_1_byte(0x1);
printf("inserting %ld bytes\n", profile_length);
while (profile_length > 0) {
write_1_byte(*profile++);
profile_length--;
}
/* Duplicate the remainder of the source file.
* Note that any COM markers occuring after SOF will not be touched.
*/
write_marker(marker);
copy_rest_of_file();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment