about summary refs log tree commit diff
path: root/converter/ppm/ppmtompeg/jpeg.c
diff options
context:
space:
mode:
Diffstat (limited to 'converter/ppm/ppmtompeg/jpeg.c')
-rw-r--r--converter/ppm/ppmtompeg/jpeg.c230
1 files changed, 115 insertions, 115 deletions
diff --git a/converter/ppm/ppmtompeg/jpeg.c b/converter/ppm/ppmtompeg/jpeg.c
index 24c9ae2d..4540f6f0 100644
--- a/converter/ppm/ppmtompeg/jpeg.c
+++ b/converter/ppm/ppmtompeg/jpeg.c
@@ -1,12 +1,12 @@
 /*===========================================================================*
- * jpeg.c                              
- *                                     
- *  procedures to deal with JPEG files 
- *                                     
- * EXPORTED PROCEDURES:                
- *  JMovie2JPEG                        
- *      ReadJPEG                      
- *                                    
+ * jpeg.c
+ *
+ *  procedures to deal with JPEG files
+ *
+ * EXPORTED PROCEDURES:
+ *  JMovie2JPEG
+ *      ReadJPEG
+ *
  *===========================================================================*/
 
 /* COPYRIGHT INFORMATION IS AT THE END OF THIS FILE */
@@ -20,7 +20,7 @@
 /* With the lossless jpeg patch applied to the Jpeg library
     (ftp://ftp.wizards.dupont.com/pub/ImageMagick/delegates/ljpeg-6b.tar.gz),
     the name of min_DCT_scaled_size changes to min_codec_data_unit,
-    for some reason.  With this macro, we change it back.  
+    for some reason.  With this macro, we change it back.
 */
 #define min_codec_data_unit min_DCT_scaled_size
 #include <jpeglib.h>
@@ -40,7 +40,7 @@
 /* make it happier.... */
 #undef DCTSIZE2
 
-/* jcopy_sample_rows() is an internal routine in the JPEG library, not 
+/* jcopy_sample_rows() is an internal routine in the JPEG library, not
    meant for use by us.  We should figure out what the official interface
    for this is and use it.  The following is copied out of jpegint.h, which
    is part of the JPEG library source code.
@@ -88,7 +88,7 @@ void
 JMovie2JPEG(const char * const infilename,
                 /* input filename string */
             const char * const obase,
-                /* output filename base string=>obase##.jpg */ 
+                /* output filename base string=>obase##.jpg */
             int          const start,
                 /* first frame to be extracted */
             int          const end
