jcmarker.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. /*
  2. * jcmarker.c
  3. *
  4. * This file was part of the Independent JPEG Group's software:
  5. * Copyright (C) 1991-1998, Thomas G. Lane.
  6. * Modified 2003-2010 by Guido Vollbeding.
  7. * libjpeg-turbo Modifications:
  8. * Copyright (C) 2010, D. R. Commander.
  9. * For conditions of distribution and use, see the accompanying README.ijg
  10. * file.
  11. *
  12. * This file contains routines to write JPEG datastream markers.
  13. */
  14. #define JPEG_INTERNALS
  15. #include "jinclude.h"
  16. #include "jpeglib.h"
  17. #include "jpegcomp.h"
  18. typedef enum { /* JPEG marker codes */
  19. M_SOF0 = 0xc0,
  20. M_SOF1 = 0xc1,
  21. M_SOF2 = 0xc2,
  22. M_SOF3 = 0xc3,
  23. M_SOF5 = 0xc5,
  24. M_SOF6 = 0xc6,
  25. M_SOF7 = 0xc7,
  26. M_JPG = 0xc8,
  27. M_SOF9 = 0xc9,
  28. M_SOF10 = 0xca,
  29. M_SOF11 = 0xcb,
  30. M_SOF13 = 0xcd,
  31. M_SOF14 = 0xce,
  32. M_SOF15 = 0xcf,
  33. M_DHT = 0xc4,
  34. M_DAC = 0xcc,
  35. M_RST0 = 0xd0,
  36. M_RST1 = 0xd1,
  37. M_RST2 = 0xd2,
  38. M_RST3 = 0xd3,
  39. M_RST4 = 0xd4,
  40. M_RST5 = 0xd5,
  41. M_RST6 = 0xd6,
  42. M_RST7 = 0xd7,
  43. M_SOI = 0xd8,
  44. M_EOI = 0xd9,
  45. M_SOS = 0xda,
  46. M_DQT = 0xdb,
  47. M_DNL = 0xdc,
  48. M_DRI = 0xdd,
  49. M_DHP = 0xde,
  50. M_EXP = 0xdf,
  51. M_APP0 = 0xe0,
  52. M_APP1 = 0xe1,
  53. M_APP2 = 0xe2,
  54. M_APP3 = 0xe3,
  55. M_APP4 = 0xe4,
  56. M_APP5 = 0xe5,
  57. M_APP6 = 0xe6,
  58. M_APP7 = 0xe7,
  59. M_APP8 = 0xe8,
  60. M_APP9 = 0xe9,
  61. M_APP10 = 0xea,
  62. M_APP11 = 0xeb,
  63. M_APP12 = 0xec,
  64. M_APP13 = 0xed,
  65. M_APP14 = 0xee,
  66. M_APP15 = 0xef,
  67. M_JPG0 = 0xf0,
  68. M_JPG13 = 0xfd,
  69. M_COM = 0xfe,
  70. M_TEM = 0x01,
  71. M_ERROR = 0x100
  72. } JPEG_MARKER;
  73. /* Private state */
  74. typedef struct {
  75. struct jpeg_marker_writer pub; /* public fields */
  76. unsigned int last_restart_interval; /* last DRI value emitted; 0 after SOI */
  77. } my_marker_writer;
  78. typedef my_marker_writer *my_marker_ptr;
  79. /*
  80. * Basic output routines.
  81. *
  82. * Note that we do not support suspension while writing a marker.
  83. * Therefore, an application using suspension must ensure that there is
  84. * enough buffer space for the initial markers (typ. 600-700 bytes) before
  85. * calling jpeg_start_compress, and enough space to write the trailing EOI
  86. * (a few bytes) before calling jpeg_finish_compress. Multipass compression
  87. * modes are not supported at all with suspension, so those two are the only
  88. * points where markers will be written.
  89. */
  90. LOCAL(void)
  91. emit_byte(j_compress_ptr cinfo, int val)
  92. /* Emit a byte */
  93. {
  94. struct jpeg_destination_mgr *dest = cinfo->dest;
  95. *(dest->next_output_byte)++ = (JOCTET)val;
  96. if (--dest->free_in_buffer == 0) {
  97. if (!(*dest->empty_output_buffer) (cinfo))
  98. ERREXIT(cinfo, JERR_CANT_SUSPEND);
  99. }
  100. }
  101. LOCAL(void)
  102. emit_marker(j_compress_ptr cinfo, JPEG_MARKER mark)
  103. /* Emit a marker code */
  104. {
  105. emit_byte(cinfo, 0xFF);
  106. emit_byte(cinfo, (int)mark);
  107. }
  108. LOCAL(void)
  109. emit_2bytes(j_compress_ptr cinfo, int value)
  110. /* Emit a 2-byte integer; these are always MSB first in JPEG files */
  111. {
  112. emit_byte(cinfo, (value >> 8) & 0xFF);
  113. emit_byte(cinfo, value & 0xFF);
  114. }
  115. /*
  116. * Routines to write specific marker types.
  117. */
  118. LOCAL(int)
  119. emit_dqt(j_compress_ptr cinfo, int index)
  120. /* Emit a DQT marker */
  121. /* Returns the precision used (0 = 8bits, 1 = 16bits) for baseline checking */
  122. {
  123. JQUANT_TBL *qtbl = cinfo->quant_tbl_ptrs[index];
  124. int prec;
  125. int i;
  126. if (qtbl == NULL)
  127. ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, index);
  128. prec = 0;
  129. for (i = 0; i < DCTSIZE2; i++) {
  130. if (qtbl->quantval[i] > 255)
  131. prec = 1;
  132. }
  133. if (!qtbl->sent_table) {
  134. emit_marker(cinfo, M_DQT);
  135. emit_2bytes(cinfo, prec ? DCTSIZE2 * 2 + 1 + 2 : DCTSIZE2 + 1 + 2);
  136. emit_byte(cinfo, index + (prec << 4));
  137. for (i = 0; i < DCTSIZE2; i++) {
  138. /* The table entries must be emitted in zigzag order. */
  139. unsigned int qval = qtbl->quantval[jpeg_natural_order[i]];
  140. if (prec)
  141. emit_byte(cinfo, (int)(qval >> 8));
  142. emit_byte(cinfo, (int)(qval & 0xFF));
  143. }
  144. qtbl->sent_table = TRUE;
  145. }
  146. return prec;
  147. }
  148. LOCAL(int)
  149. emit_multi_dqt (j_compress_ptr cinfo)
  150. /* Emits a DQT marker containing all quantization tables */
  151. /* Returns number of emitted 16-bit tables, or -1 for failed for baseline checking. */
  152. {
  153. int prec[MAX_COMPONENTS];
  154. int seen[MAX_COMPONENTS] = { 0 };
  155. int fin_prec = 0;
  156. int ci;
  157. int size = 0;
  158. if (cinfo->master->compress_profile == JCP_FASTEST)
  159. return -1;
  160. for (ci = 0; ci < cinfo->num_components; ci++) {
  161. int tbl_num = cinfo->comp_info[ci].quant_tbl_no;
  162. int i;
  163. JQUANT_TBL * qtbl = cinfo->quant_tbl_ptrs[tbl_num];
  164. if (qtbl == NULL || qtbl->sent_table == TRUE)
  165. return -1;
  166. prec[ci] = 0;
  167. for (i = 0; i < DCTSIZE2; i++)
  168. prec[ci] = !!(prec[ci] + (qtbl->quantval[i] > 255));
  169. fin_prec += prec[ci];
  170. }
  171. emit_marker(cinfo, M_DQT);
  172. for (ci = 0; ci < cinfo->num_components; ci++) {
  173. int tbl_num = cinfo->comp_info[ci].quant_tbl_no;
  174. if (!seen[tbl_num]) {
  175. size += DCTSIZE2 * (prec[ci] + 1) + 1;
  176. seen[tbl_num] = 1;
  177. }
  178. }
  179. size += 2;
  180. emit_2bytes(cinfo, size);
  181. for (ci = 0; ci < cinfo->num_components; ci++) {
  182. int tbl_num = cinfo->comp_info[ci].quant_tbl_no;
  183. int i;
  184. JQUANT_TBL * qtbl = cinfo->quant_tbl_ptrs[tbl_num];
  185. if (qtbl->sent_table == TRUE)
  186. continue;
  187. emit_byte(cinfo, tbl_num + (prec[ci] << 4));
  188. for (i = 0; i < DCTSIZE2; i++) {
  189. unsigned int qval = qtbl->quantval[jpeg_natural_order[i]];
  190. if (prec[ci])
  191. emit_byte(cinfo, (int) (qval >> 8));
  192. emit_byte(cinfo, (int) (qval & 0xFF));
  193. }
  194. qtbl->sent_table = TRUE;
  195. }
  196. return fin_prec;
  197. }
  198. LOCAL(void)
  199. emit_dht(j_compress_ptr cinfo, int index, boolean is_ac)
  200. /* Emit a DHT marker */
  201. {
  202. JHUFF_TBL *htbl;
  203. int length, i;
  204. if (is_ac) {
  205. htbl = cinfo->ac_huff_tbl_ptrs[index];
  206. index += 0x10; /* output index has AC bit set */
  207. } else {
  208. htbl = cinfo->dc_huff_tbl_ptrs[index];
  209. }
  210. if (htbl == NULL)
  211. ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, index);
  212. if (!htbl->sent_table) {
  213. emit_marker(cinfo, M_DHT);
  214. length = 0;
  215. for (i = 1; i <= 16; i++)
  216. length += htbl->bits[i];
  217. emit_2bytes(cinfo, length + 2 + 1 + 16);
  218. emit_byte(cinfo, index);
  219. for (i = 1; i <= 16; i++)
  220. emit_byte(cinfo, htbl->bits[i]);
  221. for (i = 0; i < length; i++)
  222. emit_byte(cinfo, htbl->huffval[i]);
  223. htbl->sent_table = TRUE;
  224. }
  225. }
  226. LOCAL(boolean)
  227. emit_multi_dht (j_compress_ptr cinfo)
  228. /* Emit all DHT markers */
  229. /* Returns FALSE on failure, TRUE otherwise. */
  230. {
  231. int i, j;
  232. int length = 2;
  233. int dclens[NUM_HUFF_TBLS] = { 0 };
  234. int aclens[NUM_HUFF_TBLS] = { 0 };
  235. JHUFF_TBL *dcseen[NUM_HUFF_TBLS] = { NULL };
  236. JHUFF_TBL *acseen[NUM_HUFF_TBLS] = { NULL };
  237. if (cinfo->master->compress_profile == JCP_FASTEST)
  238. return 0;
  239. /* Calclate the total length. */
  240. for (i = 0; i < cinfo->comps_in_scan; i++) {
  241. jpeg_component_info *compptr = cinfo->cur_comp_info[i];
  242. int dcidx = compptr->dc_tbl_no;
  243. int acidx = compptr->ac_tbl_no;
  244. JHUFF_TBL *dctbl = cinfo->dc_huff_tbl_ptrs[dcidx];
  245. JHUFF_TBL *actbl = cinfo->ac_huff_tbl_ptrs[acidx];
  246. int seen = 0;
  247. /* Handle DC table lenghts */
  248. if (cinfo->Ss == 0 && cinfo->Ah == 0) {
  249. if (dctbl == NULL)
  250. ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, dcidx);
  251. if (dctbl->sent_table)
  252. continue;
  253. for (j = 0; j < NUM_HUFF_TBLS; j++)
  254. seen += (dctbl == dcseen[j]);
  255. if (seen)
  256. continue;
  257. dcseen[i] = dctbl;
  258. for (j = 1; j <= 16; j++)
  259. dclens[i] += dctbl->bits[j];
  260. length += dclens[i] + 16 + 1;
  261. }
  262. /* Handle AC table lengths */
  263. if (cinfo->Se) {
  264. if (actbl == NULL)
  265. ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, acidx + 0x10);
  266. if (actbl->sent_table)
  267. continue;
  268. seen = 0;
  269. for (j = 0; j < NUM_HUFF_TBLS; j++)
  270. seen += (actbl == acseen[j]);
  271. if (seen)
  272. continue;
  273. acseen[i] = actbl;
  274. for (j = 1; j <= 16; j++)
  275. aclens[i] += actbl->bits[j];
  276. length += aclens[i] + 16 + 1;
  277. }
  278. }
  279. /* Make sure we can fit it all into one DHT marker */
  280. if (length > (1 << 16) - 1)
  281. return FALSE;
  282. emit_marker(cinfo, M_DHT);
  283. emit_2bytes(cinfo, length);
  284. for (i = 0; i < cinfo->comps_in_scan; i++) {
  285. jpeg_component_info *compptr = cinfo->cur_comp_info[i];
  286. int dcidx = compptr->dc_tbl_no;
  287. int acidx = compptr->ac_tbl_no;
  288. JHUFF_TBL *dctbl = cinfo->dc_huff_tbl_ptrs[dcidx];
  289. JHUFF_TBL *actbl = cinfo->ac_huff_tbl_ptrs[acidx];
  290. acidx += 0x10;
  291. /* DC */
  292. if (cinfo->Ss == 0 && cinfo->Ah == 0 && !dctbl->sent_table) {
  293. emit_byte(cinfo, dcidx);
  294. for (j = 1; j <= 16; j++)
  295. emit_byte(cinfo, dctbl->bits[j]);
  296. for (j = 0; j < dclens[i]; j++)
  297. emit_byte(cinfo, dctbl->huffval[j]);
  298. dctbl->sent_table = TRUE;
  299. }
  300. if (cinfo->Se && !actbl->sent_table) {
  301. emit_byte(cinfo, acidx);
  302. for (j = 1; j <= 16; j++)
  303. emit_byte(cinfo, actbl->bits[j]);
  304. for (j = 0; j < aclens[i]; j++)
  305. emit_byte(cinfo, actbl->huffval[j]);
  306. actbl->sent_table = TRUE;
  307. }
  308. }
  309. return TRUE;
  310. }
  311. LOCAL(void)
  312. emit_dac(j_compress_ptr cinfo)
  313. /* Emit a DAC marker */
  314. /* Since the useful info is so small, we want to emit all the tables in */
  315. /* one DAC marker. Therefore this routine does its own scan of the table. */
  316. {
  317. #ifdef C_ARITH_CODING_SUPPORTED
  318. char dc_in_use[NUM_ARITH_TBLS];
  319. char ac_in_use[NUM_ARITH_TBLS];
  320. int length, i;
  321. jpeg_component_info *compptr;
  322. for (i = 0; i < NUM_ARITH_TBLS; i++)
  323. dc_in_use[i] = ac_in_use[i] = 0;
  324. for (i = 0; i < cinfo->comps_in_scan; i++) {
  325. compptr = cinfo->cur_comp_info[i];
  326. /* DC needs no table for refinement scan */
  327. if (cinfo->Ss == 0 && cinfo->Ah == 0)
  328. dc_in_use[compptr->dc_tbl_no] = 1;
  329. /* AC needs no table when not present */
  330. if (cinfo->Se)
  331. ac_in_use[compptr->ac_tbl_no] = 1;
  332. }
  333. length = 0;
  334. for (i = 0; i < NUM_ARITH_TBLS; i++)
  335. length += dc_in_use[i] + ac_in_use[i];
  336. if (length) {
  337. emit_marker(cinfo, M_DAC);
  338. emit_2bytes(cinfo, length * 2 + 2);
  339. for (i = 0; i < NUM_ARITH_TBLS; i++) {
  340. if (dc_in_use[i]) {
  341. emit_byte(cinfo, i);
  342. emit_byte(cinfo, cinfo->arith_dc_L[i] + (cinfo->arith_dc_U[i] << 4));
  343. }
  344. if (ac_in_use[i]) {
  345. emit_byte(cinfo, i + 0x10);
  346. emit_byte(cinfo, cinfo->arith_ac_K[i]);
  347. }
  348. }
  349. }
  350. #endif /* C_ARITH_CODING_SUPPORTED */
  351. }
  352. LOCAL(void)
  353. emit_dri(j_compress_ptr cinfo)
  354. /* Emit a DRI marker */
  355. {
  356. emit_marker(cinfo, M_DRI);
  357. emit_2bytes(cinfo, 4); /* fixed length */
  358. emit_2bytes(cinfo, (int)cinfo->restart_interval);
  359. }
  360. LOCAL(void)
  361. emit_sof(j_compress_ptr cinfo, JPEG_MARKER code)
  362. /* Emit a SOF marker */
  363. {
  364. int ci;
  365. jpeg_component_info *compptr;
  366. emit_marker(cinfo, code);
  367. emit_2bytes(cinfo, 3 * cinfo->num_components + 2 + 5 + 1); /* length */
  368. /* Make sure image isn't bigger than SOF field can handle */
  369. if ((long)cinfo->_jpeg_height > 65535L || (long)cinfo->_jpeg_width > 65535L)
  370. ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int)65535);
  371. emit_byte(cinfo, cinfo->data_precision);
  372. emit_2bytes(cinfo, (int)cinfo->_jpeg_height);
  373. emit_2bytes(cinfo, (int)cinfo->_jpeg_width);
  374. emit_byte(cinfo, cinfo->num_components);
  375. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  376. ci++, compptr++) {
  377. emit_byte(cinfo, compptr->component_id);
  378. emit_byte(cinfo, (compptr->h_samp_factor << 4) + compptr->v_samp_factor);
  379. emit_byte(cinfo, compptr->quant_tbl_no);
  380. }
  381. }
  382. LOCAL(void)
  383. emit_sos(j_compress_ptr cinfo)
  384. /* Emit a SOS marker */
  385. {
  386. int i, td, ta;
  387. jpeg_component_info *compptr;
  388. emit_marker(cinfo, M_SOS);
  389. emit_2bytes(cinfo, 2 * cinfo->comps_in_scan + 2 + 1 + 3); /* length */
  390. emit_byte(cinfo, cinfo->comps_in_scan);
  391. for (i = 0; i < cinfo->comps_in_scan; i++) {
  392. compptr = cinfo->cur_comp_info[i];
  393. emit_byte(cinfo, compptr->component_id);
  394. /* We emit 0 for unused field(s); this is recommended by the P&M text
  395. * but does not seem to be specified in the standard.
  396. */
  397. /* DC needs no table for refinement scan */
  398. td = cinfo->Ss == 0 && cinfo->Ah == 0 ? compptr->dc_tbl_no : 0;
  399. /* AC needs no table when not present */
  400. ta = cinfo->Se ? compptr->ac_tbl_no : 0;
  401. emit_byte(cinfo, (td << 4) + ta);
  402. }
  403. emit_byte(cinfo, cinfo->Ss);
  404. emit_byte(cinfo, cinfo->Se);
  405. emit_byte(cinfo, (cinfo->Ah << 4) + cinfo->Al);
  406. }
  407. LOCAL(void)
  408. emit_jfif_app0(j_compress_ptr cinfo)
  409. /* Emit a JFIF-compliant APP0 marker */
  410. {
  411. /*
  412. * Length of APP0 block (2 bytes)
  413. * Block ID (4 bytes - ASCII "JFIF")
  414. * Zero byte (1 byte to terminate the ID string)
  415. * Version Major, Minor (2 bytes - major first)
  416. * Units (1 byte - 0x00 = none, 0x01 = inch, 0x02 = cm)
  417. * Xdpu (2 bytes - dots per unit horizontal)
  418. * Ydpu (2 bytes - dots per unit vertical)
  419. * Thumbnail X size (1 byte)
  420. * Thumbnail Y size (1 byte)
  421. */
  422. emit_marker(cinfo, M_APP0);
  423. emit_2bytes(cinfo, 2 + 4 + 1 + 2 + 1 + 2 + 2 + 1 + 1); /* length */
  424. emit_byte(cinfo, 0x4A); /* Identifier: ASCII "JFIF" */
  425. emit_byte(cinfo, 0x46);
  426. emit_byte(cinfo, 0x49);
  427. emit_byte(cinfo, 0x46);
  428. emit_byte(cinfo, 0);
  429. emit_byte(cinfo, cinfo->JFIF_major_version); /* Version fields */
  430. emit_byte(cinfo, cinfo->JFIF_minor_version);
  431. emit_byte(cinfo, cinfo->density_unit); /* Pixel size information */
  432. emit_2bytes(cinfo, (int)cinfo->X_density);
  433. emit_2bytes(cinfo, (int)cinfo->Y_density);
  434. emit_byte(cinfo, 0); /* No thumbnail image */
  435. emit_byte(cinfo, 0);
  436. }
  437. LOCAL(void)
  438. emit_adobe_app14(j_compress_ptr cinfo)
  439. /* Emit an Adobe APP14 marker */
  440. {
  441. /*
  442. * Length of APP14 block (2 bytes)
  443. * Block ID (5 bytes - ASCII "Adobe")
  444. * Version Number (2 bytes - currently 100)
  445. * Flags0 (2 bytes - currently 0)
  446. * Flags1 (2 bytes - currently 0)
  447. * Color transform (1 byte)
  448. *
  449. * Although Adobe TN 5116 mentions Version = 101, all the Adobe files
  450. * now in circulation seem to use Version = 100, so that's what we write.
  451. *
  452. * We write the color transform byte as 1 if the JPEG color space is
  453. * YCbCr, 2 if it's YCCK, 0 otherwise. Adobe's definition has to do with
  454. * whether the encoder performed a transformation, which is pretty useless.
  455. */
  456. emit_marker(cinfo, M_APP14);
  457. emit_2bytes(cinfo, 2 + 5 + 2 + 2 + 2 + 1); /* length */
  458. emit_byte(cinfo, 0x41); /* Identifier: ASCII "Adobe" */
  459. emit_byte(cinfo, 0x64);
  460. emit_byte(cinfo, 0x6F);
  461. emit_byte(cinfo, 0x62);
  462. emit_byte(cinfo, 0x65);
  463. emit_2bytes(cinfo, 100); /* Version */
  464. emit_2bytes(cinfo, 0); /* Flags0 */
  465. emit_2bytes(cinfo, 0); /* Flags1 */
  466. switch (cinfo->jpeg_color_space) {
  467. case JCS_YCbCr:
  468. emit_byte(cinfo, 1); /* Color transform = 1 */
  469. break;
  470. case JCS_YCCK:
  471. emit_byte(cinfo, 2); /* Color transform = 2 */
  472. break;
  473. default:
  474. emit_byte(cinfo, 0); /* Color transform = 0 */
  475. break;
  476. }
  477. }
  478. /*
  479. * These routines allow writing an arbitrary marker with parameters.
  480. * The only intended use is to emit COM or APPn markers after calling
  481. * write_file_header and before calling write_frame_header.
  482. * Other uses are not guaranteed to produce desirable results.
  483. * Counting the parameter bytes properly is the caller's responsibility.
  484. */
  485. METHODDEF(void)
  486. write_marker_header(j_compress_ptr cinfo, int marker, unsigned int datalen)
  487. /* Emit an arbitrary marker header */
  488. {
  489. if (datalen > (unsigned int)65533) /* safety check */
  490. ERREXIT(cinfo, JERR_BAD_LENGTH);
  491. emit_marker(cinfo, (JPEG_MARKER)marker);
  492. emit_2bytes(cinfo, (int)(datalen + 2)); /* total length */
  493. }
  494. METHODDEF(void)
  495. write_marker_byte(j_compress_ptr cinfo, int val)
  496. /* Emit one byte of marker parameters following write_marker_header */
  497. {
  498. emit_byte(cinfo, val);
  499. }
  500. /*
  501. * Write datastream header.
  502. * This consists of an SOI and optional APPn markers.
  503. * We recommend use of the JFIF marker, but not the Adobe marker,
  504. * when using YCbCr or grayscale data. The JFIF marker should NOT
  505. * be used for any other JPEG colorspace. The Adobe marker is helpful
  506. * to distinguish RGB, CMYK, and YCCK colorspaces.
  507. * Note that an application can write additional header markers after
  508. * jpeg_start_compress returns.
  509. */
  510. METHODDEF(void)
  511. write_file_header(j_compress_ptr cinfo)
  512. {
  513. my_marker_ptr marker = (my_marker_ptr)cinfo->marker;
  514. emit_marker(cinfo, M_SOI); /* first the SOI */
  515. /* SOI is defined to reset restart interval to 0 */
  516. marker->last_restart_interval = 0;
  517. if (cinfo->write_JFIF_header) /* next an optional JFIF APP0 */
  518. emit_jfif_app0(cinfo);
  519. if (cinfo->write_Adobe_marker) /* next an optional Adobe APP14 */
  520. emit_adobe_app14(cinfo);
  521. }
  522. /*
  523. * Write frame header.
  524. * This consists of DQT and SOFn markers.
  525. * Note that we do not emit the SOF until we have emitted the DQT(s).
  526. * This avoids compatibility problems with incorrect implementations that
  527. * try to error-check the quant table numbers as soon as they see the SOF.
  528. */
  529. METHODDEF(void)
  530. write_frame_header(j_compress_ptr cinfo)
  531. {
  532. int ci, prec;
  533. boolean is_baseline;
  534. jpeg_component_info *compptr;
  535. /* Emit DQT for each quantization table.
  536. * Note that emit_dqt() suppresses any duplicate tables.
  537. */
  538. prec = emit_multi_dqt(cinfo);
  539. if (prec == -1) {
  540. prec = 0;
  541. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  542. ci++, compptr++) {
  543. prec += emit_dqt(cinfo, compptr->quant_tbl_no);
  544. }
  545. }
  546. /* now prec is nonzero iff there are any 16-bit quant tables. */
  547. /* Check for a non-baseline specification.
  548. * Note we assume that Huffman table numbers won't be changed later.
  549. */
  550. if (cinfo->arith_code || cinfo->progressive_mode ||
  551. cinfo->data_precision != 8) {
  552. is_baseline = FALSE;
  553. } else {
  554. is_baseline = TRUE;
  555. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  556. ci++, compptr++) {
  557. if (compptr->dc_tbl_no > 1 || compptr->ac_tbl_no > 1)
  558. is_baseline = FALSE;
  559. }
  560. if (prec && is_baseline) {
  561. is_baseline = FALSE;
  562. /* If it's baseline except for quantizer size, warn the user */
  563. TRACEMS(cinfo, 0, JTRC_16BIT_TABLES);
  564. }
  565. }
  566. /* Emit the proper SOF marker */
  567. if (cinfo->arith_code) {
  568. if (cinfo->progressive_mode)
  569. emit_sof(cinfo, M_SOF10); /* SOF code for progressive arithmetic */
  570. else
  571. emit_sof(cinfo, M_SOF9); /* SOF code for sequential arithmetic */
  572. } else {
  573. if (cinfo->progressive_mode)
  574. emit_sof(cinfo, M_SOF2); /* SOF code for progressive Huffman */
  575. else if (is_baseline)
  576. emit_sof(cinfo, M_SOF0); /* SOF code for baseline implementation */
  577. else
  578. emit_sof(cinfo, M_SOF1); /* SOF code for non-baseline Huffman file */
  579. }
  580. }
  581. /*
  582. * Write scan header.
  583. * This consists of DHT or DAC markers, optional DRI, and SOS.
  584. * Compressed data will be written following the SOS.
  585. */
  586. METHODDEF(void)
  587. write_scan_header(j_compress_ptr cinfo)
  588. {
  589. my_marker_ptr marker = (my_marker_ptr)cinfo->marker;
  590. int i;
  591. jpeg_component_info *compptr;
  592. if (cinfo->arith_code) {
  593. /* Emit arith conditioning info. We may have some duplication
  594. * if the file has multiple scans, but it's so small it's hardly
  595. * worth worrying about.
  596. */
  597. emit_dac(cinfo);
  598. } else {
  599. /* Emit Huffman tables.
  600. * Note that emit_dht() suppresses any duplicate tables.
  601. */
  602. if (!emit_multi_dht(cinfo)) {
  603. for (i = 0; i < cinfo->comps_in_scan; i++) {
  604. compptr = cinfo->cur_comp_info[i];
  605. /* DC needs no table for refinement scan */
  606. if (cinfo->Ss == 0 && cinfo->Ah == 0)
  607. emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
  608. /* AC needs no table when not present */
  609. if (cinfo->Se)
  610. emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
  611. }
  612. }
  613. }
  614. /* Emit DRI if required --- note that DRI value could change for each scan.
  615. * We avoid wasting space with unnecessary DRIs, however.
  616. */
  617. if (cinfo->restart_interval != marker->last_restart_interval) {
  618. emit_dri(cinfo);
  619. marker->last_restart_interval = cinfo->restart_interval;
  620. }
  621. emit_sos(cinfo);
  622. }
  623. /*
  624. * Write datastream trailer.
  625. */
  626. METHODDEF(void)
  627. write_file_trailer(j_compress_ptr cinfo)
  628. {
  629. emit_marker(cinfo, M_EOI);
  630. }
  631. /*
  632. * Write an abbreviated table-specification datastream.
  633. * This consists of SOI, DQT and DHT tables, and EOI.
  634. * Any table that is defined and not marked sent_table = TRUE will be
  635. * emitted. Note that all tables will be marked sent_table = TRUE at exit.
  636. */
  637. METHODDEF(void)
  638. write_tables_only(j_compress_ptr cinfo)
  639. {
  640. int i;
  641. emit_marker(cinfo, M_SOI);
  642. for (i = 0; i < NUM_QUANT_TBLS; i++) {
  643. if (cinfo->quant_tbl_ptrs[i] != NULL)
  644. (void)emit_dqt(cinfo, i);
  645. }
  646. if (!cinfo->arith_code) {
  647. for (i = 0; i < NUM_HUFF_TBLS; i++) {
  648. if (cinfo->dc_huff_tbl_ptrs[i] != NULL)
  649. emit_dht(cinfo, i, FALSE);
  650. if (cinfo->ac_huff_tbl_ptrs[i] != NULL)
  651. emit_dht(cinfo, i, TRUE);
  652. }
  653. }
  654. emit_marker(cinfo, M_EOI);
  655. }
  656. /*
  657. * Initialize the marker writer module.
  658. */
  659. GLOBAL(void)
  660. jinit_marker_writer(j_compress_ptr cinfo)
  661. {
  662. my_marker_ptr marker;
  663. /* Create the subobject */
  664. marker = (my_marker_ptr)
  665. (*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE,
  666. sizeof(my_marker_writer));
  667. cinfo->marker = (struct jpeg_marker_writer *)marker;
  668. /* Initialize method pointers */
  669. marker->pub.write_file_header = write_file_header;
  670. marker->pub.write_frame_header = write_frame_header;
  671. marker->pub.write_scan_header = write_scan_header;
  672. marker->pub.write_file_trailer = write_file_trailer;
  673. marker->pub.write_tables_only = write_tables_only;
  674. marker->pub.write_marker_header = write_marker_header;
  675. marker->pub.write_marker_byte = write_marker_byte;
  676. /* Initialize private state */
  677. marker->last_restart_interval = 0;
  678. }