1 /*
2 * decomp.c --
3 *
4 * This is the routine that is called to decompress a frame
5 * image data. It is based on the program originally named ljpgtopnm.c.
6 * Major portions taken from the Independent JPEG Group' software, and
7 * from the Cornell lossless JPEG code
8 */
9 /*
10 *
11 */
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include "io.h"
16 #include "jpeg.h"
17 #include "mcu.h"
18 #include "proto.h"
19
20 static DecompressInfo dcInfo;
21 static StreamIN JpegInFile;
22
23 /*
24 *--------------------------------------------------------------
25 *
26 * ReadJpegData --
27 *
28 * This is an interface routine to the JPEG library. The
29 * JPEG library calls this routine to "get more data"
30 *
31 * Results:
32 * Number of bytes actually returned.
33 *
34 * Side effects:
35 * None.
36 *
37 *--------------------------------------------------------------
38 */
efree(void ** ptr)39 static void efree(void **ptr)
40 {
41 if((*ptr) != 0)
42 free((*ptr));
43 *ptr = 0;
44 }
45
46
ReadJpegData(Uchar * buffer,int numBytes)47 int ReadJpegData (Uchar *buffer, int numBytes)
48 {
49 unsigned long size = sizeof(unsigned char);
50 int r;
51
52 r = fread(buffer,size,(unsigned)numBytes,JpegInFile);
53 if (r != numBytes) return r;
54
55 return numBytes;
56 }
57
58
JPEGLosslessDecodeImage(StreamIN inFile,unsigned short * image16,int depth,int length)59 short JPEGLosslessDecodeImage (StreamIN inFile, unsigned short *image16, int depth, int length)
60 {
61 /* Initialization */
62 JpegInFile = inFile;
63 MEMSET (&dcInfo, 0, sizeof (dcInfo));
64 inputBufferOffset = 0;
65
66 /* Allocate input buffer */
67 inputBuffer = (unsigned char*)malloc((size_t)length+5);
68 if (inputBuffer == NULL)
69 return -1;
70
71 /* Read input buffer */
72 ReadJpegData (inputBuffer, length);
73 inputBuffer [length] = (unsigned char)EOF;
74
75 /* Read JPEG File header */
76 ReadFileHeader (&dcInfo);
77 if (dcInfo.error) { efree ((void **)&inputBuffer); return -1; }
78
79 /* Read the scan header */
80 if (!ReadScanHeader (&dcInfo)) { efree ((void **)&inputBuffer); return -1; }
81
82 /*
83 * Decode the image bits stream. Clean up everything when
84 * finished decoding.
85 */
86 DecoderStructInit (&dcInfo);
87 if (dcInfo.error) { efree ((void **)&inputBuffer); return -1; }
88
89 HuffDecoderInit (&dcInfo);
90 if (dcInfo.error) { efree ((void **)&inputBuffer); return -1; }
91
92 DecodeImage (&dcInfo, (unsigned short **) &image16, depth);
93
94 /* Free input buffer */
95 efree ((void **)&inputBuffer);
96
97 return 0;
98 }
99