PageRenderTime 51ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/FreeImage/Source/FreeImage/PluginJPEG.cpp

https://bitbucket.org/cabalistic/ogredeps/
C++ | 1801 lines | 1042 code | 330 blank | 429 comment | 256 complexity | eac9a6561e8829034f59d4fefed92708 MD5 | raw file
Possible License(s): LGPL-3.0, BSD-3-Clause, CPL-1.0, Unlicense, GPL-2.0, GPL-3.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, BSD-2-Clause, LGPL-2.1
  1. // ==========================================================
  2. // JPEG Loader and writer
  3. // Based on code developed by The Independent JPEG Group
  4. //
  5. // Design and implementation by
  6. // - Floris van den Berg (flvdberg@wxs.nl)
  7. // - Jan L. Nauta (jln@magentammt.com)
  8. // - Markus Loibl (markus.loibl@epost.de)
  9. // - Karl-Heinz Bussian (khbussian@moss.de)
  10. // - Hervé Drolon (drolon@infonie.fr)
  11. // - Jascha Wetzel (jascha@mainia.de)
  12. // - Mihail Naydenov (mnaydenov@users.sourceforge.net)
  13. //
  14. // This file is part of FreeImage 3
  15. //
  16. // COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
  17. // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
  18. // THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
  19. // OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
  20. // CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
  21. // THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
  22. // SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
  23. // PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
  24. // THIS DISCLAIMER.
  25. //
  26. // Use at your own risk!
  27. // ==========================================================
  28. #ifdef _MSC_VER
  29. #pragma warning (disable : 4786) // identifier was truncated to 'number' characters
  30. #endif
  31. extern "C" {
  32. #define XMD_H
  33. #undef FAR
  34. #include <setjmp.h>
  35. #include "../LibJPEG/jinclude.h"
  36. #include "../LibJPEG/jpeglib.h"
  37. #include "../LibJPEG/jerror.h"
  38. }
  39. #include "FreeImage.h"
  40. #include "Utilities.h"
  41. #include "../Metadata/FreeImageTag.h"
  42. // ==========================================================
  43. // Plugin Interface
  44. // ==========================================================
  45. static int s_format_id;
  46. // ----------------------------------------------------------
  47. // Constant declarations
  48. // ----------------------------------------------------------
  49. #define INPUT_BUF_SIZE 4096 // choose an efficiently fread'able size
  50. #define OUTPUT_BUF_SIZE 4096 // choose an efficiently fwrite'able size
  51. #define EXIF_MARKER (JPEG_APP0+1) // EXIF marker / Adobe XMP marker
  52. #define ICC_MARKER (JPEG_APP0+2) // ICC profile marker
  53. #define IPTC_MARKER (JPEG_APP0+13) // IPTC marker / BIM marker
  54. #define ICC_HEADER_SIZE 14 // size of non-profile data in APP2
  55. #define MAX_BYTES_IN_MARKER 65533L // maximum data length of a JPEG marker
  56. #define MAX_DATA_BYTES_IN_MARKER 65519L // maximum data length of a JPEG APP2 marker
  57. #define MAX_JFXX_THUMB_SIZE (MAX_BYTES_IN_MARKER - 5 - 1)
  58. #define JFXX_TYPE_JPEG 0x10 // JFIF extension marker: JPEG-compressed thumbnail image
  59. #define JFXX_TYPE_8bit 0x11 // JFIF extension marker: palette thumbnail image
  60. #define JFXX_TYPE_24bit 0x13 // JFIF extension marker: RGB thumbnail image
  61. // ----------------------------------------------------------
  62. // Typedef declarations
  63. // ----------------------------------------------------------
  64. typedef struct tagErrorManager {
  65. /// "public" fields
  66. struct jpeg_error_mgr pub;
  67. /// for return to caller
  68. jmp_buf setjmp_buffer;
  69. } ErrorManager;
  70. typedef struct tagSourceManager {
  71. /// public fields
  72. struct jpeg_source_mgr pub;
  73. /// source stream
  74. fi_handle infile;
  75. FreeImageIO *m_io;
  76. /// start of buffer
  77. JOCTET * buffer;
  78. /// have we gotten any data yet ?
  79. boolean start_of_file;
  80. } SourceManager;
  81. typedef struct tagDestinationManager {
  82. /// public fields
  83. struct jpeg_destination_mgr pub;
  84. /// destination stream
  85. fi_handle outfile;
  86. FreeImageIO *m_io;
  87. /// start of buffer
  88. JOCTET * buffer;
  89. } DestinationManager;
  90. typedef SourceManager* freeimage_src_ptr;
  91. typedef DestinationManager* freeimage_dst_ptr;
  92. typedef ErrorManager* freeimage_error_ptr;
  93. // ----------------------------------------------------------
  94. // Error handling
  95. // ----------------------------------------------------------
  96. /** Fatal errors (print message and exit) */
  97. static inline void
  98. JPEG_EXIT(j_common_ptr cinfo, int code) {
  99. freeimage_error_ptr error_ptr = (freeimage_error_ptr)cinfo->err;
  100. error_ptr->pub.msg_code = code;
  101. error_ptr->pub.error_exit(cinfo);
  102. }
  103. /** Nonfatal errors (we can keep going, but the data is probably corrupt) */
  104. static inline void
  105. JPEG_WARNING(j_common_ptr cinfo, int code) {
  106. freeimage_error_ptr error_ptr = (freeimage_error_ptr)cinfo->err;
  107. error_ptr->pub.msg_code = code;
  108. error_ptr->pub.emit_message(cinfo, -1);
  109. }
  110. /**
  111. Receives control for a fatal error. Information sufficient to
  112. generate the error message has been stored in cinfo->err; call
  113. output_message to display it. Control must NOT return to the caller;
  114. generally this routine will exit() or longjmp() somewhere.
  115. */
  116. METHODDEF(void)
  117. jpeg_error_exit (j_common_ptr cinfo) {
  118. freeimage_error_ptr error_ptr = (freeimage_error_ptr)cinfo->err;
  119. // always display the message
  120. error_ptr->pub.output_message(cinfo);
  121. // allow JPEG with unknown markers
  122. if(error_ptr->pub.msg_code != JERR_UNKNOWN_MARKER) {
  123. // let the memory manager delete any temp files before we die
  124. jpeg_destroy(cinfo);
  125. // return control to the setjmp point
  126. longjmp(error_ptr->setjmp_buffer, 1);
  127. }
  128. }
  129. /**
  130. Actual output of any JPEG message. Note that this method does not know
  131. how to generate a message, only where to send it.
  132. */
  133. METHODDEF(void)
  134. jpeg_output_message (j_common_ptr cinfo) {
  135. char buffer[JMSG_LENGTH_MAX];
  136. freeimage_error_ptr error_ptr = (freeimage_error_ptr)cinfo->err;
  137. // create the message
  138. error_ptr->pub.format_message(cinfo, buffer);
  139. // send it to user's message proc
  140. FreeImage_OutputMessageProc(s_format_id, buffer);
  141. }
  142. // ----------------------------------------------------------
  143. // Destination manager
  144. // ----------------------------------------------------------
  145. /**
  146. Initialize destination. This is called by jpeg_start_compress()
  147. before any data is actually written. It must initialize
  148. next_output_byte and free_in_buffer. free_in_buffer must be
  149. initialized to a positive value.
  150. */
  151. METHODDEF(void)
  152. init_destination (j_compress_ptr cinfo) {
  153. freeimage_dst_ptr dest = (freeimage_dst_ptr) cinfo->dest;
  154. dest->buffer = (JOCTET *)
  155. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  156. OUTPUT_BUF_SIZE * sizeof(JOCTET));
  157. dest->pub.next_output_byte = dest->buffer;
  158. dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
  159. }
  160. /**
  161. This is called whenever the buffer has filled (free_in_buffer
  162. reaches zero). In typical applications, it should write out the
  163. *entire* buffer (use the saved start address and buffer length;
  164. ignore the current state of next_output_byte and free_in_buffer).
  165. Then reset the pointer & count to the start of the buffer, and
  166. return TRUE indicating that the buffer has been dumped.
  167. free_in_buffer must be set to a positive value when TRUE is
  168. returned. A FALSE return should only be used when I/O suspension is
  169. desired.
  170. */
  171. METHODDEF(boolean)
  172. empty_output_buffer (j_compress_ptr cinfo) {
  173. freeimage_dst_ptr dest = (freeimage_dst_ptr) cinfo->dest;
  174. if (dest->m_io->write_proc(dest->buffer, 1, OUTPUT_BUF_SIZE, dest->outfile) != OUTPUT_BUF_SIZE) {
  175. // let the memory manager delete any temp files before we die
  176. jpeg_destroy((j_common_ptr)cinfo);
  177. JPEG_EXIT((j_common_ptr)cinfo, JERR_FILE_WRITE);
  178. }
  179. dest->pub.next_output_byte = dest->buffer;
  180. dest->pub.free_in_buffer = OUTPUT_BUF_SIZE;
  181. return TRUE;
  182. }
  183. /**
  184. Terminate destination --- called by jpeg_finish_compress() after all
  185. data has been written. In most applications, this must flush any
  186. data remaining in the buffer. Use either next_output_byte or
  187. free_in_buffer to determine how much data is in the buffer.
  188. */
  189. METHODDEF(void)
  190. term_destination (j_compress_ptr cinfo) {
  191. freeimage_dst_ptr dest = (freeimage_dst_ptr) cinfo->dest;
  192. size_t datacount = OUTPUT_BUF_SIZE - dest->pub.free_in_buffer;
  193. // write any data remaining in the buffer
  194. if (datacount > 0) {
  195. if (dest->m_io->write_proc(dest->buffer, 1, (unsigned int)datacount, dest->outfile) != datacount) {
  196. // let the memory manager delete any temp files before we die
  197. jpeg_destroy((j_common_ptr)cinfo);
  198. JPEG_EXIT((j_common_ptr)cinfo, JERR_FILE_WRITE);
  199. }
  200. }
  201. }
  202. // ----------------------------------------------------------
  203. // Source manager
  204. // ----------------------------------------------------------
  205. /**
  206. Initialize source. This is called by jpeg_read_header() before any
  207. data is actually read. Unlike init_destination(), it may leave
  208. bytes_in_buffer set to 0 (in which case a fill_input_buffer() call
  209. will occur immediately).
  210. */
  211. METHODDEF(void)
  212. init_source (j_decompress_ptr cinfo) {
  213. freeimage_src_ptr src = (freeimage_src_ptr) cinfo->src;
  214. /* We reset the empty-input-file flag for each image,
  215. * but we don't clear the input buffer.
  216. * This is correct behavior for reading a series of images from one source.
  217. */
  218. src->start_of_file = TRUE;
  219. }
  220. /**
  221. This is called whenever bytes_in_buffer has reached zero and more
  222. data is wanted. In typical applications, it should read fresh data
  223. into the buffer (ignoring the current state of next_input_byte and
  224. bytes_in_buffer), reset the pointer & count to the start of the
  225. buffer, and return TRUE indicating that the buffer has been reloaded.
  226. It is not necessary to fill the buffer entirely, only to obtain at
  227. least one more byte. bytes_in_buffer MUST be set to a positive value
  228. if TRUE is returned. A FALSE return should only be used when I/O
  229. suspension is desired.
  230. */
  231. METHODDEF(boolean)
  232. fill_input_buffer (j_decompress_ptr cinfo) {
  233. freeimage_src_ptr src = (freeimage_src_ptr) cinfo->src;
  234. size_t nbytes = src->m_io->read_proc(src->buffer, 1, INPUT_BUF_SIZE, src->infile);
  235. if (nbytes <= 0) {
  236. if (src->start_of_file) {
  237. // treat empty input file as fatal error
  238. // let the memory manager delete any temp files before we die
  239. jpeg_destroy((j_common_ptr)cinfo);
  240. JPEG_EXIT((j_common_ptr)cinfo, JERR_INPUT_EMPTY);
  241. }
  242. JPEG_WARNING((j_common_ptr)cinfo, JWRN_JPEG_EOF);
  243. /* Insert a fake EOI marker */
  244. src->buffer[0] = (JOCTET) 0xFF;
  245. src->buffer[1] = (JOCTET) JPEG_EOI;
  246. nbytes = 2;
  247. }
  248. src->pub.next_input_byte = src->buffer;
  249. src->pub.bytes_in_buffer = nbytes;
  250. src->start_of_file = FALSE;
  251. return TRUE;
  252. }
  253. /**
  254. Skip num_bytes worth of data. The buffer pointer and count should
  255. be advanced over num_bytes input bytes, refilling the buffer as
  256. needed. This is used to skip over a potentially large amount of
  257. uninteresting data (such as an APPn marker). In some applications
  258. it may be possible to optimize away the reading of the skipped data,
  259. but it's not clear that being smart is worth much trouble; large
  260. skips are uncommon. bytes_in_buffer may be zero on return.
  261. A zero or negative skip count should be treated as a no-op.
  262. */
  263. METHODDEF(void)
  264. skip_input_data (j_decompress_ptr cinfo, long num_bytes) {
  265. freeimage_src_ptr src = (freeimage_src_ptr) cinfo->src;
  266. /* Just a dumb implementation for now. Could use fseek() except
  267. * it doesn't work on pipes. Not clear that being smart is worth
  268. * any trouble anyway --- large skips are infrequent.
  269. */
  270. if (num_bytes > 0) {
  271. while (num_bytes > (long) src->pub.bytes_in_buffer) {
  272. num_bytes -= (long) src->pub.bytes_in_buffer;
  273. (void) fill_input_buffer(cinfo);
  274. /* note we assume that fill_input_buffer will never return FALSE,
  275. * so suspension need not be handled.
  276. */
  277. }
  278. src->pub.next_input_byte += (size_t) num_bytes;
  279. src->pub.bytes_in_buffer -= (size_t) num_bytes;
  280. }
  281. }
  282. /**
  283. Terminate source --- called by jpeg_finish_decompress
  284. after all data has been read. Often a no-op.
  285. NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
  286. application must deal with any cleanup that should happen even
  287. for error exit.
  288. */
  289. METHODDEF(void)
  290. term_source (j_decompress_ptr cinfo) {
  291. // no work necessary here
  292. }
  293. // ----------------------------------------------------------
  294. // Source manager & Destination manager setup
  295. // ----------------------------------------------------------
  296. /**
  297. Prepare for input from a stdio stream.
  298. The caller must have already opened the stream, and is responsible
  299. for closing it after finishing decompression.
  300. */
  301. GLOBAL(void)
  302. jpeg_freeimage_src (j_decompress_ptr cinfo, fi_handle infile, FreeImageIO *io) {
  303. freeimage_src_ptr src;
  304. // allocate memory for the buffer. is released automatically in the end
  305. if (cinfo->src == NULL) {
  306. cinfo->src = (struct jpeg_source_mgr *) (*cinfo->mem->alloc_small)
  307. ((j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof(SourceManager));
  308. src = (freeimage_src_ptr) cinfo->src;
  309. src->buffer = (JOCTET *) (*cinfo->mem->alloc_small)
  310. ((j_common_ptr) cinfo, JPOOL_PERMANENT, INPUT_BUF_SIZE * sizeof(JOCTET));
  311. }
  312. // initialize the jpeg pointer struct with pointers to functions
  313. src = (freeimage_src_ptr) cinfo->src;
  314. src->pub.init_source = init_source;
  315. src->pub.fill_input_buffer = fill_input_buffer;
  316. src->pub.skip_input_data = skip_input_data;
  317. src->pub.resync_to_restart = jpeg_resync_to_restart; // use default method
  318. src->pub.term_source = term_source;
  319. src->infile = infile;
  320. src->m_io = io;
  321. src->pub.bytes_in_buffer = 0; // forces fill_input_buffer on first read
  322. src->pub.next_input_byte = NULL; // until buffer loaded
  323. }
  324. /**
  325. Prepare for output to a stdio stream.
  326. The caller must have already opened the stream, and is responsible
  327. for closing it after finishing compression.
  328. */
  329. GLOBAL(void)
  330. jpeg_freeimage_dst (j_compress_ptr cinfo, fi_handle outfile, FreeImageIO *io) {
  331. freeimage_dst_ptr dest;
  332. if (cinfo->dest == NULL) {
  333. cinfo->dest = (struct jpeg_destination_mgr *)(*cinfo->mem->alloc_small)
  334. ((j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof(DestinationManager));
  335. }
  336. dest = (freeimage_dst_ptr) cinfo->dest;
  337. dest->pub.init_destination = init_destination;
  338. dest->pub.empty_output_buffer = empty_output_buffer;
  339. dest->pub.term_destination = term_destination;
  340. dest->outfile = outfile;
  341. dest->m_io = io;
  342. }
  343. // ----------------------------------------------------------
  344. // Special markers read functions
  345. // ----------------------------------------------------------
  346. /**
  347. Read JPEG_COM marker (comment)
  348. */
  349. static BOOL
  350. jpeg_read_comment(FIBITMAP *dib, const BYTE *dataptr, unsigned int datalen) {
  351. size_t length = datalen;
  352. BYTE *profile = (BYTE*)dataptr;
  353. // read the comment
  354. char *value = (char*)malloc((length + 1) * sizeof(char));
  355. if(value == NULL) return FALSE;
  356. memcpy(value, profile, length);
  357. value[length] = '\0';
  358. // create a tag
  359. FITAG *tag = FreeImage_CreateTag();
  360. if(tag) {
  361. unsigned int count = (unsigned int)length + 1; // includes the null value
  362. FreeImage_SetTagID(tag, JPEG_COM);
  363. FreeImage_SetTagKey(tag, "Comment");
  364. FreeImage_SetTagLength(tag, count);
  365. FreeImage_SetTagCount(tag, count);
  366. FreeImage_SetTagType(tag, FIDT_ASCII);
  367. FreeImage_SetTagValue(tag, value);
  368. // store the tag
  369. FreeImage_SetMetadata(FIMD_COMMENTS, dib, FreeImage_GetTagKey(tag), tag);
  370. // destroy the tag
  371. FreeImage_DeleteTag(tag);
  372. }
  373. free(value);
  374. return TRUE;
  375. }
  376. /**
  377. Read JPEG_APP2 marker (ICC profile)
  378. */
  379. /**
  380. Handy subroutine to test whether a saved marker is an ICC profile marker.
  381. */
  382. static BOOL
  383. marker_is_icc(jpeg_saved_marker_ptr marker) {
  384. // marker identifying string "ICC_PROFILE" (null-terminated)
  385. const BYTE icc_signature[12] = { 0x49, 0x43, 0x43, 0x5F, 0x50, 0x52, 0x4F, 0x46, 0x49, 0x4C, 0x45, 0x00 };
  386. if(marker->marker == ICC_MARKER) {
  387. // verify the identifying string
  388. if(marker->data_length >= ICC_HEADER_SIZE) {
  389. if(memcmp(icc_signature, marker->data, sizeof(icc_signature)) == 0) {
  390. return TRUE;
  391. }
  392. }
  393. }
  394. return FALSE;
  395. }
  396. /**
  397. See if there was an ICC profile in the JPEG file being read;
  398. if so, reassemble and return the profile data.
  399. TRUE is returned if an ICC profile was found, FALSE if not.
  400. If TRUE is returned, *icc_data_ptr is set to point to the
  401. returned data, and *icc_data_len is set to its length.
  402. IMPORTANT: the data at **icc_data_ptr has been allocated with malloc()
  403. and must be freed by the caller with free() when the caller no longer
  404. needs it. (Alternatively, we could write this routine to use the
  405. IJG library's memory allocator, so that the data would be freed implicitly
  406. at jpeg_finish_decompress() time. But it seems likely that many apps
  407. will prefer to have the data stick around after decompression finishes.)
  408. NOTE: if the file contains invalid ICC APP2 markers, we just silently
  409. return FALSE. You might want to issue an error message instead.
  410. */
  411. static BOOL
  412. jpeg_read_icc_profile(j_decompress_ptr cinfo, JOCTET **icc_data_ptr, unsigned *icc_data_len) {
  413. jpeg_saved_marker_ptr marker;
  414. int num_markers = 0;
  415. int seq_no;
  416. JOCTET *icc_data;
  417. unsigned total_length;
  418. const int MAX_SEQ_NO = 255; // sufficient since marker numbers are bytes
  419. BYTE marker_present[MAX_SEQ_NO+1]; // 1 if marker found
  420. unsigned data_length[MAX_SEQ_NO+1]; // size of profile data in marker
  421. unsigned data_offset[MAX_SEQ_NO+1]; // offset for data in marker
  422. *icc_data_ptr = NULL; // avoid confusion if FALSE return
  423. *icc_data_len = 0;
  424. /**
  425. this first pass over the saved markers discovers whether there are
  426. any ICC markers and verifies the consistency of the marker numbering.
  427. */
  428. memset(marker_present, 0, (MAX_SEQ_NO + 1));
  429. for(marker = cinfo->marker_list; marker != NULL; marker = marker->next) {
  430. if (marker_is_icc(marker)) {
  431. if (num_markers == 0) {
  432. // number of markers
  433. num_markers = GETJOCTET(marker->data[13]);
  434. }
  435. else if (num_markers != GETJOCTET(marker->data[13])) {
  436. return FALSE; // inconsistent num_markers fields
  437. }
  438. // sequence number
  439. seq_no = GETJOCTET(marker->data[12]);
  440. if (seq_no <= 0 || seq_no > num_markers) {
  441. return FALSE; // bogus sequence number
  442. }
  443. if (marker_present[seq_no]) {
  444. return FALSE; // duplicate sequence numbers
  445. }
  446. marker_present[seq_no] = 1;
  447. data_length[seq_no] = marker->data_length - ICC_HEADER_SIZE;
  448. }
  449. }
  450. if (num_markers == 0)
  451. return FALSE;
  452. /**
  453. check for missing markers, count total space needed,
  454. compute offset of each marker's part of the data.
  455. */
  456. total_length = 0;
  457. for(seq_no = 1; seq_no <= num_markers; seq_no++) {
  458. if (marker_present[seq_no] == 0) {
  459. return FALSE; // missing sequence number
  460. }
  461. data_offset[seq_no] = total_length;
  462. total_length += data_length[seq_no];
  463. }
  464. if (total_length <= 0)
  465. return FALSE; // found only empty markers ?
  466. // allocate space for assembled data
  467. icc_data = (JOCTET *) malloc(total_length * sizeof(JOCTET));
  468. if (icc_data == NULL)
  469. return FALSE; // out of memory
  470. // and fill it in
  471. for (marker = cinfo->marker_list; marker != NULL; marker = marker->next) {
  472. if (marker_is_icc(marker)) {
  473. JOCTET FAR *src_ptr;
  474. JOCTET *dst_ptr;
  475. unsigned length;
  476. seq_no = GETJOCTET(marker->data[12]);
  477. dst_ptr = icc_data + data_offset[seq_no];
  478. src_ptr = marker->data + ICC_HEADER_SIZE;
  479. length = data_length[seq_no];
  480. while (length--) {
  481. *dst_ptr++ = *src_ptr++;
  482. }
  483. }
  484. }
  485. *icc_data_ptr = icc_data;
  486. *icc_data_len = total_length;
  487. return TRUE;
  488. }
  489. /**
  490. Read JPEG_APPD marker (IPTC or Adobe Photoshop profile)
  491. */
  492. static BOOL
  493. jpeg_read_iptc_profile(FIBITMAP *dib, const BYTE *dataptr, unsigned int datalen) {
  494. return read_iptc_profile(dib, dataptr, datalen);
  495. }
  496. /**
  497. Read JPEG_APP1 marker (XMP profile)
  498. @param dib Input FIBITMAP
  499. @param dataptr Pointer to the APP1 marker
  500. @param datalen APP1 marker length
  501. @return Returns TRUE if successful, FALSE otherwise
  502. */
  503. static BOOL
  504. jpeg_read_xmp_profile(FIBITMAP *dib, const BYTE *dataptr, unsigned int datalen) {
  505. // marker identifying string for XMP (null terminated)
  506. const char *xmp_signature = "http://ns.adobe.com/xap/1.0/";
  507. // XMP signature is 29 bytes long
  508. const size_t xmp_signature_size = strlen(xmp_signature) + 1;
  509. size_t length = datalen;
  510. BYTE *profile = (BYTE*)dataptr;
  511. if(length <= xmp_signature_size) {
  512. // avoid reading corrupted or empty data
  513. return FALSE;
  514. }
  515. // verify the identifying string
  516. if(memcmp(xmp_signature, profile, strlen(xmp_signature)) == 0) {
  517. // XMP profile
  518. profile += xmp_signature_size;
  519. length -= xmp_signature_size;
  520. // create a tag
  521. FITAG *tag = FreeImage_CreateTag();
  522. if(tag) {
  523. FreeImage_SetTagID(tag, JPEG_APP0+1); // 0xFFE1
  524. FreeImage_SetTagKey(tag, g_TagLib_XMPFieldName);
  525. FreeImage_SetTagLength(tag, (DWORD)length);
  526. FreeImage_SetTagCount(tag, (DWORD)length);
  527. FreeImage_SetTagType(tag, FIDT_ASCII);
  528. FreeImage_SetTagValue(tag, profile);
  529. // store the tag
  530. FreeImage_SetMetadata(FIMD_XMP, dib, FreeImage_GetTagKey(tag), tag);
  531. // destroy the tag
  532. FreeImage_DeleteTag(tag);
  533. }
  534. return TRUE;
  535. }
  536. return FALSE;
  537. }
  538. /**
  539. Read JPEG_APP1 marker (Exif profile)
  540. @param dib Input FIBITMAP
  541. @param dataptr Pointer to the APP1 marker
  542. @param datalen APP1 marker length
  543. @return Returns TRUE if successful, FALSE otherwise
  544. */
  545. static BOOL
  546. jpeg_read_exif_profile_raw(FIBITMAP *dib, const BYTE *profile, unsigned int length) {
  547. // marker identifying string for Exif = "Exif\0\0"
  548. BYTE exif_signature[6] = { 0x45, 0x78, 0x69, 0x66, 0x00, 0x00 };
  549. // verify the identifying string
  550. if(memcmp(exif_signature, profile, sizeof(exif_signature)) != 0) {
  551. // not an Exif profile
  552. return FALSE;
  553. }
  554. // create a tag
  555. FITAG *tag = FreeImage_CreateTag();
  556. if(tag) {
  557. FreeImage_SetTagID(tag, EXIF_MARKER); // (JPEG_APP0 + 1) => EXIF marker / Adobe XMP marker
  558. FreeImage_SetTagKey(tag, g_TagLib_ExifRawFieldName);
  559. FreeImage_SetTagLength(tag, (DWORD)length);
  560. FreeImage_SetTagCount(tag, (DWORD)length);
  561. FreeImage_SetTagType(tag, FIDT_BYTE);
  562. FreeImage_SetTagValue(tag, profile);
  563. // store the tag
  564. FreeImage_SetMetadata(FIMD_EXIF_RAW, dib, FreeImage_GetTagKey(tag), tag);
  565. // destroy the tag
  566. FreeImage_DeleteTag(tag);
  567. return TRUE;
  568. }
  569. return FALSE;
  570. }
  571. /**
  572. Read JFIF "JFXX" extension APP0 marker
  573. @param dib Input FIBITMAP
  574. @param dataptr Pointer to the APP0 marker
  575. @param datalen APP0 marker length
  576. @return Returns TRUE if successful, FALSE otherwise
  577. */
  578. static BOOL
  579. jpeg_read_jfxx(FIBITMAP *dib, const BYTE *dataptr, unsigned int datalen) {
  580. if(datalen < 6) {
  581. return FALSE;
  582. }
  583. const int id_length = 5;
  584. const BYTE *data = dataptr + id_length;
  585. unsigned remaining = datalen - id_length;
  586. const BYTE type = *data;
  587. ++data, --remaining;
  588. switch(type) {
  589. case JFXX_TYPE_JPEG:
  590. {
  591. // load the thumbnail
  592. FIMEMORY* hmem = FreeImage_OpenMemory(const_cast<BYTE*>(data), remaining);
  593. FIBITMAP* thumbnail = FreeImage_LoadFromMemory(FIF_JPEG, hmem);
  594. FreeImage_CloseMemory(hmem);
  595. // store the thumbnail
  596. FreeImage_SetThumbnail(dib, thumbnail);
  597. // then delete it
  598. FreeImage_Unload(thumbnail);
  599. break;
  600. }
  601. case JFXX_TYPE_8bit:
  602. // colormapped uncompressed thumbnail (no supported)
  603. break;
  604. case JFXX_TYPE_24bit:
  605. // truecolor uncompressed thumbnail (no supported)
  606. break;
  607. default:
  608. break;
  609. }
  610. return TRUE;
  611. }
  612. /**
  613. Read JPEG special markers
  614. */
  615. static BOOL
  616. read_markers(j_decompress_ptr cinfo, FIBITMAP *dib) {
  617. jpeg_saved_marker_ptr marker;
  618. for(marker = cinfo->marker_list; marker != NULL; marker = marker->next) {
  619. switch(marker->marker) {
  620. case JPEG_APP0:
  621. // JFIF is handled by libjpeg already, handle JFXX
  622. if(memcmp(marker->data, "JFIF" , 5) == 0) {
  623. continue;
  624. }
  625. if(memcmp(marker->data, "JFXX" , 5) == 0) {
  626. if(!cinfo->saw_JFIF_marker || cinfo->JFIF_minor_version < 2) {
  627. FreeImage_OutputMessageProc(s_format_id, "Warning: non-standard JFXX segment");
  628. }
  629. jpeg_read_jfxx(dib, marker->data, marker->data_length);
  630. }
  631. // other values such as 'Picasa' : ignore safely unknown APP0 marker
  632. break;
  633. case JPEG_COM:
  634. // JPEG comment
  635. jpeg_read_comment(dib, marker->data, marker->data_length);
  636. break;
  637. case EXIF_MARKER:
  638. // Exif or Adobe XMP profile
  639. jpeg_read_exif_profile(dib, marker->data, marker->data_length);
  640. jpeg_read_xmp_profile(dib, marker->data, marker->data_length);
  641. jpeg_read_exif_profile_raw(dib, marker->data, marker->data_length);
  642. break;
  643. case IPTC_MARKER:
  644. // IPTC/NAA or Adobe Photoshop profile
  645. jpeg_read_iptc_profile(dib, marker->data, marker->data_length);
  646. break;
  647. }
  648. }
  649. // ICC profile
  650. BYTE *icc_profile = NULL;
  651. unsigned icc_length = 0;
  652. if( jpeg_read_icc_profile(cinfo, &icc_profile, &icc_length) ) {
  653. // copy ICC profile data
  654. FreeImage_CreateICCProfile(dib, icc_profile, icc_length);
  655. // clean up
  656. free(icc_profile);
  657. }
  658. return TRUE;
  659. }
  660. // ----------------------------------------------------------
  661. // Special markers write functions
  662. // ----------------------------------------------------------
  663. /**
  664. Write JPEG_COM marker (comment)
  665. */
  666. static BOOL
  667. jpeg_write_comment(j_compress_ptr cinfo, FIBITMAP *dib) {
  668. FITAG *tag = NULL;
  669. // write user comment as a JPEG_COM marker
  670. FreeImage_GetMetadata(FIMD_COMMENTS, dib, "Comment", &tag);
  671. if(tag) {
  672. const char *tag_value = (char*)FreeImage_GetTagValue(tag);
  673. if(NULL != tag_value) {
  674. for(long i = 0; i < (long)strlen(tag_value); i+= MAX_BYTES_IN_MARKER) {
  675. jpeg_write_marker(cinfo, JPEG_COM, (BYTE*)tag_value + i, MIN((long)strlen(tag_value + i), MAX_BYTES_IN_MARKER));
  676. }
  677. return TRUE;
  678. }
  679. }
  680. return FALSE;
  681. }
  682. /**
  683. Write JPEG_APP2 marker (ICC profile)
  684. */
  685. static BOOL
  686. jpeg_write_icc_profile(j_compress_ptr cinfo, FIBITMAP *dib) {
  687. // marker identifying string "ICC_PROFILE" (null-terminated)
  688. BYTE icc_signature[12] = { 0x49, 0x43, 0x43, 0x5F, 0x50, 0x52, 0x4F, 0x46, 0x49, 0x4C, 0x45, 0x00 };
  689. FIICCPROFILE *iccProfile = FreeImage_GetICCProfile(dib);
  690. if (iccProfile->size && iccProfile->data) {
  691. // ICC_HEADER_SIZE: ICC signature is 'ICC_PROFILE' + 2 bytes
  692. BYTE *profile = (BYTE*)malloc((iccProfile->size + ICC_HEADER_SIZE) * sizeof(BYTE));
  693. if(profile == NULL) return FALSE;
  694. memcpy(profile, icc_signature, 12);
  695. for(long i = 0; i < (long)iccProfile->size; i += MAX_DATA_BYTES_IN_MARKER) {
  696. unsigned length = MIN((long)(iccProfile->size - i), MAX_DATA_BYTES_IN_MARKER);
  697. // sequence number
  698. profile[12] = (BYTE) ((i / MAX_DATA_BYTES_IN_MARKER) + 1);
  699. // number of markers
  700. profile[13] = (BYTE) (iccProfile->size / MAX_DATA_BYTES_IN_MARKER + 1);
  701. memcpy(profile + ICC_HEADER_SIZE, (BYTE*)iccProfile->data + i, length);
  702. jpeg_write_marker(cinfo, ICC_MARKER, profile, (length + ICC_HEADER_SIZE));
  703. }
  704. free(profile);
  705. return TRUE;
  706. }
  707. return FALSE;
  708. }
  709. /**
  710. Write JPEG_APPD marker (IPTC or Adobe Photoshop profile)
  711. @return Returns TRUE if successful, FALSE otherwise
  712. */
  713. static BOOL
  714. jpeg_write_iptc_profile(j_compress_ptr cinfo, FIBITMAP *dib) {
  715. //const char *ps_header = "Photoshop 3.0\x08BIM\x04\x04\x0\x0\x0\x0";
  716. const unsigned tag_length = 26;
  717. if(FreeImage_GetMetadataCount(FIMD_IPTC, dib)) {
  718. BYTE *profile = NULL;
  719. unsigned profile_size = 0;
  720. // create a binary profile
  721. if(write_iptc_profile(dib, &profile, &profile_size)) {
  722. // write the profile
  723. for(long i = 0; i < (long)profile_size; i += 65517L) {
  724. unsigned length = MIN((long)profile_size - i, 65517L);
  725. unsigned roundup = length & 0x01; // needed for Photoshop
  726. BYTE *iptc_profile = (BYTE*)malloc(length + roundup + tag_length);
  727. if(iptc_profile == NULL) break;
  728. // Photoshop identification string
  729. memcpy(&iptc_profile[0], "Photoshop 3.0\x0", 14);
  730. // 8BIM segment type
  731. memcpy(&iptc_profile[14], "8BIM\x04\x04\x0\x0\x0\x0", 10);
  732. // segment size
  733. iptc_profile[24] = (BYTE)(length >> 8);
  734. iptc_profile[25] = (BYTE)(length & 0xFF);
  735. // segment data
  736. memcpy(&iptc_profile[tag_length], &profile[i], length);
  737. if(roundup)
  738. iptc_profile[length + tag_length] = 0;
  739. jpeg_write_marker(cinfo, IPTC_MARKER, iptc_profile, length + roundup + tag_length);
  740. free(iptc_profile);
  741. }
  742. // release profile
  743. free(profile);
  744. return TRUE;
  745. }
  746. }
  747. return FALSE;
  748. }
  749. /**
  750. Write JPEG_APP1 marker (XMP profile)
  751. @return Returns TRUE if successful, FALSE otherwise
  752. */
  753. static BOOL
  754. jpeg_write_xmp_profile(j_compress_ptr cinfo, FIBITMAP *dib) {
  755. // marker identifying string for XMP (null terminated)
  756. const char *xmp_signature = "http://ns.adobe.com/xap/1.0/";
  757. FITAG *tag_xmp = NULL;
  758. FreeImage_GetMetadata(FIMD_XMP, dib, g_TagLib_XMPFieldName, &tag_xmp);
  759. if(tag_xmp) {
  760. const BYTE *tag_value = (BYTE*)FreeImage_GetTagValue(tag_xmp);
  761. if(NULL != tag_value) {
  762. // XMP signature is 29 bytes long
  763. unsigned int xmp_header_size = (unsigned int)strlen(xmp_signature) + 1;
  764. DWORD tag_length = FreeImage_GetTagLength(tag_xmp);
  765. BYTE *profile = (BYTE*)malloc((tag_length + xmp_header_size) * sizeof(BYTE));
  766. if(profile == NULL) return FALSE;
  767. memcpy(profile, xmp_signature, xmp_header_size);
  768. for(DWORD i = 0; i < tag_length; i += 65504L) {
  769. unsigned length = MIN((long)(tag_length - i), 65504L);
  770. memcpy(profile + xmp_header_size, tag_value + i, length);
  771. jpeg_write_marker(cinfo, EXIF_MARKER, profile, (length + xmp_header_size));
  772. }
  773. free(profile);
  774. return TRUE;
  775. }
  776. }
  777. return FALSE;
  778. }
  779. /**
  780. Write JPEG_APP1 marker (Exif profile)
  781. @return Returns TRUE if successful, FALSE otherwise
  782. */
  783. static BOOL
  784. jpeg_write_exif_profile_raw(j_compress_ptr cinfo, FIBITMAP *dib) {
  785. // marker identifying string for Exif = "Exif\0\0"
  786. BYTE exif_signature[6] = { 0x45, 0x78, 0x69, 0x66, 0x00, 0x00 };
  787. FITAG *tag_exif = NULL;
  788. FreeImage_GetMetadata(FIMD_EXIF_RAW, dib, g_TagLib_ExifRawFieldName, &tag_exif);
  789. if(tag_exif) {
  790. const BYTE *tag_value = (BYTE*)FreeImage_GetTagValue(tag_exif);
  791. // verify the identifying string
  792. if(memcmp(exif_signature, tag_value, sizeof(exif_signature)) != 0) {
  793. // not an Exif profile
  794. return FALSE;
  795. }
  796. if(NULL != tag_value) {
  797. DWORD tag_length = FreeImage_GetTagLength(tag_exif);
  798. BYTE *profile = (BYTE*)malloc(tag_length * sizeof(BYTE));
  799. if(profile == NULL) return FALSE;
  800. for(DWORD i = 0; i < tag_length; i += 65504L) {
  801. unsigned length = MIN((long)(tag_length - i), 65504L);
  802. memcpy(profile, tag_value + i, length);
  803. jpeg_write_marker(cinfo, EXIF_MARKER, profile, length);
  804. }
  805. free(profile);
  806. return TRUE;
  807. }
  808. }
  809. return FALSE;
  810. }
  811. /**
  812. Write thumbnail (JFXX segment, JPEG compressed)
  813. */
  814. static BOOL
  815. jpeg_write_jfxx(j_compress_ptr cinfo, FIBITMAP *dib) {
  816. // get the thumbnail to be stored
  817. FIBITMAP* thumbnail = FreeImage_GetThumbnail(dib);
  818. if(!thumbnail) {
  819. return TRUE;
  820. }
  821. // check for a compatible output format
  822. if((FreeImage_GetImageType(thumbnail) != FIT_BITMAP) || (FreeImage_GetBPP(thumbnail) != 8) && (FreeImage_GetBPP(thumbnail) != 24)) {
  823. FreeImage_OutputMessageProc(s_format_id, FI_MSG_WARNING_INVALID_THUMBNAIL);
  824. return FALSE;
  825. }
  826. // stores the thumbnail as a baseline JPEG into a memory block
  827. // return the memory block only if its size is within JFXX marker size limit!
  828. FIMEMORY *stream = FreeImage_OpenMemory();
  829. if(FreeImage_SaveToMemory(FIF_JPEG, thumbnail, stream, JPEG_BASELINE)) {
  830. // check that the memory block size is within JFXX marker size limit
  831. FreeImage_SeekMemory(stream, 0, SEEK_END);
  832. const long eof = FreeImage_TellMemory(stream);
  833. if(eof > MAX_JFXX_THUMB_SIZE) {
  834. FreeImage_OutputMessageProc(s_format_id, "Warning: attached thumbnail is %d bytes larger than maximum supported size - Thumbnail saving aborted", eof - MAX_JFXX_THUMB_SIZE);
  835. FreeImage_CloseMemory(stream);
  836. return FALSE;
  837. }
  838. } else {
  839. FreeImage_CloseMemory(stream);
  840. return FALSE;
  841. }
  842. BYTE* thData = NULL;
  843. DWORD thSize = 0;
  844. FreeImage_AcquireMemory(stream, &thData, &thSize);
  845. BYTE id_length = 5; //< "JFXX"
  846. BYTE type = JFXX_TYPE_JPEG;
  847. DWORD totalsize = id_length + sizeof(type) + thSize;
  848. jpeg_write_m_header(cinfo, JPEG_APP0, totalsize);
  849. jpeg_write_m_byte(cinfo, 'J');
  850. jpeg_write_m_byte(cinfo, 'F');
  851. jpeg_write_m_byte(cinfo, 'X');
  852. jpeg_write_m_byte(cinfo, 'X');
  853. jpeg_write_m_byte(cinfo, '\0');
  854. jpeg_write_m_byte(cinfo, type);
  855. // write thumbnail to destination.
  856. // We "cram it straight into the data destination module", because write_m_byte is slow
  857. freeimage_dst_ptr dest = (freeimage_dst_ptr) cinfo->dest;
  858. BYTE* & out = dest->pub.next_output_byte;
  859. size_t & bufRemain = dest->pub.free_in_buffer;
  860. const BYTE *thData_end = thData + thSize;
  861. while(thData < thData_end) {
  862. *(out)++ = *(thData)++;
  863. if (--bufRemain == 0) {
  864. // buffer full - flush
  865. if (!dest->pub.empty_output_buffer(cinfo)) {
  866. break;
  867. }
  868. }
  869. }
  870. FreeImage_CloseMemory(stream);
  871. return TRUE;
  872. }
  873. /**
  874. Write JPEG special markers
  875. */
  876. static BOOL
  877. write_markers(j_compress_ptr cinfo, FIBITMAP *dib) {
  878. // write thumbnail as a JFXX marker
  879. jpeg_write_jfxx(cinfo, dib);
  880. // write user comment as a JPEG_COM marker
  881. jpeg_write_comment(cinfo, dib);
  882. // write ICC profile
  883. jpeg_write_icc_profile(cinfo, dib);
  884. // write IPTC profile
  885. jpeg_write_iptc_profile(cinfo, dib);
  886. // write Adobe XMP profile
  887. jpeg_write_xmp_profile(cinfo, dib);
  888. // write Exif raw data
  889. jpeg_write_exif_profile_raw(cinfo, dib);
  890. return TRUE;
  891. }
  892. // ------------------------------------------------------------
  893. // Keep original size info when using scale option on loading
  894. // ------------------------------------------------------------
  895. static void
  896. store_size_info(FIBITMAP *dib, JDIMENSION width, JDIMENSION height) {
  897. char buffer[256];
  898. // create a tag
  899. FITAG *tag = FreeImage_CreateTag();
  900. if(tag) {
  901. size_t length = 0;
  902. // set the original width
  903. sprintf(buffer, "%d", (int)width);
  904. length = strlen(buffer) + 1; // include the NULL/0 value
  905. FreeImage_SetTagKey(tag, "OriginalJPEGWidth");
  906. FreeImage_SetTagLength(tag, (DWORD)length);
  907. FreeImage_SetTagCount(tag, (DWORD)length);
  908. FreeImage_SetTagType(tag, FIDT_ASCII);
  909. FreeImage_SetTagValue(tag, buffer);
  910. FreeImage_SetMetadata(FIMD_COMMENTS, dib, FreeImage_GetTagKey(tag), tag);
  911. // set the original height
  912. sprintf(buffer, "%d", (int)height);
  913. length = strlen(buffer) + 1; // include the NULL/0 value
  914. FreeImage_SetTagKey(tag, "OriginalJPEGHeight");
  915. FreeImage_SetTagLength(tag, (DWORD)length);
  916. FreeImage_SetTagCount(tag, (DWORD)length);
  917. FreeImage_SetTagType(tag, FIDT_ASCII);
  918. FreeImage_SetTagValue(tag, buffer);
  919. FreeImage_SetMetadata(FIMD_COMMENTS, dib, FreeImage_GetTagKey(tag), tag);
  920. // destroy the tag
  921. FreeImage_DeleteTag(tag);
  922. }
  923. }
  924. // ------------------------------------------------------------
  925. // Rotate a dib according to Exif info
  926. // ------------------------------------------------------------
  927. static void
  928. rotate_exif(FIBITMAP **dib) {
  929. // check for Exif rotation
  930. if(FreeImage_GetMetadataCount(FIMD_EXIF_MAIN, *dib)) {
  931. FIBITMAP *rotated = NULL;
  932. // process Exif rotation
  933. FITAG *tag = NULL;
  934. FreeImage_GetMetadata(FIMD_EXIF_MAIN, *dib, "Orientation", &tag);
  935. if(tag != NULL) {
  936. if(FreeImage_GetTagID(tag) == TAG_ORIENTATION) {
  937. unsigned short orientation = *((unsigned short *)FreeImage_GetTagValue(tag));
  938. switch (orientation) {
  939. case 1: // "top, left side" => 0°
  940. break;
  941. case 2: // "top, right side" => flip left-right
  942. FreeImage_FlipHorizontal(*dib);
  943. break;
  944. case 3: // "bottom, right side"; => -180°
  945. rotated = FreeImage_Rotate(*dib, 180);
  946. FreeImage_Unload(*dib);
  947. *dib = rotated;
  948. break;
  949. case 4: // "bottom, left side" => flip up-down
  950. FreeImage_FlipVertical(*dib);
  951. break;
  952. case 5: // "left side, top" => +90° + flip up-down
  953. rotated = FreeImage_Rotate(*dib, 90);
  954. FreeImage_Unload(*dib);
  955. *dib = rotated;
  956. FreeImage_FlipVertical(*dib);
  957. break;
  958. case 6: // "right side, top" => -90°
  959. rotated = FreeImage_Rotate(*dib, -90);
  960. FreeImage_Unload(*dib);
  961. *dib = rotated;
  962. break;
  963. case 7: // "right side, bottom" => -90° + flip up-down
  964. rotated = FreeImage_Rotate(*dib, -90);
  965. FreeImage_Unload(*dib);
  966. *dib = rotated;
  967. FreeImage_FlipVertical(*dib);
  968. break;
  969. case 8: // "left side, bottom" => +90°
  970. rotated = FreeImage_Rotate(*dib, 90);
  971. FreeImage_Unload(*dib);
  972. *dib = rotated;
  973. break;
  974. default:
  975. break;
  976. }
  977. }
  978. }
  979. }
  980. }
  981. // ==========================================================
  982. // Plugin Implementation
  983. // ==========================================================
  984. static const char * DLL_CALLCONV
  985. Format() {
  986. return "JPEG";
  987. }
  988. static const char * DLL_CALLCONV
  989. Description() {
  990. return "JPEG - JFIF Compliant";
  991. }
  992. static const char * DLL_CALLCONV
  993. Extension() {
  994. return "jpg,jif,jpeg,jpe";
  995. }
  996. static const char * DLL_CALLCONV
  997. RegExpr() {
  998. return "^\377\330\377";
  999. }
  1000. static const char * DLL_CALLCONV
  1001. MimeType() {
  1002. return "image/jpeg";
  1003. }
  1004. static BOOL DLL_CALLCONV
  1005. Validate(FreeImageIO *io, fi_handle handle) {
  1006. BYTE jpeg_signature[] = { 0xFF, 0xD8 };
  1007. BYTE signature[2] = { 0, 0 };
  1008. io->read_proc(signature, 1, sizeof(jpeg_signature), handle);
  1009. return (memcmp(jpeg_signature, signature, sizeof(jpeg_signature)) == 0);
  1010. }
  1011. static BOOL DLL_CALLCONV
  1012. SupportsExportDepth(int depth) {
  1013. return (
  1014. (depth == 8) ||
  1015. (depth == 24)
  1016. );
  1017. }
  1018. static BOOL DLL_CALLCONV
  1019. SupportsExportType(FREE_IMAGE_TYPE type) {
  1020. return (type == FIT_BITMAP) ? TRUE : FALSE;
  1021. }
  1022. static BOOL DLL_CALLCONV
  1023. SupportsICCProfiles() {
  1024. return TRUE;
  1025. }
  1026. static BOOL DLL_CALLCONV
  1027. SupportsNoPixels() {
  1028. return TRUE;
  1029. }
  1030. // ----------------------------------------------------------
  1031. static FIBITMAP * DLL_CALLCONV
  1032. Load(FreeImageIO *io, fi_handle handle, int page, int flags, void *data) {
  1033. if (handle) {
  1034. FIBITMAP *dib = NULL;
  1035. BOOL header_only = (flags & FIF_LOAD_NOPIXELS) == FIF_LOAD_NOPIXELS;
  1036. // set up the jpeglib structures
  1037. struct jpeg_decompress_struct cinfo;
  1038. ErrorManager fi_error_mgr;
  1039. try {
  1040. // step 1: allocate and initialize JPEG decompression object
  1041. // we set up the normal JPEG error routines, then override error_exit & output_message
  1042. cinfo.err = jpeg_std_error(&fi_error_mgr.pub);
  1043. fi_error_mgr.pub.error_exit = jpeg_error_exit;
  1044. fi_error_mgr.pub.output_message = jpeg_output_message;
  1045. // establish the setjmp return context for jpeg_error_exit to use
  1046. if (setjmp(fi_error_mgr.setjmp_buffer)) {
  1047. // If we get here, the JPEG code has signaled an error.
  1048. // We need to clean up the JPEG object, close the input file, and return.
  1049. jpeg_destroy_decompress(&cinfo);
  1050. throw (const char*)NULL;
  1051. }
  1052. jpeg_create_decompress(&cinfo);
  1053. // step 2a: specify data source (eg, a handle)
  1054. jpeg_freeimage_src(&cinfo, handle, io);
  1055. // step 2b: save special markers for later reading
  1056. jpeg_save_markers(&cinfo, JPEG_COM, 0xFFFF);
  1057. for(int m = 0; m < 16; m++) {
  1058. jpeg_save_markers(&cinfo, JPEG_APP0 + m, 0xFFFF);
  1059. }
  1060. // step 3: read handle parameters with jpeg_read_header()
  1061. jpeg_read_header(&cinfo, TRUE);
  1062. // step 4: set parameters for decompression
  1063. unsigned int scale_denom = 1; // fraction by which to scale image
  1064. int requested_size = flags >> 16; // requested user size in pixels
  1065. if(requested_size > 0) {
  1066. // the JPEG codec can perform x2, x4 or x8 scaling on loading
  1067. // try to find the more appropriate scaling according to user's need
  1068. double scale = MAX((double)cinfo.image_width, (double)cinfo.image_height) / (double)requested_size;
  1069. if(scale >= 8) {
  1070. scale_denom = 8;
  1071. } else if(scale >= 4) {
  1072. scale_denom = 4;
  1073. } else if(scale >= 2) {
  1074. scale_denom = 2;
  1075. }
  1076. }
  1077. cinfo.scale_num = 1;
  1078. cinfo.scale_denom = scale_denom;
  1079. if ((flags & JPEG_ACCURATE) != JPEG_ACCURATE) {
  1080. cinfo.dct_method = JDCT_IFAST;
  1081. cinfo.do_fancy_upsampling = FALSE;
  1082. }
  1083. // step 5a: start decompressor and calculate output width and height
  1084. jpeg_start_decompress(&cinfo);
  1085. // step 5b: allocate dib and init header
  1086. if((cinfo.num_components == 4) && (cinfo.out_color_space == JCS_CMYK)) {
  1087. // CMYK image
  1088. if((flags & JPEG_CMYK) == JPEG_CMYK) {
  1089. // load as CMYK
  1090. dib = FreeImage_AllocateHeader(header_only, cinfo.output_width, cinfo.output_height, 32, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK);
  1091. if(!dib) throw FI_MSG_ERROR_DIB_MEMORY;
  1092. FreeImage_GetICCProfile(dib)->flags |= FIICC_COLOR_IS_CMYK;
  1093. } else {
  1094. // load as CMYK and convert to RGB
  1095. dib = FreeImage_AllocateHeader(header_only, cinfo.output_width, cinfo.output_height, 24, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK);
  1096. if(!dib) throw FI_MSG_ERROR_DIB_MEMORY;
  1097. }
  1098. } else {
  1099. // RGB or greyscale image
  1100. dib = FreeImage_AllocateHeader(header_only, cinfo.output_width, cinfo.output_height, 8 * cinfo.num_components, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK);
  1101. if(!dib) throw FI_MSG_ERROR_DIB_MEMORY;
  1102. if (cinfo.num_components == 1) {
  1103. // build a greyscale palette
  1104. RGBQUAD *colors = FreeImage_GetPalette(dib);
  1105. for (int i = 0; i < 256; i++) {
  1106. colors[i].rgbRed = (BYTE)i;
  1107. colors[i].rgbGreen = (BYTE)i;
  1108. colors[i].rgbBlue = (BYTE)i;
  1109. }
  1110. }
  1111. }
  1112. if(scale_denom != 1) {
  1113. // store original size info if a scaling was requested
  1114. store_size_info(dib, cinfo.image_width, cinfo.image_height);
  1115. }
  1116. // step 5c: handle metrices
  1117. if (cinfo.density_unit == 1) {
  1118. // dots/inch
  1119. FreeImage_SetDotsPerMeterX(dib, (unsigned) (((float)cinfo.X_density) / 0.0254000 + 0.5));
  1120. FreeImage_SetDotsPerMeterY(dib, (unsigned) (((float)cinfo.Y_density) / 0.0254000 + 0.5));
  1121. } else if (cinfo.density_unit == 2) {
  1122. // dots/cm
  1123. FreeImage_SetDotsPerMeterX(dib, (unsigned) (cinfo.X_density * 100));
  1124. FreeImage_SetDotsPerMeterY(dib, (unsigned) (cinfo.Y_density * 100));
  1125. }
  1126. // step 6: read special markers
  1127. read_markers(&cinfo, dib);
  1128. // --- header only mode => clean-up and return
  1129. if (header_only) {
  1130. // release JPEG decompression object
  1131. jpeg_destroy_decompress(&cinfo);
  1132. // return header data
  1133. return dib;
  1134. }
  1135. // step 7a: while (scan lines remain to be read) jpeg_read_scanlines(...);
  1136. if((cinfo.out_color_space == JCS_CMYK) && ((flags & JPEG_CMYK) != JPEG_CMYK)) {
  1137. // convert from CMYK to RGB
  1138. JSAMPARRAY buffer; // output row buffer
  1139. unsigned row_stride; // physical row width in output buffer
  1140. // JSAMPLEs per row in output buffer
  1141. row_stride = cinfo.output_width * cinfo.output_components;
  1142. // make a one-row-high sample array that will go away when done with image
  1143. buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
  1144. while (cinfo.output_scanline < cinfo.output_height) {
  1145. JSAMPROW src = buffer[0];
  1146. JSAMPROW dst = FreeImage_GetScanLine(dib, cinfo.output_height - cinfo.output_scanline - 1);
  1147. jpeg_read_scanlines(&cinfo, buffer, 1);
  1148. for(unsigned x = 0; x < cinfo.output_width; x++) {
  1149. WORD K = (WORD)src[3];
  1150. dst[FI_RGBA_RED] = (BYTE)((K * src[0]) / 255); // C -> R
  1151. dst[FI_RGBA_GREEN] = (BYTE)((K * src[1]) / 255); // M -> G
  1152. dst[FI_RGBA_BLUE] = (BYTE)((K * src[2]) / 255); // Y -> B
  1153. src += 4;
  1154. dst += 3;
  1155. }
  1156. }
  1157. } else if((cinfo.out_color_space == JCS_CMYK) && ((flags & JPEG_CMYK) == JPEG_CMYK)) {
  1158. // convert from LibJPEG CMYK to standard CMYK
  1159. JSAMPARRAY buffer; // output row buffer
  1160. unsigned row_stride; // physical row width in output buffer
  1161. // JSAMPLEs per row in output buffer
  1162. row_stride = cinfo.output_width * cinfo.output_components;
  1163. // make a one-row-high sample array that will go away when done with image
  1164. buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
  1165. while (cinfo.output_scanline < cinfo.output_height) {
  1166. JSAMPROW src = buffer[0];
  1167. JSAMPROW dst = FreeImage_GetScanLine(dib, cinfo.output_height - cinfo.output_scanline - 1);
  1168. jpeg_read_scanlines(&cinfo, buffer, 1);
  1169. for(unsigned x = 0; x < cinfo.output_width; x++) {
  1170. // CMYK pixels are inverted
  1171. dst[0] = ~src[0]; // C
  1172. dst[1] = ~src[1]; // M
  1173. dst[2] = ~src[2]; // Y
  1174. dst[3] = ~src[3]; // K
  1175. src += 4;
  1176. dst += 4;
  1177. }
  1178. }
  1179. } else {
  1180. // normal case (RGB or greyscale image)
  1181. while (cinfo.output_scanline < cinfo.output_height) {
  1182. JSAMPROW dst = FreeImage_GetScanLine(dib, cinfo.output_height - cinfo.output_scanline - 1);
  1183. jpeg_read_scanlines(&cinfo, &dst, 1);
  1184. }
  1185. // step 7b: swap red and blue components (see LibJPEG/jmorecfg.h: #define RGB_RED, ...)
  1186. // The default behavior of the JPEG library is kept "as is" because LibTIFF uses
  1187. // LibJPEG "as is".
  1188. #if FREEIMAGE_COLORORDER == FREEIMAGE_COLORORDER_BGR
  1189. SwapRedBlue32(dib);
  1190. #endif
  1191. }
  1192. // step 8: finish decompression
  1193. jpeg_finish_decompress(&cinfo);
  1194. // step 9: release JPEG decompression object
  1195. jpeg_destroy_decompress(&cinfo);
  1196. // check for automatic Exif rotation
  1197. if(!header_only && ((flags & JPEG_EXIFROTATE) == JPEG_EXIFROTATE)) {
  1198. rotate_exif(&dib);
  1199. }
  1200. // everything went well. return the loaded dib
  1201. return dib;
  1202. } catch (const char *text) {
  1203. jpeg_destroy_decompress(&cinfo);
  1204. if(NULL != dib) {
  1205. FreeImage_Unload(dib);
  1206. }
  1207. if(NULL != text) {
  1208. FreeImage_OutputMessageProc(s_format_id, text);
  1209. }
  1210. }
  1211. }
  1212. return NULL;
  1213. }
  1214. // ----------------------------------------------------------
  1215. static BOOL DLL_CALLCONV
  1216. Save(FreeImageIO *io, FIBITMAP *dib, fi_handle handle, int page, int flags, void *data) {
  1217. if ((dib) && (handle)) {
  1218. try {
  1219. // Check dib format
  1220. const char *sError = "only 24-bit highcolor or 8-bit greyscale/palette bitmaps can be saved as JPEG";
  1221. FREE_IMAGE_COLOR_TYPE color_type = FreeImage_GetColorType(dib);
  1222. WORD bpp = (WORD)FreeImage_GetBPP(dib);
  1223. if ((bpp != 24) && (bpp != 8)) {
  1224. throw sError;
  1225. }
  1226. if(bpp == 8) {
  1227. // allow grey, reverse grey and palette
  1228. if ((color_type != FIC_MINISBLACK) && (color_type != FIC_MINISWHITE) && (color_type != FIC_PALETTE)) {
  1229. throw sError;
  1230. }
  1231. }
  1232. struct jpeg_compress_struct cinfo;
  1233. ErrorManager fi_error_mgr;
  1234. // Step 1: allocate and initialize JPEG compression object
  1235. // we set up the normal JPEG error routines, then override error_exit & output_message
  1236. cinfo.err = jpeg_std_error(&fi_error_mgr.pub);
  1237. fi_error_mgr.pub.error_exit = jpeg_error_exit;
  1238. fi_error_mgr.pub.output_message = jpeg_output_message;
  1239. // establish the setjmp return context for jpeg_error_exit to use
  1240. if (setjmp(fi_error_mgr.setjmp_buffer)) {
  1241. // If we get here, the JPEG code has signaled an error.
  1242. // We need to clean up the JPEG object, close the input file, and return.
  1243. jpeg_destroy_compress(&cinfo);
  1244. throw (const char*)NULL;
  1245. }
  1246. // Now we can initialize the JPEG compression object
  1247. jpeg_create_compress(&cinfo);
  1248. // Step 2: specify data destination (eg, a file)
  1249. jpeg_freeimage_dst(&cinfo, handle, io);
  1250. // Step 3: set parameters for compression
  1251. cinfo.image_width = FreeImage_GetWidth(dib);
  1252. cinfo.image_height = FreeImage_GetHeight(dib);
  1253. switch(color_type) {
  1254. case FIC_MINISBLACK :
  1255. case FIC_MINISWHITE :
  1256. cinfo.in_color_space = JCS_GRAYSCALE;
  1257. cinfo.input_components = 1;
  1258. break;
  1259. default :
  1260. cinfo.in_color_space = JCS_RGB;
  1261. cinfo.input_components = 3;
  1262. break;
  1263. }
  1264. jpeg_set_defaults(&cinfo);
  1265. // progressive-JPEG support
  1266. if((flags & JPEG_PROGRESSIVE) == JPEG_PROGRESSIVE) {
  1267. jpeg_simple_progression(&cinfo);
  1268. }
  1269. // compute optimal Huffman coding tables for the image
  1270. if((flags & JPEG_OPTIMIZE) == JPEG_OPTIMIZE) {
  1271. cinfo.optimize_coding = TRUE;
  1272. }
  1273. // Set JFIF density parameters from the DIB data
  1274. cinfo.X_density = (UINT16) (0.5 + 0.0254 * FreeImage_GetDotsPerMeterX(dib));
  1275. cinfo.Y_density = (UINT16) (0.5 + 0.0254 * FreeImage_GetDotsPerMeterY(dib));
  1276. cinfo.density_unit = 1; // dots / inch
  1277. // thumbnail support (JFIF 1.02 extension markers)
  1278. if(FreeImage_GetThumbnail(dib) != NULL) {
  1279. cinfo.write_JFIF_header = 1; //<### force it, though when color is CMYK it will be incorrect
  1280. cinfo.JFIF_minor_version = 2;
  1281. }
  1282. // baseline JPEG support
  1283. if ((flags & JPEG_BASELINE) == JPEG_BASELINE) {
  1284. cinfo.write_JFIF_header = 0; // No marker for non-JFIF colorspaces
  1285. cinfo.write_Adobe_marker = 0; // write no Adobe marker by default
  1286. }
  1287. // set subsampling options if required
  1288. if(cinfo.in_color_space == JCS_RGB) {
  1289. if((flags & JPEG_SUBSAMPLING_411) == JPEG_SUBSAMPLING_411) {
  1290. // 4:1:1 (4x1 1x1 1x1) - CrH 25% - CbH 25% - CrV 100% - CbV 100%
  1291. // the horizontal color resolution is quartered
  1292. cinfo.comp_info[0].h_samp_factor = 4; // Y
  1293. cinfo.comp_info[0].v_samp_factor = 1;
  1294. cinfo.comp_info[1].h_samp_factor = 1; // Cb
  1295. cinfo.comp_info[1].v_samp_factor = 1;
  1296. cinfo.comp_info[2].h_samp_factor = 1; // Cr
  1297. cinfo.comp_info[2].v_samp_factor = 1;
  1298. } else if((flags & JPEG_SUBSAMPLING_420) == JPEG_SUBSAMPLING_420) {
  1299. // 4:2:0 (2x2 1x1 1x1) - CrH 50% - CbH 50% - CrV 50% - CbV 50%
  1300. // the chrominance resolution in both the horizontal and vertical directions is cut in half
  1301. cinfo.comp_info[0].h_samp_factor = 2; // Y
  1302. cinfo.comp_info[0].v_samp_factor = 2;
  1303. cinfo.comp_info[1].h_samp_factor = 1; // Cb
  1304. cinfo.comp_info[1].v_samp_factor = 1;
  1305. cinfo.comp_info[2].h_samp_factor = 1; // Cr
  1306. cinfo.comp_info[2].v_samp_factor = 1;
  1307. } else if((flags & JPEG_SUBSAMPLING_422) == JPEG_SUBSAMPLING_422){ //2x1 (low)
  1308. // 4:2:2 (2x1 1x1 1x1) - CrH 50% - CbH 50% - CrV 100% - CbV 100%
  1309. // half of the horizontal resolution in the chrominance is dropped (Cb & Cr),
  1310. // while the full resolution is retained in the vertical direction, with respect to the luminance
  1311. cinfo.comp_info[0].h_samp_factor = 2; // Y
  1312. cinfo.comp_info[0].v_samp_factor = 1;
  1313. cinfo.comp_info[1].h_samp_factor = 1; // Cb
  1314. cinfo.comp_info[1].v_samp_factor = 1;
  1315. cinfo.comp_info[2].h_samp_factor = 1; // Cr
  1316. cinfo.comp_info[2].v_samp_factor = 1;
  1317. }
  1318. else if((flags & JPEG_SUBSAMPLING_444) == JPEG_SUBSAMPLING_444){ //1x1 (no subsampling)
  1319. // 4:4:4 (1x1 1x1 1x1) - CrH 100% - CbH 100% - CrV 100% - CbV 100%
  1320. // the resolution of chrominance information (Cb & Cr) is preserved
  1321. // at the same rate as the luminance (Y) information
  1322. cinfo.comp_info[0].h_samp_factor = 1; // Y
  1323. cinfo.comp_info[0].v_samp_factor = 1;
  1324. cinfo.comp_info[1].h_samp_factor = 1; // Cb
  1325. cinfo.comp_info[1].v_samp_factor = 1;
  1326. cinfo.comp_info[2].h_samp_factor = 1; // Cr
  1327. cinfo.comp_info[2].v_samp_factor = 1;
  1328. }
  1329. }
  1330. // Step 4: set quality
  1331. // the first 7 bits are reserved for low level quality settings
  1332. // the other bits are high level (i.e. enum-ish)
  1333. int quality;
  1334. if ((flags & JPEG_QUALITYBAD) == JPEG_QUALITYBAD) {
  1335. quality = 10;
  1336. } else if ((flags & JPEG_QUALITYAVERAGE) == JPEG_QUALITYAVERAGE) {
  1337. quality = 25;
  1338. } else if ((flags & JPEG_QUALITYNORMAL) == JPEG_QUALITYNORMAL) {
  1339. quality = 50;
  1340. } else if ((flags & JPEG_QUALITYGOOD) == JPEG_QUALITYGOOD) {
  1341. quality = 75;
  1342. } else if ((flags & JPEG_QUALITYSUPERB) == JPEG_QUALITYSUPERB) {
  1343. quality = 100;
  1344. } else {
  1345. if ((flags & 0x7F) == 0) {
  1346. quality = 75;
  1347. } else {
  1348. quality = flags & 0x7F;
  1349. }
  1350. }
  1351. jpeg_set_quality(&cinfo, quality, TRUE); /* limit to baseline-JPEG values */
  1352. // Step 5: Start compressor
  1353. jpeg_start_compress(&cinfo, TRUE);
  1354. // Step 6: Write special markers
  1355. if ((flags & JPEG_BASELINE) != JPEG_BASELINE) {
  1356. write_markers(&cinfo, dib);
  1357. }
  1358. // Step 7: while (scan lines remain to be written)
  1359. if(color_type == FIC_RGB) {
  1360. // 24-bit RGB image : need to swap red and blue channels
  1361. unsigned pitch = FreeImage_GetPitch(dib);
  1362. BYTE *target = (BYTE*)malloc(pitch * sizeof(BYTE));
  1363. if (target == NULL) {
  1364. throw FI_MSG_ERROR_MEMORY;
  1365. }
  1366. while (cinfo.next_scanline < cinfo.image_height) {
  1367. // get a copy of the scanline
  1368. memcpy(target, FreeImage_GetScanLine(dib, FreeImage_GetHeight(dib) - cinfo.next_scanline - 1), pitch);
  1369. #if FREEIMAGE_COLORORDER == FREEIMAGE_COLORORDER_BGR
  1370. // swap R and B channels
  1371. BYTE *target_p = target;
  1372. for(unsigned x = 0; x < cinfo.image_width; x++) {
  1373. INPLACESWAP(target_p[0], target_p[2]);
  1374. target_p += 3;
  1375. }
  1376. #endif
  1377. // write the scanline
  1378. jpeg_write_scanlines(&cinfo, &target, 1);
  1379. }
  1380. free(target);
  1381. }
  1382. else if(color_type == FIC_MINISBLACK) {
  1383. // 8-bit standard greyscale images
  1384. while (cinfo.next_scanline < cinfo.image_height) {
  1385. JSAMPROW b = FreeImage_GetScanLine(dib, FreeImage_GetHeight(dib) - cinfo.next_scanline - 1);
  1386. jpeg_write_scanlines(&cinfo, &b, 1);
  1387. }
  1388. }
  1389. else if(color_type == FIC_PALETTE) {
  1390. // 8-bit palettized images are converted to 24-bit images
  1391. RGBQUAD *palette = FreeImage_GetPalette(dib);
  1392. BYTE *target = (BYTE*)malloc(cinfo.image_width * 3);
  1393. if (target == NULL) {
  1394. throw FI_MSG_ERROR_MEMORY;
  1395. }
  1396. while (cinfo.next_scanline < cinfo.image_height) {
  1397. BYTE *source = FreeImage_GetScanLine(dib, FreeImage_GetHeight(dib) - cinfo.next_scanline - 1);
  1398. FreeImage_ConvertLine8To24(target, source, cinfo.image_width, palette);
  1399. #if FREEIMAGE_COLORORDER == FREEIMAGE_COLORORDER_BGR
  1400. // swap R and B channels
  1401. BYTE *target_p = target;
  1402. for(unsigned x = 0; x < cinfo.image_width; x++) {
  1403. INPLACESWAP(target_p[0], target_p[2]);
  1404. target_p += 3;
  1405. }
  1406. #endif
  1407. jpeg_write_scanlines(&cinfo, &target, 1);
  1408. }
  1409. free(target);
  1410. }
  1411. else if(color_type == FIC_MINISWHITE) {
  1412. // reverse 8-bit greyscale image, so reverse grey value on the fly
  1413. unsigned i;
  1414. BYTE reverse[256];
  1415. BYTE *target = (BYTE *)malloc(cinfo.image_width);
  1416. if (target == NULL) {
  1417. throw FI_MSG_ERROR_MEMORY;
  1418. }
  1419. for(i = 0; i < 256; i++) {
  1420. reverse[i] = (BYTE)(255 - i);
  1421. }
  1422. while(cinfo.next_scanline < cinfo.image_height) {
  1423. BYTE *source = FreeImage_GetScanLine(dib, FreeImage_GetHeight(dib) - cinfo.next_scanline - 1);
  1424. for(i = 0; i < cinfo.image_width; i++) {
  1425. target[i] = reverse[ source[i] ];
  1426. }
  1427. jpeg_write_scanlines(&cinfo, &target, 1);
  1428. }
  1429. free(target);
  1430. }
  1431. // Step 8: Finish compression
  1432. jpeg_finish_compress(&cinfo);
  1433. // Step 9: release JPEG compression object
  1434. jpeg_destroy_compress(&cinfo);
  1435. return TRUE;
  1436. } catch (const char *text) {
  1437. if(text) {
  1438. FreeImage_OutputMessageProc(s_format_id, text);
  1439. }
  1440. return FALSE;
  1441. }
  1442. }
  1443. return FALSE;
  1444. }
  1445. // ==========================================================
  1446. // Init
  1447. // ==========================================================
  1448. void DLL_CALLCONV
  1449. InitJPEG(Plugin *plugin, int format_id) {
  1450. s_format_id = format_id;
  1451. plugin->format_proc = Format;
  1452. plugin->description_proc = Description;
  1453. plugin->extension_proc = Extension;
  1454. plugin->regexpr_proc = RegExpr;
  1455. plugin->open_proc = NULL;
  1456. plugin->close_proc = NULL;
  1457. plugin->pagecount_proc = NULL;
  1458. plugin->pagecapability_proc = NULL;
  1459. plugin->load_proc = Load;
  1460. plugin->save_proc = Save;
  1461. plugin->validate_proc = Validate;
  1462. plugin->mime_proc = MimeType;
  1463. plugin->supports_export_bpp_proc = SupportsExportDepth;
  1464. plugin->supports_export_type_proc = SupportsExportType;
  1465. plugin->supports_icc_profiles_proc = SupportsICCProfiles;
  1466. plugin->supports_no_pixels_proc = SupportsNoPixels;
  1467. }