]> git.imager.perl.org - imager.git/blobdiff - JPEG/imjpeg.c
update Changes
[imager.git] / JPEG / imjpeg.c
index d4341b40bd28784d32de32277a8a5152371a0ca4..dcb0ed639c3683cf78315376c2d6e14f9b330f03 100644 (file)
@@ -28,12 +28,12 @@ Reads and writes JPEG images
 #include <unistd.h>
 #endif
 #include <setjmp.h>
+#include <string.h>
 
 #include "jpeglib.h"
 #include "jerror.h"
 #include <errno.h>
 #include <stdlib.h>
-#include <stdio.h>
 #include "imexif.h"
 
 #define JPEG_APP13       0xED    /* APP13 marker code */
@@ -42,17 +42,13 @@ Reads and writes JPEG images
 
 #define JPEG_DIM_MAX JPEG_MAX_DIMENSION
 
-static unsigned char fake_eoi[]={(JOCTET) 0xFF,(JOCTET) JPEG_EOI};
-
-/* Bad design right here */
-
-static int tlength=0;
-static char **iptc_text=NULL;
+#define _STRINGIFY(x) #x
+#define STRINGIFY(x) _STRINGIFY(x)
 
+static unsigned char fake_eoi[]={(JOCTET) 0xFF,(JOCTET) JPEG_EOI};
 
 /* Source and Destination managers */
 
