]> Creatis software - gdcm.git/blob - src/jpeg/ljpg/decomp.c
*FIX: only one function is being exported for now ! You should use GLOBAL(return...
[gdcm.git] / src / jpeg / ljpg / decomp.c
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  * $Id: decomp.c,v 1.2 2003/10/29 18:24:40 malaterre Exp $
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 DecompressInfo  dcInfo;
21 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  */
39 static void efree(void **ptr)
40 {
41         if((*ptr) != 0)
42                 free((*ptr));
43         *ptr = 0;
44 }
45
46
47 int ReadJpegData (Uchar *buffer, int numBytes)
48 {
49     unsigned long size = sizeof(unsigned char);
50
51     fread(buffer,size,numBytes, JpegInFile);
52
53     return numBytes;  
54 }
55
56
57 GLOBAL(short) JPEGLosslessDecodeImage (StreamIN inFile, unsigned short *image16, int depth, int length)
58
59     /* Initialization */
60     JpegInFile = inFile;
61     MEMSET (&dcInfo, 0, sizeof (dcInfo));
62     inputBufferOffset = 0;
63      
64     /* Allocate input buffer */
65     inputBuffer = (unsigned char*)malloc((size_t)length+5);
66     if (inputBuffer == NULL)
67                 return -1;
68
69         /* Read input buffer */
70     ReadJpegData (inputBuffer, length);
71     inputBuffer [length] = EOF;
72     
73         /* Read JPEG File header */
74     ReadFileHeader (&dcInfo);
75     if (dcInfo.error) { efree ((void **)&inputBuffer); return -1; }
76
77     /* Read the scan header */
78     if (!ReadScanHeader (&dcInfo)) { efree ((void **)&inputBuffer); return -1; }
79    
80     /* 
81      * Decode the image bits stream. Clean up everything when
82      * finished decoding.
83      */
84     DecoderStructInit (&dcInfo);
85
86     if (dcInfo.error) { efree ((void **)&inputBuffer); return -1; }
87
88     HuffDecoderInit (&dcInfo);
89     
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 }