File: WRITE.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 (369 lines) | stat: -rwxr-xr-x 8,769 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
 * write.c --
 *
 * Code for writing JPEG files.  Large parts are grabbed
 * from the IJG software, so:
 *
 * Copyright (C) 1991, 1992, Thomas G. Lane.
 * Part of the Independent JPEG Group's software.
 * See the file Copyright for more details.
 *
 * Copyright (c) 1993 Brian C. Smith, The Regents of the University
 * of California
 * All rights reserved.
 * 
 * Copyright (c) 1994 Kongji Huang and Brian C. Smith.
 * Cornell University
 * 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 <malloc.h>
*/
#include <string.h>
#include "JPEG.H"
#include "MCU.H"
#include "IO.H"
#include "PROTO.H"

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

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

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

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

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

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

#endif

/* 
 * Enumerate all the JPEG marker codes
 */
typedef enum {
	M_SOF0 = 0xc0,
	M_SOF1 = 0xc1,
	M_SOF2 = 0xc2,
	M_SOF3 = 0xc3,
	M_SOF5 = 0xc5,
	M_SOF6 = 0xc6,
	M_SOF7 = 0xc7,
	M_JPG = 0xc8,
	M_SOF9 = 0xc9,
	M_SOF10 = 0xca,
	M_SOF11 = 0xcb,
	M_SOF13 = 0xcd,
	M_SOF14 = 0xce,
	M_SOF15 = 0xcf,
		M_DHT = 0xc4,
		M_DAC = 0xcc,
		M_RST0 = 0xd0,
		M_RST1 = 0xd1,
		M_RST2 = 0xd2,
		M_RST3 = 0xd3,
		M_RST4 = 0xd4,
		M_RST5 = 0xd5,
		M_RST6 = 0xd6,
		M_RST7 = 0xd7,
		M_SOI = 0xd8,
		M_EOI = 0xd9,
		M_SOS = 0xda,
		M_DQT = 0xdb,
		M_DNL = 0xdc,
		M_DRI = 0xdd,
		M_DHP = 0xde,
		M_EXP = 0xdf,
		M_APP0 = 0xe0,
		M_APP15 = 0xef,
		M_JPG0 = 0xf0,
		M_JPG13 = 0xfd,
		M_COM = 0xfe,
		M_TEM = 0x01,
		M_ERROR = 0x100
} JpegMarker;
/*
*--------------------------------------------------------------
 *
 * EmitMarker --
 *
 *		  Emit a marker code into the output stream.
 *
 * Results:
 *		  None.
 *
 * Side effects:
 *		  None.
 *
*--------------------------------------------------------------
 */
static void
EmitMarker (JpegMarker mark)
{
	EmitByte (0xFF);
	EmitByte (mark);
}
/*
*--------------------------------------------------------------
 *
 * Emit2bytes --
 *
 *		  Emit a 2-byte integer; these are always MSB first in JPEG
 *		  files
 *
 * Results:
 *			None.
 *
 * Side effects:
 *			None.
 *
*--------------------------------------------------------------
 */
static void
Emit2bytes (int value)
{
	EmitByte ((value >> 8) & 0xFF);
	EmitByte (value & 0xFF);
}
/*
*--------------------------------------------------------------
 *
 * EmitDht --
 *
 *			Emit a DHT marker, follwed by the huffman data.
 *
 * Results:
 *			None
 *
 * Side effects:
 *			None
 *
*--------------------------------------------------------------
 */
static void
EmitDht (CompressInfo *cPtr, int index, int isAc)
{
	HuffmanTable *htbl;
	int length, i;
	if (isAc) {
		/* printf("Not a huffman table for lossless mode\n"); */
	} else {
			htbl = cPtr->dcHuffTblPtrs[index];
	}
	if (htbl == NULL) {
		/* printf ("Huffman table 0x%02x was not defined\n", index); exit (1); */
	}
	if (!htbl->sentTable) {
			EmitMarker (M_DHT);
			length = 0;
			for (i = 1; i <= 16; i++)
				length += htbl->bits[i];
Emit2bytes (length + 2 + 1 + 16); EmitByte (index);
for (i = 1; i <= 16; i++) EmitByte (htbl->bits[i]);
for (i = 0; i < length; i++) EmitByte (htbl->huffval[i]);
				htbl->sentTable = 1;
	}
}
/*
*--------------------------------------------------------------
 *
 * EmitDri --
 *
 *				Emit a DRI marker
 *
 * Results:
 *				None.
 *
 * Side effects:
 *				Exit on too big restart interval.
 *
*--------------------------------------------------------------
 */
