File: pnmtoljpoptimisebug.c

package info (click to toggle)
papyrus 3.7.1%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 6,216 kB
  • sloc: ansic: 49,488; cpp: 21,301; asm: 284; makefile: 221; sh: 152
file content (266 lines) | stat: -rwxr-xr-x 7,955 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
 * pnmtoljpoptimisebug.c --
 *
 * This is the main routine for the lossless JPEG encoder.
 *
 * Tranformations were made in order to integrate the lossless JPEG
 * in the Papyrus Toolkit and to increase the speed. 
 * Main options proposed by the Cornell Univ. were disabled, such as
 * Huffman table optimization. However this encoder is still JPEG compliant.
 * Two kinds of images are now supported by this lossless encoder:
 *		- 8-bits grayscale images,
 *		- 16-bits grayscale images.
 *
 * Copyright (c) 1994 Kongji Huang and Brian C. Smith.
 * Cornell University
 * All rights reserved.
 * 
 * Copyright (c) 1997 OSIRIS Team. Digital Imaging Unit
 * University Hospital of Geneva
 * changes made by David Bandon
 * All rights reserved
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice and the following
 * two paragraphs appear in all copies of this software.
 * 
 * IN NO EVENT SHALL CORNELL UNIVERSITY BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF CORNELL
 * UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * CORNELL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND CORNELL UNIVERSITY HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#include <string.h>
#include "JPEG.H"
#include "MCU.H"
#include "PROTO.H"
#include "JPEGLESS.H"

/* Papyrus 3 redefined basic types */
#ifndef FILENAME83		/* this is for the normal machines ... */

#ifndef PapyTypeDef3H
#include "PapyTypeDef3.h"
#endif

#ifndef PapyEalloc3H
#include "PapyEalloc3.h"
#endif

#ifndef   PapyFileSystem3H
#include "PapyFileSystem3.h"
#endif

#else				/* FILENAME83 defined for the DOS machines */

#ifndef PapyTypeDef3H
#include "PapyDef3.h"
#endif

#ifndef PapyEalloc3H
#include "Papaloc3.h"
#endif

#ifndef   PapyFileSystem3H
#include "PapFSys3.h"
#endif

#endif


#define HEADERSIZE 1000

/*
 * Global and static variables 
 */
 
int 		    verbose;			/* If verbose!=0, the verbose flag is on.	 	*/
int 		    psvSet[7];			/* The PSV set 									*/			
int 		    numSelValue;		/* Number of elements in psvPSV 				*/
PapyULong 	    inputFileBytes;		/* Total input file bytes 						*/
PapyULong 	    outputFileBytes;	/* Total output file bytes 						*/
long 		    totalHuffSym[7];	/* Total bits of Huffman coded categroy symbols	*/
long 		    totalAddBits[7];	/* Total additional bits 						*/
static PAPY_FILE    OutFile;		/* Output file pointer 							*/
PapyUChar 	    *JPEGBuffer;		/* Buffer for JPEG Data 						*/
PapyUChar 	    *readimBuffer;		/* Buffer for input image  8 bytes				*/
PapyUShort 	    *readim16Buffer;/* Buffer for input image  16 bytes				*/


extern void PmRead (CompressInfo *, int);
extern void HuffOptimize (CompressInfo *, long *);
extern void LoadStdHuffTables (CompressInfo *);

/*
 *--------------------------------------------------------------
 *
 * WriteJpegData --
 *
 *	This is an interface routine to the JPEG library.  The
 *	library calls this routine to output a block of data.
 *
 * Results:
 *	Number of bytes written.  If this is less than numBytes,
 *	it indeicates an error.
 *
 * Side effects:
 *	The number of bytes output this time is added to
 *	outputFileBytes.
 *
 *--------------------------------------------------------------
 */
PapyULong WriteJpegData  (PapyUChar *buffer, PapyULong numBytes)
/* Data to write */
/* Number of bytes to write */
{
  PapyULong i;
    
  for (i=0; i<numBytes; i++)
  {
    JPEGBuffer[outputFileBytes] = buffer[i];
    outputFileBytes++;
  }			
  return numBytes;
}