-
 typedef struct {
   struct jpeg_source_mgr pub;  /* public fields */
   io_glue *data;
@@ -71,7 +67,6 @@ typedef struct {
 typedef wiol_source_mgr *wiol_src_ptr;
 typedef wiol_destination_mgr *wiol_dest_ptr;
 
-
 /*
  * Methods for io manager objects 
  * 
@@ -223,7 +218,7 @@ wiol_empty_output_buffer(j_compress_ptr cinfo) {
 
   if (rc != JPGS) { /* XXX: Should raise some jpeg error */
     myfree(dest->buffer);
-    mm_log((1, "wiol_empty_output_buffer: Error: nbytes = %d != rc = %d\n", JPGS, rc));
+    mm_log((1, "wiol_empty_output_buffer: Error: nbytes = %d != rc = %d\n", JPGS, (int)rc));
     ERREXIT(cinfo, JERR_FILE_WRITE);
   }
   dest->pub.free_in_buffer = JPGS;
@@ -271,38 +266,6 @@ jpeg_wiol_dest(j_compress_ptr cinfo, io_glue *ig) {
   dest->pub.next_output_byte    = dest->buffer;
 }
 
-LOCAL(unsigned int)
-jpeg_getc (j_decompress_ptr cinfo)
-/* Read next byte */
-{
-  struct jpeg_source_mgr * datasrc = cinfo->src;
-
-  if (datasrc->bytes_in_buffer == 0) {
-    if (! (*datasrc->fill_input_buffer) (cinfo))
-      { fprintf(stderr,"Jpeglib: cant suspend.\n"); exit(3); }
-      /*      ERREXIT(cinfo, JERR_CANT_SUSPEND);*/
-  }
-  datasrc->bytes_in_buffer--;
-  return GETJOCTET(*datasrc->next_input_byte++);
-}
-
-METHODDEF(boolean)
-APP13_handler (j_decompress_ptr cinfo) {
-  INT32 length;
-  unsigned int cnt=0;
-  
-  length = jpeg_getc(cinfo) << 8;
-  length += jpeg_getc(cinfo);
-  length -= 2; /* discount the length word itself */
-  
-  tlength=length;
-
-  if ( ((*iptc_text)=mymalloc(length)) == NULL ) return FALSE;
-  while (--length >= 0) (*iptc_text)[cnt++] = jpeg_getc(cinfo); 
-  return TRUE;
-}
-
 METHODDEF(void)
 my_output_message (j_common_ptr cinfo) {
   char buffer[JMSG_LENGTH_MAX];
@@ -377,6 +340,25 @@ transfer_gray(i_color *out, JSAMPARRAY in, int width) {
 
 typedef void (*transfer_function_t)(i_color *out, JSAMPARRAY in, int width);
 
+static const char version_string[] =
+#ifdef LIBJPEG_TURBO_VERSION
+  "libjpeg-turbo " STRINGIFY(LIBJPEG_TURBO_VERSION) " api " STRINGIFY(JPEG_LIB_VERSION)
+#else
+  "libjpeg " STRINGIFY(JPEG_LIB_VERSION)
+#endif
+  ;
+
+/*
+=item i_libjpeg_version()
+
+=cut
+*/
+
+const char *
+i_libjpeg_version(void) {
+  return version_string;
+}
+
 /*
 =item i_readjpeg_wiol(data, length, iptc_itext, itlength)
 
@@ -400,7 +382,9 @@ i_readjpeg_wiol(io_glue *data, int length, char** iptc_itext, int *itlength) {
 
   i_clear_error();
 
-  iptc_text = iptc_itext;
+  *iptc_itext = NULL;
+  *itlength = 0;
+
   cinfo.err = jpeg_std_error(&jerr.pub);
   jerr.pub.error_exit     = my_error_exit;
   jerr.pub.output_message = my_output_message;
@@ -410,8 +394,6 @@ i_readjpeg_wiol(io_glue *data, int length, char** iptc_itext, int *itlength) {
     if (src_set)
       wiol_term_source(&cinfo);
     jpeg_destroy_decompress(&cinfo); 
-    *iptc_itext=NULL;
-    *itlength=0;
     if (line_buffer)
       myfree(line_buffer);
     if (im)
@@ -420,7 +402,7 @@ i_readjpeg_wiol(io_glue *data, int length, char** iptc_itext, int *itlength) {
   }
   
   jpeg_create_decompress(&cinfo);
-  jpeg_set_marker_processor(&cinfo, JPEG_APP13, APP13_handler);
+  jpeg_save_markers(&cinfo, JPEG_APP13, 0xFFFF);
   jpeg_save_markers(&cinfo, JPEG_APP1, 0xFFFF);
   jpeg_save_markers(&cinfo, JPEG_COM, 0xFFFF);
   jpeg_wiol_src(&cinfo, data, length);
@@ -515,6 +497,11 @@ i_readjpeg_wiol(io_glue *data, int length, char** iptc_itext, int *itlength) {
     else if (markerp->marker == JPEG_APP1 && !seen_exif) {
       seen_exif = i_int_decode_exif(im, markerp->data, markerp->data_length);
     }
+    else if (markerp->marker == JPEG_APP13) {
+      *iptc_itext = mymalloc(markerp->data_length);
+      memcpy(*iptc_itext, markerp->data, markerp->data_length);
+      *itlength = markerp->data_length;
+    }
 
     markerp = markerp->next;
   }
@@ -557,11 +544,10 @@ i_readjpeg_wiol(io_glue *data, int length, char** iptc_itext, int *itlength) {
 
   (void) jpeg_finish_decompress(&cinfo);
   jpeg_destroy_decompress(&cinfo);
-  *itlength=tlength;
 
   i_tags_set(&im->tags, "i_format", "jpeg", 4);
 
-  mm_log((1,"i_readjpeg_wiol -> (0x%x)\n",im));
+  mm_log((1,"i_readjpeg_wiol -> (%p)\n",im));
   return im;
 }
 
@@ -580,6 +566,7 @@ i_writejpeg_wiol(i_img *im, io_glue *ig, int qfactor) {
   int comment_entry;
   int want_channels = im->channels;
   int progressive = 0;
+  int optimize = 0;
 
   struct jpeg_compress_struct cinfo;
   struct my_error_mgr jerr;
@@ -641,6 +628,9 @@ i_writejpeg_wiol(i_img *im, io_glue *ig, int qfactor) {
   if (progressive) {
     jpeg_simple_progression(&cinfo);
   }
+  if (!i_tags_get_int(&im->tags, "jpeg_optimize", 0, &optimize))
+    optimize = 0;
+  cinfo.optimize_coding = optimize;
 
   got_xres = i_tags_get_float(&im->tags, "i_xres", 0, &xres);
   got_yres = i_tags_get_float(&im->tags, "i_yres", 0, &yres);