71 int huff_codes_present[4];
94 0x00,0x01,0x05,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
96 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,
103 0x00,0x03,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,
105 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,
112 0x00,0x02,0x01,0x03,0x03,0x02,0x04,0x03,0x05,0x05,0x04,0x04,0x00,0x00,0x01,0x7D,
114 0x01,0x02,0x03,0x00,0x04,0x11,0x05,0x12,0x21,0x31,0x41,0x06,0x13,0x51,0x61,
115 0x07,0x22,0x71,0x14,0x32,0x81,0x91,0xA1,0x08,0x23,0x42,0xB1,0xC1,0x15,0x52,0xD1,0xF0,0x24,
116 0x33,0x62,0x72,0x82,0x09,0x0A,0x16,0x17,0x18,0x19,0x1A,0x25,0x26,0x27,0x28,0x29,0x2A,0x34,
117 0x35,0x36,0x37,0x38,0x39,0x3A,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x53,0x54,0x55,0x56,
118 0x57,0x58,0x59,0x5A,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,0x73,0x74,0x75,0x76,0x77,0x78,
119 0x79,0x7A,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,
120 0x9A,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,
121 0xBA,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,
122 0xDA,0xE1,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xF1,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,
130 0x00,0x02,0x01,0x02,0x04,0x04,0x03,0x04,0x07,0x05,0x04,0x04,0x00,0x01,0x02,0x77,
132 0x00,0x01,0x02,0x03,0x11,0x04,0x05,0x21,0x31,0x06,0x12,0x41,0x51,0x07,0x61,0x71,
133 0x13,0x22,0x32,0x81,0x08,0x14,0x42,0x91,0xA1,0xB1,0xC1,0x09,0x23,0x33,0x52,0xF0,0x15,0x62,
134 0x72,0xD1,0x0A,0x16,0x24,0x34,0xE1,0x25,0xF1,0x17,0x18,0x19,0x1A,0x26,0x27,0x28,0x29,0x2A,
135 0x35,0x36,0x37,0x38,0x39,0x3A,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x53,0x54,0x55,0x56,
136 0x57,0x58,0x59,0x5A,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,0x73,0x74,0x75,0x76,0x77,0x78,
137 0x79,0x7A,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x92,0x93,0x94,0x95,0x96,0x97,0x98,
138 0x99,0x9A,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,
139 0xB9,0xBA,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,
140 0xD9,0xDA,0xE2,0xE3,0xE4,0xE5,0xE6,0xE7,0xE8,0xE9,0xEA,0xF2,0xF3,0xF4,0xF5,0xF6,0xF7,0xF8,
144 static inline uint8_t
max_u8(uint8_t a, uint8_t b)
146 return a > b ? a : b;
150 static const char ZZ[64] = { 0, 1, 8, 16, 9, 2, 3, 10,
151 17, 24, 32, 25, 18, 11, 4, 5,
152 12, 19, 26, 33, 40, 48, 41, 34,
153 27, 20, 13, 6, 7, 14, 21, 28,
154 35, 42, 49, 56, 57, 50, 43, 36,
155 29, 22, 15, 23, 30, 37, 44, 51,
156 58, 59, 52, 45, 38, 31, 39, 46,
157 53, 60, 61, 54, 47, 55, 62, 63 };
180 printf(
"hallucinating 1s!\n");
187 uint8_t nextbyte = bd->
in[bd->
inpos];
197 bd->
bits = (bd->
bits << 8) | nextbyte;
256 int marker_sync_skipped = 0;
257 int marker_sync_skipped_from_offset = 0;
267 if (marker_sync_skipped == 0)
268 marker_sync_skipped_from_offset = marker_offset;
269 marker_sync_skipped++;
273 if (marker_sync_skipped) {
274 printf(
"%08x: skipped %04x bytes\n", marker_sync_skipped_from_offset, marker_sync_skipped);
275 marker_sync_skipped = 0;
307 if (((length-2) % 65) != 0)
311 for (
int offset = 0; offset < length - 2; offset += 65) {
317 if ((pqtq & 0xf0) != 0 || (pqtq & 0x0f) >= 4)
320 uint8_t
id = pqtq & 3;
322 for (
int i = 0; i < 64; i++)
344 if (nf < 1 || nf > 3)
350 for (
int i = 0; i < nf; i++) {
379 printf(
"pjepg.c: unsupported JPEG type %02x\n", marker);
392 uint8_t Tc = (TcTh >> 4);
393 int Th = TcTh & 0x0f;
395 if (Tc >= 2 || Th >= 2)
400 int htidx = Tc*2 + Th;
410 uint32_t code_pos = 0;
413 int nvalues = L[
nbits];
417 uint32_t ncodes = (1 << (16 -
nbits));
420 for (
int vi = 0; vi < nvalues; vi++) {
423 if (code_pos + ncodes > 0xffff)
426 for (
int ci = 0; ci < ncodes; ci++) {
453 uint8_t *comp_idx = calloc(ns,
sizeof(uint8_t));
455 for (
int i = 0; i < ns; i++) {
485 if (ss != 0 || se != 0x3f || Ahl != 0x00)
489 int maxmcux = 0, maxmcuy = 0;
490 for (
int i = 0; i < ns; i++) {
498 int mcus_x = (pjd->
width + maxmcux - 1) / maxmcux;
499 int mcus_y = (pjd->
height + maxmcuy - 1) / maxmcuy;
502 printf(
"Image has %d x %d MCU blocks, each %d x %d pixels\n",
503 mcus_x, mcus_y, maxmcux, maxmcuy);
506 for (
int i = 0; i < ns; i++) {
513 if ((comp->
stride % alignment) != 0)
521 int32_t *dcpred = calloc(ns,
sizeof(int32_t));
525 for (
int mcu_y = 0; mcu_y < mcus_y; mcu_y++) {
526 for (
int mcu_x = 0; mcu_x < mcus_x; mcu_x++) {
550 printf(
"RST SYNC\n");
562 memset(dcpred, 0,
sizeof(*dcpred));
565 for (
int nsidx = 0; nsidx < ns; nsidx++) {
571 int qtabidx = comp->
tq;
573 for (
int sby = 0; sby < comp->
scaley; sby++) {
574 for (
int sbx = 0; sbx < comp->
scalex; sbx++) {
576 memset(block, 0,
sizeof(block));
578 int dc_huff_table_idx = comp->
tda >> 4;
579 int ac_huff_table_idx = 2 + (comp->
tda & 0x0f);
592 int ssss = huff_code->
code & 0x0f;
596 if ((value & (1 << (ssss-1))) == 0)
597 value += ((-1) << ssss) + 1;
599 dcpred[nsidx] += value;
600 block[0] = dcpred[nsidx] * pjd->
qtab[qtabidx][0];
605 for (
int coeff = 1; coeff < 64; coeff++) {
612 if (huff_code->
code == 0) {
616 int rrrr = huff_code->
code >> 4;
617 int ssss = huff_code->
code & 0x0f;
622 if ((value & (1 << (ssss-1))) == 0)
623 value += ((-1) << ssss) + 1;
627 block[(int)
ZZ[coeff]] = value * pjd->
qtab[qtabidx][coeff];
636 uint32_t comp_x = (mcu_x * comp->
scalex + sbx) * 8;
637 uint32_t comp_y = (mcu_y * comp->
scaley + sby) * 8;
638 uint32_t dataidx = comp_y * comp->
stride + comp_x;
659 goto got_end_of_image;
674 printf(
"pjepg: Unknown marker %02x at offset %04x\n", marker, marker_offset);
712 for (
int y = 0; y < im->
height; y++)
754 if (Cr_factor_y == 1 && Cr_factor_x == 1 && Cb_factor_y == 1 && Cb_factor_x == 1) {
756 for (
int y = 0; y < pj->
height; y++) {
757 for (
int x = 0; x < pj->
width; x++) {
758 int32_t y_val = Y->
data[y*Y->
stride + x] * 65536;
759 int32_t cb_val = Cb->
data[y*Cb->
stride + x] - 128;
760 int32_t cr_val = Cr->
data[y*Cr->
stride + x] - 128;
762 int32_t r_val = y_val + 91881 * cr_val;
763 int32_t g_val = y_val + -22554 * cb_val - 46802 * cr_val;
764 int32_t b_val = y_val + 116130 * cb_val;
771 }
else if (Cb_factor_y == Cr_factor_y && Cb_factor_x == Cr_factor_x) {
772 for (
int by = 0; by < pj->
height / Cb_factor_y; by++) {
773 for (
int bx = 0; bx < pj->
width / Cb_factor_x; bx++) {
775 int32_t cb_val = Cb->
data[by*Cb->
stride + bx] - 128;
776 int32_t cr_val = Cr->
data[by*Cr->
stride + bx] - 128;
778 int32_t r0 = 91881 * cr_val;
779 int32_t g0 = -22554 * cb_val - 46802 * cr_val;
780 int32_t b0 = 116130 * cb_val;
782 for (
int dy = 0; dy < Cb_factor_y; dy++) {
783 int y = by*Cb_factor_y + dy;
785 for (
int dx = 0; dx < Cb_factor_x; dx++) {
786 int x = bx*Cb_factor_x + dx;
788 int32_t y_val = Y->
data[y*Y->
stride + x] * 65536;
790 int32_t r_val = r0 + y_val;
791 int32_t g_val = g0 + y_val;
792 int32_t b_val = b0 + y_val;
803 for (
int y = 0; y < pj->
height; y++) {
804 for (
int x = 0; x < pj->
width; x++) {
806 int32_t cb_val = Cb->
data[(y / Cb_factor_y)*Cb->
stride + (x / Cb_factor_x)] - 128;
807 int32_t cr_val = Cr->
data[(y / Cr_factor_y)*Cr->
stride + (x / Cr_factor_x)] - 128;
809 uint8_t r_val =
clampd(y_val + 1.402 * cr_val);
810 uint8_t g_val =
clampd(y_val - 0.34414 * cb_val - 0.71414 * cr_val);
811 uint8_t b_val =
clampd(y_val + 1.772 * cb_val);
813 im->
buf[y*im->
stride + 3*x + 0 ] = r_val;
814 im->
buf[y*im->
stride + 3*x + 1 ] = g_val;
815 im->
buf[y*im->
stride + 3*x + 2 ] = b_val;
827 FILE *f = fopen(path,
"rb");
831 fseek(f, 0, SEEK_END);
832 long buflen = ftell(f);
834 uint8_t *buf = malloc(buflen);
835 fseek(f, 0, SEEK_SET);
836 int res = fread(buf, 1, buflen, f);
860 memset(&pjd, 0,
sizeof(pjd));
#define debug_print(fmt,...)
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)
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)
image_u8x3_t * image_u8x3_create(unsigned int width, unsigned int height)
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)
pjpeg_t * pjpeg_create_from_buffer(uint8_t *buf, int buflen, uint32_t flags, int *error)
image_u8_t * image_u8_create(unsigned int width, unsigned int height)
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)