/*
 *--------------------------------------------------------------
 *
 * JPEGLosslessEncodeImage --
 *
 * This is the routine used in the Papyrus Toolkit for Lossless encoder
 *
 * Results
 * JPEGBytes bytes of the JPEG data are written in JPEGInput
 *--------------------------------------------------------------
 */


void JPEGLosslessEncodeImage (PapyUShort *Image, PapyUChar **JPEGInput, PapyULong *JPEGBytes,int nbcols, int nbrows, int depth)
{
    JpegComponentInfo 	*compPtr;
    CompressInfo 	enInfo;
    int 		i, optimize;
    PapyULong 		CountBits, JPEGSize;
    

    if (depth == 8) 
      readimBuffer   = (PapyUChar *) Image;
    else 
      readim16Buffer = (PapyUShort *) Image; 

    optimize = 0;

    /* optimize is a flag used to enable or disabled the psv optimization
     * if optimize flag is on, a default psv (psv =1) is applied
     * ifnot the optimal psv is searched. Warning: such a config. is time consuming!
     */
	
    /*
     * zeroes the memory of the enInfo parameter.
     */
    MEMSET (&enInfo, 0, sizeof (enInfo));
    
    /*
     * default values
     */
    JPEGSize = 0;
    outputFileBytes = 0L;
    enInfo.imageWidth    = nbcols;
    enInfo.imageHeight   = nbrows;
    enInfo.dataPrecision = depth;
    enInfo.restartInRows = 0;
    enInfo.Pt            = 0;
    
    numSelValue = 7;
    for (i = 0; i < numSelValue; i++) {
        psvSet [i] = i + 1;
    }

    /* 
     * Load the mcu if necessary (optimize flag is on) and get ready for encoding.
     */

    PmRead (&enInfo, optimize);
    HuffEncoderInit (&enInfo);

    /*
     * Assign a Huffman table to be used by a component.
     * In non-optimal encoding, all components share one 
     * stardard Huffman table. 
     */
     for (i = 0; i < enInfo.compsInScan; i++) 
     {
       compPtr = enInfo.curCompInfo [i];
       compPtr->dcTblNo = 0;
     } /* for */

     /*
      * Load and prepare the standard Huffman table.
      */ 
     LoadStdHuffTables(&enInfo);
     for (i = 0; i < enInfo.compsInScan; i++) 
     {
       compPtr = enInfo.curCompInfo [i];
       FixHuffTbl ((enInfo.dcHuffTblPtrs) [compPtr->dcTblNo]);
     } /* for */

     /*
      * Apply a default psv if optimize flag is off
      * Select the best PSV using standard Huffman table if optimize flag is on
      */
     if (optimize == 0) enInfo.Ss = 1;
     else 
     {
       StdPickSelValue (&enInfo, (long *) &CountBits);
       JPEGSize = (CountBits >> 3) + HEADERSIZE;
     }
       
    /*
     * Write the frame and scan headers. Encode the image.
     * Clean up everything. 
     */
     
     /* Assign memory for JPEG data. Note that the space is redefined since
      * we do not know the required space before compression 
      */
     if (JPEGSize == 0) 
     {
     	if (enInfo.dataPrecision == 8) 
     	  JPEGSize = enInfo.imageWidth * enInfo.imageHeight;
     	else 
     	  JPEGSize = enInfo.imageWidth * enInfo.imageHeight * 4;
     }
	 
    JPEGBuffer = (PapyUChar *) ecalloc3 ((PapyULong) JPEGSize, (PapyULong) sizeof (PapyUChar));
    
    WriteFileHeader  (&enInfo); 
    WriteScanHeader  (&enInfo);
    HuffEncode 	     (&enInfo, (PapyUShort *) Image);
    /* FreeArray2D      ((char **) &mcuTable); */
    /* efree3((void **) &mcuTable[0]); */
	if (optimize == 1) efree3((void **) &mcuTable); 
    HuffEncoderTerm  ();
    WriteFileTrailer (&enInfo);
    FlushBytes       ();

    efree3 ((void **) &(enInfo.compInfo));
    efree3 ((void **) &(enInfo.dcHuffTblPtrs[0]));
    efree3 ((void **) &(enInfo.dcHuffTblPtrs[1]));

    *JPEGBytes = outputFileBytes;
    *JPEGInput = (PapyUChar *) JPEGBuffer;


} /* endof JPEGLosslessEncodeImage */