jcmarker.c 18 KB

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