static void
EmitDri (CompressInfo *cPtr)
{
	int restartInterval;
restartInterval = cPtr->restartInRows * cPtr->imageWidth;
	/*
* DIS only specifies 16 bits to store this value, so
		*/
	if (restartInterval > 65535) {
		/* printf("Error: Restart interval is too big.\n"); 
		printf("It should be less than %d rows.\n", 65535/cPtr->imageWidth); */
			return; /* MAL exit(1); */
	}
	EmitMarker (M_DRI);
	Emit2bytes (4);     /* length */
	Emit2bytes ((int)restartInterval);
}
/*
*--------------------------------------------------------------
 *
 * EmitSof --
 *
 *				Emit a SOF marker plus data.
 *
 * Results:
 *				None.
 *
 * Side effects:
 *				None.
 *
*--------------------------------------------------------------
 */
static void
EmitSof (CompressInfo *cPtr, JpegMarker code)
{
	int i;
	EmitMarker (code);
	Emit2bytes (3 * cPtr->numComponents + 2 + 5 + 1);   /* length */
	if ((cPtr->imageHeight > 65535) ||
		(cPtr->imageWidth > 65535)) {
		/* printf ("Maximum image dimension for JFIF is 65535 pixels\n"); */
		return; /* MAL exit(1); */
	}
	EmitByte (cPtr->dataPrecision);
	Emit2bytes ((int)cPtr->imageHeight);
	Emit2bytes ((int)cPtr->imageWidth);
	EmitByte (cPtr->numComponents);
	for (i = 0; i < cPtr->numComponents; i++) {
EmitByte (cPtr->compInfo[i].componentId);
EmitByte ((cPtr->compInfo[i].hSampFactor << 4) + cPtr->compInfo[i].vSampFactor);
		EmitByte (0);   /* Tq shall be 0 for lossless */
	}
}
/*
 *--------------------------------------------------------------
 *
 * EmitSos --
 *
 *		Emit a SOS marker plus data.
 *
 * Results:
 *		None.
 *
 * Side effects:
 *		None.
 *
 *--------------------------------------------------------------
 */
static void
EmitSos (CompressInfo *cPtr)
{
	int i;
	EmitMarker (M_SOS);
Emit2bytes (2*cPtr->compsInScan + 2 + 1 + 3);       /* length */ 
EmitByte (cPtr->compsInScan);                       /* Ns     */ 
for (i = 0; i < cPtr->compsInScan; i++) {           /* Cs,Td,Ta */ 
EmitByte (cPtr->curCompInfo[i]->componentId);
		EmitByte ((cPtr->curCompInfo[i]->dcTblNo << 4));
	}
	EmitByte (cPtr->Ss);        /* the PSV */
EmitByte (0);               /* Spectral selection end  - Se */ 
EmitByte(cPtr->Pt & 0x0F);  /* the point transform parameter */  
}
/*
 *--------------------------------------------------------------
 *
 * WriteFileTrailer --
 *
 *		Write the End of image marker at the end of a JPEG file.
 *
 *			XXX: This is hardwored into stdout.
 *
 * Results:
 *			None.
 *
 * Side effects:
 *			None.
 *
 *--------------------------------------------------------------
 */
void
WriteFileTrailer (CompressInfo *cPtr)
{
	EmitMarker (M_EOI);
}
/*
 *--------------------------------------------------------------
 *
 * WriteScanHeader --
 *
 *			Write the start of a scan (everything through the SOS marker).
 *
 * Results:
 *			None.
 *
 * Side effects:
 *			None.
 *
 *--------------------------------------------------------------
 */
void
WriteScanHeader (CompressInfo *cPtr)
{
	int i;
	/*
		* Emit Huffman tables.  Note that EmitDht takes care of
		* suppressing duplicate tables.
		*/
	for (i = 0; i < cPtr->compsInScan; i++) {
			EmitDht (cPtr, cPtr->curCompInfo[i]->dcTblNo, 0);
	}
	/*
* Emit DRI if required --- note that DRI value could change for each * scan. If it doesn't, a tiny amount of space is wasted in
* multiple-scan files. We assume DRI will never be nonzero for one * scan and zero for a later one.
		*/
	if (cPtr->restartInRows)
			EmitDri (cPtr);
	EmitSos (cPtr);
}
/*
 *--------------------------------------------------------------
 *
 * WriteFileHeader --
 *
 *			Write the file header.
 *
 * Results:
 *		None.
 *
 * Side effects:
 *		None.
 *
*--------------------------------------------------------------
 */
void
WriteFileHeader (CompressInfo *cPtr)
{
	EmitMarker (M_SOI); /* first the SOI */
	EmitSof (cPtr, M_SOF3);
}