@@ -126,154 +126,154 @@ JMovie2JPEG(const char * const infilename,
        for the appending of the jpeg bitstream. It can be
        made smaller if you have a better idea of its expected size
     */
-    static unsigned char inbuffer[300000] = {    
-        0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46,  
+    static unsigned char inbuffer[300000] = {
+        0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46,
         0x49, 0x46, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01,
-        0x00, 0x01, 0x00, 0x00, 0xFF, 0xC0, 0x00, 0x11,  
+        0x00, 0x01, 0x00, 0x00, 0xFF, 0xC0, 0x00, 0x11,
         0x08, 0x00, 0xF0, 0x01, 0x40, 0x03, 0x01, 0x21,
-        0x00, 0x02, 0x11, 0x01, 0x03, 0x11, 0x01, 0xFF,  
+        0x00, 0x02, 0x11, 0x01, 0x03, 0x11, 0x01, 0xFF,
         0xDB, 0x00, 0x84, 0x00, 0x10, 0x0B, 0x0C, 0x0E,
-        0x0C, 0x0A, 0x10, 0x0E, 0x0D, 0x0E, 0x12,  
+        0x0C, 0x0A, 0x10, 0x0E, 0x0D, 0x0E, 0x12,
         0x11, 0x10, 0x13, 0x18, 0x28, 0x1A, 0x18, 0x16,
-        0x16, 0x18, 0x31, 0x23, 0x25, 0x1D, 0x28, 0x3A,  
+        0x16, 0x18, 0x31, 0x23, 0x25, 0x1D, 0x28, 0x3A,
         0x33, 0x3D, 0x3C, 0x39, 0x33, 0x38, 0x37, 0x40,
-        0x48, 0x5C, 0x4E, 0x40, 0x44, 0x57, 0x45, 0x37,  
+        0x48, 0x5C, 0x4E, 0x40, 0x44, 0x57, 0x45, 0x37,
         0x38, 0x50, 0x6D, 0x51, 0x57, 0x5F, 0x62, 0x67,
-        0x68, 0x67, 0x3E, 0x4D, 0x71, 0x79, 0x70, 0x64,  
+        0x68, 0x67, 0x3E, 0x4D, 0x71, 0x79, 0x70, 0x64,
         0x78, 0x5C, 0x65, 0x67, 0x63, 0x01, 0x11, 0x12,
-        0x12, 0x18, 0x15, 0x18, 0x2F, 0x1A, 0x1A, 0x2F,  
+        0x12, 0x18, 0x15, 0x18, 0x2F, 0x1A, 0x1A, 0x2F,
         0x63, 0x42, 0x38, 0x42, 0x63, 0x63, 0x63, 0x63,
-        0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,  
-        0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,  
-        0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,  
-        0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,  
-        0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,  
+        0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
+        0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
+        0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
+        0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
+        0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
         0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0xFF, 0xC4,
-        0x01, 0xA2, 0x00, 0x00, 0x01, 0x05, 0x01, 0x01,  
+        0x01, 0xA2, 0x00, 0x00, 0x01, 0x05, 0x01, 0x01,
         0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
-        0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04,  
+        0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04,
         0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
-        0x10, 0x00, 0x02, 0x01, 0x03, 0x03, 0x02, 0x04,  
+        0x10, 0x00, 0x02, 0x01, 0x03, 0x03, 0x02, 0x04,
         0x03, 0x05, 0x05, 0x04, 0x04, 0x00, 0x00, 0x01,
-        0x7D, 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05,  
+        0x7D, 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05,
         0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61,
-        0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xA1,  
+        0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xA1,
         0x08, 0x23, 0x42, 0xB1, 0xC1, 0x15, 0x52, 0xD1,
-        0xF0, 0x24, 0x33, 0x62, 0x72, 0x82, 0x09,   
+        0xF0, 0x24, 0x33, 0x62, 0x72, 0x82, 0x09,
         0x0A, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x25, 0x26,
-        0x27, 0x28, 0x29, 0x2A, 0x34, 0x35, 0x36, 0x37,  
+        0x27, 0x28, 0x29, 0x2A, 0x34, 0x35, 0x36, 0x37,
         0x38, 0x39, 0x3A, 0x43, 0x44, 0x45, 0x46, 0x47,
-        0x48, 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57,  
+        0x48, 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57,
         0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67,
-        0x68, 0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77,  
+        0x68, 0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77,
         0x78, 0x79, 0x7A, 0x83, 0x84, 0x85, 0x86, 0x87,
-        0x88, 0x89, 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96,  
+        0x88, 0x89, 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96,
         0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5,
-        0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4,  
+        0xA6, 0xA7, 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4,
         0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xC2, 0xC3,
-        0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2,  
+        0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2,
         0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA,
-        0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8,  
+        0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8,
         0xE9, 0xEA, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6,
-        0xF7, 0xF8, 0xF9, 0xFA, 0x01, 0x00, 0x03, 0x01,  
+        0xF7, 0xF8, 0xF9, 0xFA, 0x01, 0x00, 0x03, 0x01,
         0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
-        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,  
-        0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 
-        0x0A, 0x0B, 0x11, 0x00, 0x02, 0x01, 0x02, 0x04,  
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,
+        0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
+        0x0A, 0x0B, 0x11, 0x00, 0x02, 0x01, 0x02, 0x04,
         0x04, 0x03, 0x04, 0x07, 0x05, 0x04, 0x04, 0x00,
-        0x01, 0x02, 0x77, 0x00, 0x01, 0x02, 0x03, 0x11,  
+        0x01, 0x02, 0x77, 0x00, 0x01, 0x02, 0x03, 0x11,
         0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51,
-        0x07, 0x61, 0x71, 0x13, 0x22, 0x32, 0x81, 0x08,  
+        0x07, 0x61, 0x71, 0x13, 0x22, 0x32, 0x81, 0x08,
         0x14, 0x42, 0x91, 0xA1, 0xB1, 0xC1, 0x09, 0x23,
-        0x33, 0x52, 0xF0, 0x15, 0x62, 0x72, 0xD1,   
+        0x33, 0x52, 0xF0, 0x15, 0x62, 0x72, 0xD1,
         0x0A, 0x16, 0x24, 0x34, 0xE1, 0x25, 0xF1, 0x17,
-        0x18, 0x19, 0x1A, 0x26, 0x27, 0x28, 0x29, 0x2A,  
+        0x18, 0x19, 0x1A, 0x26, 0x27, 0x28, 0x29, 0x2A,
         0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x43, 0x44,
-        0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54,  
+        0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54,
         0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x63, 0x64,
-        0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x73, 0x74,  
+        0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x73, 0x74,
         0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x82, 0x83,
-        0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x92,  
+        0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x92,
         0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A,
-        0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9,  
+        0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8, 0xA9,
         0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8,
-        0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,  
+        0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
         0xC8, 0xC9, 0xCA, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6,
-        0xD7, 0xD8, 0xD9, 0xDA, 0xE2, 0xE3, 0xE4, 0xE5,  
+        0xD7, 0xD8, 0xD9, 0xDA, 0xE2, 0xE3, 0xE4, 0xE5,
         0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xF2, 0xF3, 0xF4,
-        0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFF, 0xDA,  
+        0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFF, 0xDA,
         0x00, 0x0C, 0x03, 0x01, 0x00, 0x02, 0x11, 0x03,
-        0x11, 0x00, 0x3F, 0x00  
+        0x11, 0x00, 0x3F, 0x00
 
     };
-    
+
     if (start > end) {
         fprintf(stderr,"bad frame numbers\n");
         exit(1);
     }
-    
+
     /* open J_Movie */
     inFile = fopen(infilename, "rb");
     if (inFile == NULL) {
         perror (infilename);
         exit(1);
     }
-    
-    /* get file descriptor */    
+
+    /* get file descriptor */
     fd = fileno(inFile);
-    
+
     /* The following lines parse the jpeg_movie header and recover the */
-    /* relavant information */
+    /* relevant information */
 
     fseek(inFile, 8 * sizeof(char), 0);
-    
+
     if (fread(&ver_no, sizeof(int), 1, inFile) != 1) {
         perror("Error in reading version");
         exit(1);
-    }  
+    }
     if (ver_no != 2) {
         perror("Unrecognized version - Quantization tables may be wrong\n");
     }
     if (fread(&fps, sizeof(int), 1, inFile) != 1) {
         perror("Error in reading fps");
         exit(1);
-    }  
+    }
     if (fread (&no_frames, sizeof(int), 1, inFile) != 1) {
         perror("Error in reading no_frames");
         exit(1);
-    }  
+    }
 
     MALLOCARRAY(inoffsets, no_frames);
