64 int huff_codes_present[4];
87 0x00,0x01,0x05,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
89 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,
96 0x00,0x03,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,
98 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,
105 0x00,0x02,0x01,0x03,0x03,0x02,0x04,0x03,0x05,0x05,0x04,0x04,0x00,0x00,0x01,0x7D,
107 0x01,0x02,0x03,0x00,0x04,0x11,0x05,0x12,0x21,0x31,0x41,0x06,0x13,0x51,0x61,
108 0x07,0x22,0x71,0x14,0x32,0x81,0x91,0xA1,0x08,0x23,0x42,0xB1,0xC1,0x15,0x52,0xD1,0xF0,0x24,
109 0x33,0x62,0x72,0x82,0x09,0x0A,0x16,0x17,0x18,0x19,0x1A,0x25,0x26,0x27,0x28,0x29,0x2A,0x34,
110 0x35,0x36,0x37,0x38,0x39,0x3A,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x53,0x54,0x55,0x56,
111 0x57,0x58,0x59,0x5A,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,0x73,0x74,0x75,0x76,0x77,0x78,
112 0x79,0x7A,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,
113 0x9A,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,
114 0xBA,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,
115 0xDA,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,
123 0x00,0x02,0x01,0x02,0x04,0x04,0x03,0x04,0x07,0x05,0x04,0x04,0x00,0x01,0x02,0x77,
125 0x00,0x01,0x02,0x03,0x11,0x04,0x05,0x21,0x31,0x06,0x12,0x41,0x51,0x07,0x61,0x71,
126 0x13,0x22,0x32,0x81,0x08,0x14,0x42,0x91,0xA1,0xB1,0xC1,0x09,0x23,0x33,0x52,0xF0,0x15,0x62,
127 0x72,0xD1,0x0A,0x16,0x24,0x34,0xE1,0x25,0xF1,0x17,0x18,0x19,0x1A,0x26,0x27,0x28,0x29,0x2A,
128 0x35,0x36,0x37,0x38,0x39,0x3A,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x53,0x54,0x55,0x56,
129 0x57,0x58,0x59,0x5A,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,0x73,0x74,0x75,0x76,0x77,0x78,
130 0x79,0x7A,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x92,0x93,0x94,0x95,0x96,0x97,0x98,
131 0x99,0x9A,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,
132 0xB9,0xBA,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,
133 0xD9,0xDA,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,
137 static inline uint8_t
max_u8(uint8_t a, uint8_t b)
139 return a > b ? a : b;
143 static const char ZZ[64] = { 0, 1, 8, 16, 9, 2, 3, 10,
144 17, 24, 32, 25, 18, 11, 4, 5,
145 12, 19, 26, 33, 40, 48, 41, 34,
146 27, 20, 13, 6, 7, 14, 21, 28,
147 35, 42, 49, 56, 57, 50, 43, 36,
148 29, 22, 15, 23, 30, 37, 44, 51,
149 58, 59, 52, 45, 38, 31, 39, 46,
150 53, 60, 61, 54, 47, 55, 62, 63 };
173 printf(
"hallucinating 1s!\n");
180 uint8_t nextbyte = bd->
in[bd->
inpos];
190 bd->
bits = (bd->
bits << 8) | nextbyte;
249 int marker_sync_skipped = 0;
250 int marker_sync_skipped_from_offset = 0;
260 if (marker_sync_skipped == 0)
261 marker_sync_skipped_from_offset = marker_offset;
262 marker_sync_skipped++;
266 if (marker_sync_skipped) {
267 printf(
"%08x: skipped %04x bytes\n", marker_sync_skipped_from_offset, marker_sync_skipped);
268 marker_sync_skipped = 0;
300 if (((length-2) % 65) != 0)
304 for (
int offset = 0; offset < length - 2; offset += 65) {
310 if ((pqtq & 0xf0) != 0 || (pqtq & 0x0f) >= 4)
313 uint8_t
id = pqtq & 3;
315 for (
int i = 0; i < 64; i++)
337 if (nf < 1 || nf > 3)
343 for (
int i = 0; i < nf; i++) {
372 printf(
"pjepg.c: unsupported JPEG type %02x\n", marker);
385 uint8_t Tc = (TcTh >> 4);
386 int Th = TcTh & 0x0f;
388 if (Tc >= 2 || Th >= 2)
393 int htidx = Tc*2 + Th;
403 uint32_t code_pos = 0;
406 int nvalues = L[
nbits];
410 uint32_t ncodes = (1 << (16 -
nbits));
413 for (
int vi = 0; vi < nvalues; vi++) {
416 if (code_pos + ncodes > 0xffff)
419 for (
int ci = 0; ci < ncodes; ci++) {
446 uint8_t comp_idx[ns];
447 memset(comp_idx, 0, ns*
sizeof(uint8_t));
449 for (
int i = 0; i < ns; i++) {
479 if (ss != 0 || se != 0x3f || Ahl != 0x00)
483 int maxmcux = 0, maxmcuy = 0;
484 for (
int i = 0; i < ns; i++) {
492 int mcus_x = (pjd->
width + maxmcux - 1) / maxmcux;
493 int mcus_y = (pjd->
height + maxmcuy - 1) / maxmcuy;
496 printf(
"Image has %d x %d MCU blocks, each %d x %d pixels\n",
497 mcus_x, mcus_y, maxmcux, maxmcuy);
500 for (
int i = 0; i < ns; i++) {
507 if ((comp->
stride % alignment) != 0)
516 memset(dcpred, 0,
sizeof(dcpred));
520 for (
int mcu_y = 0; mcu_y < mcus_y; mcu_y++) {
521 for (
int mcu_x = 0; mcu_x < mcus_x; mcu_x++) {
545 printf(
"RST SYNC\n");
557 memset(dcpred, 0,
sizeof(dcpred));
560 for (
int nsidx = 0; nsidx < ns; nsidx++) {
566 int qtabidx = comp->
tq;
568 for (
int sby = 0; sby < comp->
scaley; sby++) {
569 for (
int sbx = 0; sbx < comp->
scalex; sbx++) {
571 memset(block, 0,
sizeof(block));
573 int dc_huff_table_idx = comp->
tda >> 4;
574 int ac_huff_table_idx = 2 + (comp->
tda & 0x0f);
587 int ssss = huff_code->
code & 0x0f;
591 if ((value & (1 << (ssss-1))) == 0)
592 value += ((-1) << ssss) + 1;
594 dcpred[nsidx] += value;
595 block[0] = dcpred[nsidx] * pjd->
qtab[qtabidx][0];
600 for (
int coeff = 1; coeff < 64; coeff++) {
607 if (huff_code->
code == 0) {
611 int rrrr = huff_code->
code >> 4;
612 int ssss = huff_code->
code & 0x0f;
617 if ((value & (1 << (ssss-1))) == 0)
618 value += ((-1) << ssss) + 1;
622 block[(int)
ZZ[coeff]] = value * pjd->
qtab[qtabidx][coeff];
631 uint32_t comp_x = (mcu_x * comp->
scalex + sbx) * 8;
632 uint32_t comp_y = (mcu_y * comp->
scaley + sby) * 8;
633 uint32_t dataidx = comp_y * comp->
stride + comp_x;
651 goto got_end_of_image;
666 printf(
"pjepg: Unknown marker %02x at offset %04x\n", marker, marker_offset);
704 for (
int y = 0; y < im->
height; y++)
746 if (Cr_factor_y == 1 && Cr_factor_x == 1 && Cb_factor_y == 1 && Cb_factor_x == 1) {
748 for (
int y = 0; y < pj->
height; y++) {
749 for (
int x = 0; x < pj->
width; x++) {
750 int32_t y_val = Y->
data[y*Y->
stride + x] * 65536;
751 int32_t cb_val = Cb->
data[y*Cb->
stride + x] - 128;
752 int32_t cr_val = Cr->
data[y*Cr->
stride + x] - 128;
754 int32_t r_val = y_val + 91881 * cr_val;
755 int32_t g_val = y_val + -22554 * cb_val - 46802 * cr_val;
756 int32_t b_val = y_val + 116130 * cb_val;
763 }
else if (Cb_factor_y == Cr_factor_y && Cb_factor_x == Cr_factor_x) {
764 for (
int by = 0; by < pj->
height / Cb_factor_y; by++) {
765 for (
int bx = 0; bx < pj->
width / Cb_factor_x; bx++) {
767 int32_t cb_val = Cb->
data[by*Cb->
stride + bx] - 128;
768 int32_t cr_val = Cr->
data[by*Cr->
stride + bx] - 128;
770 int32_t r0 = 91881 * cr_val;
771 int32_t g0 = -22554 * cb_val - 46802 * cr_val;
772 int32_t b0 = 116130 * cb_val;
774 for (
int dy = 0; dy < Cb_factor_y; dy++) {
775 int y = by*Cb_factor_y + dy;
777 for (
int dx = 0; dx < Cb_factor_x; dx++) {
778 int x = bx*Cb_factor_x + dx;
780 int32_t y_val = Y->
data[y*Y->
stride + x] * 65536;
782 int32_t r_val = r0 + y_val;
783 int32_t g_val = g0 + y_val;
784 int32_t b_val = b0 + y_val;
795 for (
int y = 0; y < pj->
height; y++) {
796 for (
int x = 0; x < pj->
width; x++) {
798 int32_t cb_val = Cb->
data[(y / Cb_factor_y)*Cb->
stride + (x / Cb_factor_x)] - 128;
799 int32_t cr_val = Cr->
data[(y / Cr_factor_y)*Cr->
stride + (x / Cr_factor_x)] - 128;
801 uint8_t r_val =
clampd(y_val + 1.402 * cr_val);
802 uint8_t g_val =
clampd(y_val - 0.34414 * cb_val - 0.71414 * cr_val);
803 uint8_t b_val =
clampd(y_val + 1.772 * cb_val);
805 im->
buf[y*im->
stride + 3*x + 0 ] = r_val;
806 im->
buf[y*im->
stride + 3*x + 1 ] = g_val;
807 im->
buf[y*im->
stride + 3*x + 2 ] = b_val;
819 FILE *f = fopen(path,
"r");
823 fseek(f, 0, SEEK_END);
824 long buflen = ftell(f);
826 uint8_t *buf = malloc(buflen);
827 fseek(f, 0, SEEK_SET);
828 int res = fread(buf, 1, buflen, f);
846 memset(&pjd, 0,
sizeof(pjd));
void pjpeg_idct_2D_double(int32_t in[64], uint8_t *out, uint32_t outstride)
int huff_codes_present[4]
void pjpeg_destroy(pjpeg_t *pj)
image_u8_t * image_u8_create(unsigned int width, unsigned int height)
pjpeg_component_t * components
static uint32_t bd_consume_bits(struct bit_decoder *bd, int nbits)
static void bd_discard_to_byte_boundary(struct bit_decoder *bd)
pjpeg_t * pjpeg_create_from_file(const char *path, uint32_t flags, int *error)
static uint32_t bd_get_offset(struct bit_decoder *bd)
static uint8_t max_u8(uint8_t a, uint8_t b)
struct pjpeg_huffman_code huff_codes[4][65536]
void pjpeg_idct_2D_nanojpeg(int32_t in[64], uint8_t *out, uint32_t outstride)
static uint8_t clamp_u8(int32_t v)
image_u8x3_t * pjpeg_to_u8x3_baseline(pjpeg_t *pj)
static void bd_discard_bytes(struct bit_decoder *bd, int nbytes)
static int bd_has_more(struct bit_decoder *bd)
static void bd_ensure(struct bit_decoder *bd, int nbits)
image_u8x3_t * image_u8x3_create(unsigned int width, unsigned int height)
pjpeg_t * pjpeg_create_from_buffer(uint8_t *buf, int buflen, uint32_t flags, int *error)
pjpeg_component_t * components
static uint8_t clampd(double v)
static uint8_t mjpeg_dht[]
image_u8_t * pjpeg_to_u8_baseline(pjpeg_t *pj)
static int pjpeg_decode_buffer(struct pjpeg_decode_state *pjd)
static uint32_t bd_peek_bits(struct bit_decoder *bd, int nbits)
void pjpeg_idct_2D_u32(int32_t in[64], uint8_t *out, uint32_t outstride)