-    
+
     if (fread(&width, sizeof(int), 1, inFile) != 1) {
         perror("Error in reading width");
         exit(1);
-    }  
+    }
     /* set image width in JFIF header */
     inbuffer[27] = (char)(0xFF & (width >> 8));
     inbuffer[28] = (char)(0xFF & width);
- 
+
     if (fread(&height, sizeof(int), 1, inFile) != 1) {
         perror("Error in reading height");
         exit(1);
-    }  
+    }
     /* set image height in JFIF header */
     inbuffer[25] = (char)(0xFF & (height >> 8));
     inbuffer[26] = (char)(0xFF & height);
-    
+
     if (fread(&bandwidth, sizeof(int), 1, inFile) != 1) {
         perror("Error in reading bandwidth");
         exit(1);
-    }  
-    
+    }
+
     if (fread(&qfactor, sizeof(int), 1, inFile) != 1) {
         perror("Error in reading qfactor");
         exit(1);
-    }  
+    }
     /* The default quality factor = 100, therefore, if
        our quality factor does not equal 100 we must
        scale the quantization matrices in the JFIF header
-    */    
+    */
     /* Note values are clipped to a max of 255 */
     if (qfactor != 100) {
         for (Temp = 44; Temp < 108; ++Temp) {
@@ -283,13 +283,13 @@ JMovie2JPEG(const char * const infilename,
         for (Temp = 109; Temp < 173; ++Temp) {
             temp = (inbuffer[Temp]*qfactor)/100;
             inbuffer[Temp] = (char)((temp<255) ? temp : 255);
-        }    
+        }
     }
-  
+
     if (fread(&mapsize, sizeof(int), 1, inFile) != 1) {
         perror("Error in reading mapsize");
         exit(1);
-    }  
+    }
     if (fread (&image_offset, sizeof(int), 1, inFile) != 1) {
         perror("Error in reading image offset");
         exit(1);
@@ -298,32 +298,32 @@ JMovie2JPEG(const char * const infilename,
         perror("Error in reading audio tracks");
         exit(1);
     }
-    
+
     fread(junk,sizeof(int),1,inFile);
-    
+
     if (fread (&audiosize, sizeof(int), 1, inFile) != 1) {
         perror("Error in reading audiosize");
         exit(1);
     }
-    
+
     fseek (inFile, image_offset, 0);
-    
+
     last = MIN(end, no_frames-1);
 
     for (i = 0; i < no_frames; ++i)  {
         fread(&(inoffsets[i]), sizeof(int), 1, inFile);
     } /* Reads in the frame sizes into the array */
-    
+
     rewind(inFile);
 
-    /* Extract JFIF files from J_Movie */    
+    /* Extract JFIF files from J_Movie */
     for (i = start; i <= last; ++i) {
         size = inoffsets[i] - inoffsets[i-1]- 5;
-        lseek(fd, inoffsets[i-1], 0); 
+        lseek(fd, inoffsets[i-1], 0);
         read(fd, &(op_code), 1);
         while (op_code !=  0xffffffec) {
             read(fd,junk,audiosize);
-            read(fd, &(op_code), 1);  
+            read(fd, &(op_code), 1);
             size = size - audiosize ;
         } /* To skip the audio bytes in each frame */
         read(fd, jpeg_size, 4);
@@ -331,7 +331,7 @@ JMovie2JPEG(const char * const infilename,
         sprintf(ofname, "%s%d.jpg", obase, i);
         outFile = fopen(ofname, "wb");
         fwrite(inbuffer, (size+607), sizeof(char), outFile);
-        fclose(outFile);        
+        fclose(outFile);
     }
     free(inoffsets);
     fclose(inFile);
@@ -413,50 +413,50 @@ ReadJPEG(MpegFrame * const mf,
             jpeg_stdio_src(&cinfo, fp);
         }
     }
-  
+
     /* specify data source (eg, a file) */
-  
+
     jpeg_stdio_src(&cinfo, fp);
-  
+
     /* read file parameters with jpeg_read_header() */
-  
-  
+
+
     (void) jpeg_read_header(&cinfo, TRUE);
     /* We can ignore the return value from jpeg_read_header since
      *   (a) suspension is not possible with the stdio data source, and
      *   (b) we passed TRUE to reject a tables-only JPEG file as an error.
      */
-  
+
     /* set parameters for decompression */
     cinfo.raw_data_out = TRUE;
     cinfo.out_color_space = JCS_YCbCr;
-  
+
     /* calculate image output dimensions */
     jpeg_calc_output_dimensions(&cinfo);
     /* the above calculation will set these soon */
     /* for now we'll set them ourselves */
-    
+
     /* tell mpeg_encode the size of the JPEG Image*/
     Fsize_Note(mf->id,(int)(cinfo.image_width),(int)(cinfo.image_height));
-  
+
     /* Allocate memory for the raw YCbCr data to occupy*/
     Frame_AllocYCC(mf);      /*allocate space for mpeg frame*/
-  
+
     /* copy pointers to array structure- this make the following
        code more compact
     */
     orig[0] = mf->orig_y;
     orig[1] = mf->orig_cb;
     orig[2] = mf->orig_cr;
-  
+
     /* Note that we can use the info obtained from jpeg_read_header.
      */
-  
+
     /* Start decompressor */
-  
+
     jpeg_start_decompress(&cinfo);
-  
-  
+
+
     /* JSAMPLEs per row in output buffer  */
     /* collect component subsample values*/
     for (cp=0, compptr = cinfo.comp_info;
@@ -470,28 +470,28 @@ ReadJPEG(MpegFrame * const mf,
     max_h_samp = (temp_h<h_samp[2]) ? h_samp[2]:temp_h;
     temp_v = (v_samp[0]<v_samp[1]) ? v_samp[1] : v_samp[0];
     max_v_samp = (temp_v<v_samp[2]) ? v_samp[2]:temp_v;
-  
+
     /* Make an 8-row-high sample array that will go away when done
        with image
     */
     buffer_height = cinfo.max_v_samp_factor * minDctVScaledSize(cinfo);
-  
+
     for(cp=0,compptr = cinfo.comp_info;cp<cinfo.num_components;
         cp++,compptr++) {
         ncols[cp] = (JDIMENSION)((cinfo.image_width*compptr->h_samp_factor)/
                                  max_h_samp);
-    
+
         nrows[cp] = (JDIMENSION)((buffer_height*compptr->v_samp_factor)/
                                  max_v_samp);
-    
+
         scanarray[cp] = (*cinfo.mem->alloc_sarray)
             ((j_common_ptr) &cinfo, JPOOL_IMAGE, ncols[cp], nrows[cp]);
     }
-  
+
     /*  while (scan lines remain to be read)
            jpeg_read_scanlines(...);
     */
-  
+
     /* Here we use the library's state variable cinfo.output_scanline as the
      * loop counter, so that we don't have to keep track ourselves.
      */
@@ -525,29 +525,29 @@ ReadJPEG(MpegFrame * const mf,
             }
         } else
             pm_error("Invalid subsampling ratio");
-    
+
         /* transfer data from jpeg buffer to MPEG frame */
         /* calculate the row we wish to output into */
         for (ci = 0, compptr = cinfo.comp_info;
              ci < cinfo.num_components;
              ++ci, ++compptr) {
             current_row[ci] =((cinfo.output_scanline - buffer_height)*
-                              (v_samp[ci])/max_v_samp);  
-      
+                              (v_samp[ci])/max_v_samp);
+
             jcopy_sample_rows(scanarray[ci],0,(JSAMPARRAY)(orig[ci]),
                               current_row[ci],nrows[ci],ncols[ci]);
         }
-    }  
-  
+    }
+
     /* Step 7: Finish decompression */
-  
+
     (void) jpeg_finish_decompress(&cinfo);
     /* We can ignore the return value since suspension is not possible
      * with the stdio data source.
      */
-  
+
     /* Step 8: Release JPEG decompression object */
-  
+
     /*
     ** DO NOT release the cinfo struct if we are reading from stdin, this
     ** is because the cinfo struct contains the read buffer, and the read
@@ -572,7 +572,7 @@ ReadJPEG(MpegFrame * const mf,
        think that jpeg_destroy can do an error exit, but why assume
        anything...)
     */
-  
+
     /* At this point you may want to check to see whether any corrupt-data
      * warnings occurred (test whether jerr.pub.num_warnings is nonzero).
      * If you prefer to treat corrupt data as a fatal error, override the