KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Acme > JPM > Encoders > JpegEncoder


1 // JpegEncoder - write out an image as a JPEG
2
//
3
// Copyright (C) 1996 by Jef Poskanzer <jef@acme.com>. All rights reserved.
4
//
5
// Redistribution and use in source and binary forms, with or without
6
// modification, are permitted provided that the following conditions
7
// are met:
8
// 1. Redistributions of source code must retain the above copyright
9
// notice, this list of conditions and the following disclaimer.
10
// 2. Redistributions in binary form must reproduce the above copyright
11
// notice, this list of conditions and the following disclaimer in the
12
// documentation and/or other materials provided with the distribution.
13
//
14
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17
// ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20
// OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24
// SUCH DAMAGE.
25
//
26
// Visit the ACME Labs Java page for up-to-date versions of this and other
27
// fine Java utilities: http://www.acme.com/java/
28

29 package Acme.JPM.Encoders;
30
31 import java.awt.*;
32 import java.awt.image.ImageProducer JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.OutputStream JavaDoc;
35
36 /// Write out an image as a JPEG.
37
// DOESN'T WORK YET.
38
// <P>
39
// <A HREF="../../../../resources/classes/Acme/JPM/Encoders/JpegEncoder.java">Fetch the software.</A><BR>
40
// <A HREF="../../../../resources/classes/Acme.tar.gz">Fetch the entire Acme package.</A>
41
// <P>
42
// @see ToJpeg
43

44 public class JpegEncoder extends ImageEncoder {
45
46     /// Constructor.
47
// @param img The image to encode.
48
// @param out The stream to write the JPEG to.
49
public JpegEncoder(Image img, OutputStream JavaDoc out) throws IOException JavaDoc {
50         super(img, out);
51     }
52
53     /// Constructor.
54
// @param prod The ImageProducer to encode.
55
// @param out The stream to write the JPEG to.
56
public JpegEncoder(ImageProducer JavaDoc prod, OutputStream JavaDoc out) throws IOException JavaDoc {
57         super(prod, out);
58     }
59
60     int qfactor = 100;
61
62     /// Set the Q-factor.
63
public void setQfactor(int qfactor) {
64         this.qfactor = qfactor;
65     }
66
67     int width, height;
68     int[][] rgbPixels;
69
70     void encodeStart(int width, int height) throws IOException JavaDoc {
71         this.width = width;
72         this.height = height;
73         rgbPixels = new int[height][width];
74     }
75
76     void encodePixels(int x, int y, int w, int h, int[] rgbPixels, int off, int scansize)
77             throws IOException JavaDoc {
78         // Save the pixels.
79
for (int row = 0; row < h; ++row)
80             System.arraycopy(rgbPixels, row * scansize + off,
81                     this.rgbPixels[y + row], x, w);
82
83     }
84
85     void encodeDone() throws IOException JavaDoc {
86         writeJfifHuffHeader();
87         // !!!
88
}
89     
90
91     // Some of the following code is derived from the Berkeley Continuous
92
// Media Toolkit (http://bmrc.berkeley.edu/projects/cmt/), which is
93
// Copyright (c) 1996 The Regents of the University of California.
94
// All rights reserved.
95
//
96
// IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
97
// DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING
98
// OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE
99
// UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
100
// DAMAGE.
101
//
102
// THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
103
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
104
// AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
105
// ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION
106
// TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
107

108
109     // This array represents the default JFIF header for quality = 100 and
110
// size = 640x480, with Huffman tables. The values are adjusted when a
111
// file is generated.
112
private static byte[] jfifHuff100Header = {
113         // SOI
114
(byte) 0xFF, (byte) 0xD8,
115
116         // JFIF header
117
(byte) 0xFF, (byte) 0xE0, // Marker
118
(byte) 0x00, (byte) 0x10, // Length = 16 bytes
119
(byte) 0x4A, (byte) 0x46, (byte) 0x49, (byte) 0x46, // "JFIF"
120
(byte) 0x00, (byte) 0x01, (byte) 0x01, (byte) 0x00, (byte) 0x00,
121         (byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00,
122
123         // Start of frame (section B.2.2)
124
(byte) 0xFF, (byte) 0xC0, // Baseline DCT
125
(byte) 0x00, (byte) 0x11, // Length = 17 bytes
126
(byte) 0x08, // Sample precision
127
(byte) 0x01, (byte) 0xE0, // Height
128
(byte) 0x02, (byte) 0x80, // Width
129
(byte) 0x03, // Number of components = 3
130
// Scan 1: 2:1 horiz, (byte) 1:1 vertical, (byte) use QT 0
131
(byte) 0x01, (byte) 0x21, (byte) 0x00,
132         // Scan 2: 1:1 horiz, (byte) 1:1 vertical, (byte) use QT 1
133
(byte) 0x02, (byte) 0x11, (byte) 0x01,
134         // Scan 3: 1:1 horiz, (byte) 1:1 vertical, (byte) use QT 1
135
(byte) 0x03, (byte) 0x11, (byte) 0x01,
136
137         // Define Quant table (section B.2.4.1)
138
(byte) 0xFF, (byte) 0xDB, // Marker
139
(byte) 0x00, (byte) 0x84, // Length (both tables)
140
(byte) 0x00, // 8 bit values, (byte) table 0
141
(byte) 0x10, (byte) 0x0B, (byte) 0x0C, (byte) 0x0E, (byte) 0x0C,
142         (byte) 0x0A, (byte) 0x10, (byte) 0x0E, (byte) 0x0D, (byte) 0x0E,
143         (byte) 0x12, (byte) 0x11, (byte) 0x10, (byte) 0x13, (byte) 0x18,
144         (byte) 0x28, (byte) 0x1A, (byte) 0x18, (byte) 0x16, (byte) 0x16,
145         (byte) 0x18, (byte) 0x31, (byte) 0x23, (byte) 0x25, (byte) 0x1D,
146         (byte) 0x28, (byte) 0x3A, (byte) 0x33, (byte) 0x3D, (byte) 0x3C,
147         (byte) 0x39, (byte) 0x33, (byte) 0x38, (byte) 0x37, (byte) 0x40,
148         (byte) 0x48, (byte) 0x5C, (byte) 0x4E, (byte) 0x40, (byte) 0x44,
149         (byte) 0x57, (byte) 0x45, (byte) 0x37, (byte) 0x38, (byte) 0x50,
150         (byte) 0x6D, (byte) 0x51, (byte) 0x57, (byte) 0x5F, (byte) 0x62,
151         (byte) 0x67, (byte) 0x68, (byte) 0x67, (byte) 0x3E, (byte) 0x4D,
152         (byte) 0x71, (byte) 0x79, (byte) 0x70, (byte) 0x64, (byte) 0x78,
153         (byte) 0x5C, (byte) 0x65, (byte) 0x67, (byte) 0x63,
154
155         (byte) 0x01, // 8 bit values, (byte) table 1
156
(byte) 0x11, (byte) 0x12, (byte) 0x12, (byte) 0x18, (byte) 0x15,
157         (byte) 0x18, (byte) 0x2F, (byte) 0x1A, (byte) 0x1A, (byte) 0x2F,
158         (byte) 0x63, (byte) 0x42, (byte) 0x38, (byte) 0x42, (byte) 0x63,
159         (byte) 0x63, (byte) 0x63, (byte) 0x63, (byte) 0x63, (byte) 0x63,
160         (byte) 0x63, (byte) 0x63, (byte) 0x63, (byte) 0x63, (byte) 0x63,
161         (byte) 0x63, (byte) 0x63, (byte) 0x63, (byte) 0x63, (byte) 0x63,
162         (byte) 0x63, (byte) 0x63, (byte) 0x63, (byte) 0x63, (byte) 0x63,
163         (byte) 0x63, (byte) 0x63, (byte) 0x63, (byte) 0x63, (byte) 0x63,
164         (byte) 0x63, (byte) 0x63, (byte) 0x63, (byte) 0x63, (byte) 0x63,
165         (byte) 0x63, (byte) 0x63, (byte) 0x63, (byte) 0x63, (byte) 0x63,
166         (byte) 0x63, (byte) 0x63, (byte) 0x63, (byte) 0x63, (byte) 0x63,
167         (byte) 0x63, (byte) 0x63, (byte) 0x63, (byte) 0x63, (byte) 0x63,
168         (byte) 0x63, (byte) 0x63, (byte) 0x63, (byte) 0x63,
169
170         // Define huffman table (section B.2.4.1)
171
(byte) 0xFF, (byte) 0xC4, // Marker
172
(byte) 0x00, (byte) 0x1F, // Length (31 bytes)
173
(byte) 0x00, // DC, (byte) table 0
174
(byte) 0x00, (byte) 0x01, (byte) 0x05, (byte) 0x01, (byte) 0x01,
175         (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x00,
176         (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
177         (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03,
178         (byte) 0x04, (byte) 0x05, (byte) 0x06, (byte) 0x07, (byte) 0x08,
179         (byte) 0x09, (byte) 0x0A, (byte) 0x0B,
180
181         // Define huffman table (section B.2.4.1)
182
(byte) 0xFF, (byte) 0xC4, // Marker
183
(byte) 0x00, (byte) 0xB5, // Length (181 bytes)
184
(byte) 0x10, // AC, (byte) table 0
185
(byte) 0x00, (byte) 0x02, (byte) 0x01, (byte) 0x03, (byte) 0x03,
186         (byte) 0x02, (byte) 0x04, (byte) 0x03, (byte) 0x05, (byte) 0x05,
187         (byte) 0x04, (byte) 0x04, (byte) 0x00, (byte) 0x00, (byte) 0x01,
188         (byte) 0x7D, (byte) 0x01, (byte) 0x02, (byte) 0x03, (byte) 0x00,
189         (byte) 0x04, (byte) 0x11, (byte) 0x05, (byte) 0x12, (byte) 0x21,
190         (byte) 0x31, (byte) 0x41, (byte) 0x06, (byte) 0x13, (byte) 0x51,
191         (byte) 0x61, (byte) 0x07, (byte) 0x22, (byte) 0x71, (byte) 0x14,
192         (byte) 0x32, (byte) 0x81, (byte) 0x91, (byte) 0xA1, (byte) 0x08,
193         (byte) 0x23, (byte) 0x42, (byte) 0xB1, (byte) 0xC1, (byte) 0x15,
194         (byte) 0x52, (byte) 0xD1, (byte) 0xF0, (byte) 0x24, (byte) 0x33,
195         (byte) 0x62, (byte) 0x72, (byte) 0x82, (byte) 0x09, (byte) 0x0A,
196         (byte) 0x16, (byte) 0x17, (byte) 0x18, (byte) 0x19, (byte) 0x1A,
197         (byte) 0x25, (byte) 0x26, (byte) 0x27, (byte) 0x28, (byte) 0x29,
198         (byte) 0x2A, (byte) 0x34, (byte) 0x35, (byte) 0x36, (byte) 0x37,
199         (byte) 0x38, (byte) 0x39, (byte) 0x3A, (byte) 0x43, (byte) 0x44,
200         (byte) 0x45, (byte) 0x46, (byte) 0x47, (byte) 0x48, (byte) 0x49,
201         (byte) 0x4A, (byte) 0x53, (byte) 0x54, (byte) 0x55, (byte) 0x56,
202         (byte) 0x57, (byte) 0x58, (byte) 0x59, (byte) 0x5A, (byte) 0x63,
203         (byte) 0x64, (byte) 0x65, (byte) 0x66, (byte) 0x67, (byte) 0x68,
204         (byte) 0x69, (byte) 0x6A, (byte) 0x73, (byte) 0x74, (byte) 0x75,
205         (byte) 0x76, (byte) 0x77, (byte) 0x78, (byte) 0x79, (byte) 0x7A,
206         (byte) 0x83, (byte) 0x84, (byte) 0x85, (byte) 0x86, (byte) 0x87,
207         (byte) 0x88, (byte) 0x89, (byte) 0x8A, (byte) 0x92, (byte) 0x93,
208         (byte) 0x94, (byte) 0x95, (byte) 0x96, (byte) 0x97, (byte) 0x98,
209         (byte) 0x99, (byte) 0x9A, (byte) 0xA2, (byte) 0xA3, (byte) 0xA4,
210         (byte) 0xA5, (byte) 0xA6, (byte) 0xA7, (byte) 0xA8, (byte) 0xA9,
211         (byte) 0xAA, (byte) 0xB2, (byte) 0xB3, (byte) 0xB4, (byte) 0xB5,
212         (byte) 0xB6, (byte) 0xB7, (byte) 0xB8, (byte) 0xB9, (byte) 0xBA,
213         (byte) 0xC2, (byte) 0xC3, (byte) 0xC4, (byte) 0xC5, (byte) 0xC6,
214         (byte) 0xC7, (byte) 0xC8, (byte) 0xC9, (byte) 0xCA, (byte) 0xD2,
215         (byte) 0xD3, (byte) 0xD4, (byte) 0xD5, (byte) 0xD6, (byte) 0xD7,
216         (byte) 0xD8, (byte) 0xD9, (byte) 0xDA, (byte) 0xE1, (byte) 0xE2,
217         (byte) 0xE3, (byte) 0xE4, (byte) 0xE5, (byte) 0xE6, (byte) 0xE7,
218         (byte) 0xE8, (byte) 0xE9, (byte) 0xEA, (byte) 0xF1, (byte) 0xF2,
219         (byte) 0xF3, (byte) 0xF4, (byte) 0xF5, (byte) 0xF6, (byte) 0xF7,
220         (byte) 0xF8, (byte) 0xF9, (byte) 0xFA,
221
222         // Define huffman table (section B.2.4.1)
223
(byte) 0xFF, (byte) 0xC4, // Marker
224
(byte) 0x00, (byte) 0x1F, // Length (31 bytes)
225
(byte) 0x01, // DC, (byte) table 1
226
(byte) 0x00, (byte) 0x03, (byte) 0x01, (byte) 0x01, (byte) 0x01,
227         (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01,
228         (byte) 0x01, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
229         (byte) 0x00, (byte) 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03,
230         (byte) 0x04, (byte) 0x05, (byte) 0x06, (byte) 0x07, (byte) 0x08,
231         (byte) 0x09, (byte) 0x0A, (byte) 0x0B,
232
233         // Define huffman table (section B.2.4.1)
234
(byte) 0xFF, (byte) 0xC4, // Marker
235
(byte) 0x00, (byte) 0xB5, // Length (181 bytes)
236
(byte) 0x11, // AC, (byte) table 1
237
(byte) 0x00, (byte) 0x02, (byte) 0x01, (byte) 0x02, (byte) 0x04,
238         (byte) 0x04, (byte) 0x03, (byte) 0x04, (byte) 0x07, (byte) 0x05,
239         (byte) 0x04, (byte) 0x04, (byte) 0x00, (byte) 0x01, (byte) 0x02,
240         (byte) 0x77, (byte) 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03,
241         (byte) 0x11, (byte) 0x04, (byte) 0x05, (byte) 0x21, (byte) 0x31,
242         (byte) 0x06, (byte) 0x12, (byte) 0x41, (byte) 0x51, (byte) 0x07,
243         (byte) 0x61, (byte) 0x71, (byte) 0x13, (byte) 0x22, (byte) 0x32,
244         (byte) 0x81, (byte) 0x08, (byte) 0x14, (byte) 0x42, (byte) 0x91,
245         (byte) 0xA1, (byte) 0xB1, (byte) 0xC1, (byte) 0x09, (byte) 0x23,
246         (byte) 0x33, (byte) 0x52, (byte) 0xF0, (byte) 0x15, (byte) 0x62,
247         (byte) 0x72, (byte) 0xD1, (byte) 0x0A, (byte) 0x16, (byte) 0x24,
248         (byte) 0x34, (byte) 0xE1, (byte) 0x25, (byte) 0xF1, (byte) 0x17,
249         (byte) 0x18, (byte) 0x19, (byte) 0x1A, (byte) 0x26, (byte) 0x27,
250         (byte) 0x28, (byte) 0x29, (byte) 0x2A, (byte) 0x35, (byte) 0x36,
251         (byte) 0x37, (byte) 0x38, (byte) 0x39, (byte) 0x3A, (byte) 0x43,
252         (byte) 0x44, (byte) 0x45, (byte) 0x46, (byte) 0x47, (byte) 0x48,
253         (byte) 0x49, (byte) 0x4A, (byte) 0x53, (byte) 0x54, (byte) 0x55,
254         (byte) 0x56, (byte) 0x57, (byte) 0x58, (byte) 0x59, (byte) 0x5A,
255         (byte) 0x63, (byte) 0x64, (byte) 0x65, (byte) 0x66, (byte) 0x67,
256         (byte) 0x68, (byte) 0x69, (byte) 0x6A, (byte) 0x73, (byte) 0x74,
257         (byte) 0x75, (byte) 0x76, (byte) 0x77, (byte) 0x78, (byte) 0x79,
258         (byte) 0x7A, (byte) 0x82, (byte) 0x83, (byte) 0x84, (byte) 0x85,
259         (byte) 0x86, (byte) 0x87, (byte) 0x88, (byte) 0x89, (byte) 0x8A,
260         (byte) 0x92, (byte) 0x93, (byte) 0x94, (byte) 0x95, (byte) 0x96,
261         (byte) 0x97, (byte) 0x98, (byte) 0x99, (byte) 0x9A, (byte) 0xA2,
262         (byte) 0xA3, (byte) 0xA4, (byte) 0xA5, (byte) 0xA6, (byte) 0xA7,
263         (byte) 0xA8, (byte) 0xA9, (byte) 0xAA, (byte) 0xB2, (byte) 0xB3,
264         (byte) 0xB4, (byte) 0xB5, (byte) 0xB6, (byte) 0xB7, (byte) 0xB8,
265         (byte) 0xB9, (byte) 0xBA, (byte) 0xC2, (byte) 0xC3, (byte) 0xC4,
266         (byte) 0xC5, (byte) 0xC6, (byte) 0xC7, (byte) 0xC8, (byte) 0xC9,
267         (byte) 0xCA, (byte) 0xD2, (byte) 0xD3, (byte) 0xD4, (byte) 0xD5,
268         (byte) 0xD6, (byte) 0xD7, (byte) 0xD8, (byte) 0xD9, (byte) 0xDA,
269         (byte) 0xE2, (byte) 0xE3, (byte) 0xE4, (byte) 0xE5, (byte) 0xE6,
270         (byte) 0xE7, (byte) 0xE8, (byte) 0xE9, (byte) 0xEA, (byte) 0xF2,
271         (byte) 0xF3, (byte) 0xF4, (byte) 0xF5, (byte) 0xF6, (byte) 0xF7,
272         (byte) 0xF8, (byte) 0xF9, (byte) 0xFA,
273
274         // Start of Scan (section B.2.3)
275
(byte) 0xFF, (byte) 0xDA, // Marker
276
(byte) 0x00, (byte) 0x0C, // Length of header
277
(byte) 0x03, // Number of image components
278
// Scan 1: use DC/AC huff tables 0/0
279
(byte) 0x01, (byte) 0x00,
280         // Scan 2: use DC/AC huff tables 1/1
281
(byte) 0x02, (byte) 0x11,
282         // Scan 3: use DC/AC huff tables 1/1
283
(byte) 0x03, (byte) 0x11,
284         (byte) 0x00, (byte) 0x3F, (byte) 0x00 // Not used
285
};
286
287     // This array represents the default JFIF header for quality = 100 and
288
// size = 640x480, without Huffman tables. The values are adjusted when a
289
// file is generated.
290
private static byte[] jfifNoHuff100Header = {
291         // SOI
292
(byte) 0xFF, (byte) 0xD8,
293
294         // JFIF header
295
(byte) 0xFF, (byte) 0xE0, // Marker
296
(byte) 0x00, (byte) 0x10, // Length = 16 bytes
297
(byte) 0x4A, (byte) 0x46, (byte) 0x49, (byte) 0x46, // "JFIF"
298
(byte) 0x00, (byte) 0x01, (byte) 0x01, (byte) 0x00, (byte) 0x00,
299         (byte) 0x01, (byte) 0x00, (byte) 0x01, (byte) 0x00, (byte) 0x00,
300
301         // Start of frame (section B.2.2)
302
(byte) 0xFF, (byte) 0xC0, // Baseline DCT
303
(byte) 0x00, (byte) 0x11, // Length = 17 bytes
304
(byte) 0x08, // Sample precision
305
(byte) 0x01, (byte) 0xE0, // Height
306
(byte) 0x02, (byte) 0x80, // Width
307
(byte) 0x03, // Number of components = 3
308
// Scan 1: 2:1 horiz, (byte) 1:1 vertical, (byte) use QT 0
309
(byte) 0x01, (byte) 0x21, (byte) 0x00,
310         // Scan 2: 1:1 horiz, (byte) 1:1 vertical, (byte) use QT 1
311
(byte) 0x02, (byte) 0x11, (byte) 0x01,
312         // Scan 3: 1:1 horiz, (byte) 1:1 vertical, (byte) use QT 1
313
(byte) 0x03, (byte) 0x11, (byte) 0x01,
314
315         // Define Quant table (section B.2.4.1)
316
(byte) 0xFF, (byte) 0xDB, // Marker
317
(byte) 0x00, (byte) 0x84, // Length (both tables)
318
(byte) 0x00, // 8 bit values, (byte) table 0
319
(byte) 0x10, (byte) 0x0B, (byte) 0x0C, (byte) 0x0E, (byte) 0x0C,
320         (byte) 0x0A, (byte) 0x10, (byte) 0x0E, (byte) 0x0D, (byte) 0x0E,
321         (byte) 0x12, (byte) 0x11, (byte) 0x10, (byte) 0x13, (byte) 0x18,
322         (byte) 0x28, (byte) 0x1A, (byte) 0x18, (byte) 0x16, (byte) 0x16,
323         (byte) 0x18, (byte) 0x31, (byte) 0x23, (byte) 0x25, (byte) 0x1D,
324         (byte) 0x28, (byte) 0x3A, (byte) 0x33, (byte) 0x3D, (byte) 0x3C,
325         (byte) 0x39, (byte) 0x33, (byte) 0x38, (byte) 0x37, (byte) 0x40,
326         (byte) 0x48, (byte) 0x5C, (byte) 0x4E, (byte) 0x40, (byte) 0x44,
327         (byte) 0x57, (byte) 0x45, (byte) 0x37, (byte) 0x38, (byte) 0x50,
328         (byte) 0x6D, (byte) 0x51, (byte) 0x57, (byte) 0x5F, (byte) 0x62,
329         (byte) 0x67, (byte) 0x68, (byte) 0x67, (byte) 0x3E, (byte) 0x4D,
330         (byte) 0x71, (byte) 0x79, (byte) 0x70, (byte) 0x64, (byte) 0x78,
331         (byte) 0x5C, (byte) 0x65, (byte) 0x67, (byte) 0x63,
332
333         (byte) 0x01, // 8 bit values, (byte) table 1
334
(byte) 0x11, (byte) 0x12, (byte) 0x12, (byte) 0x18, (byte) 0x15,
335         (byte) 0x18, (byte) 0x2F, (byte) 0x1A, (byte) 0x1A, (byte) 0x2F,
336         (byte) 0x63, (byte) 0x42, (byte) 0x38, (byte) 0x42, (byte) 0x63,
337         (byte) 0x63, (byte) 0x63, (byte) 0x63, (byte) 0x63, (byte) 0x63,
338         (byte) 0x63, (byte) 0x63, (byte) 0x63, (byte) 0x63, (byte) 0x63,
339         (byte) 0x63, (byte) 0x63, (byte) 0x63, (byte) 0x63, (byte) 0x63,
340         (byte) 0x63, (byte) 0x63, (byte) 0x63, (byte) 0x63, (byte) 0x63,
341         (byte) 0x63, (byte) 0x63, (byte) 0x63, (byte) 0x63, (byte) 0x63,
342         (byte) 0x63, (byte) 0x63, (byte) 0x63, (byte) 0x63, (byte) 0x63,
343         (byte) 0x63, (byte) 0x63, (byte) 0x63, (byte) 0x63, (byte) 0x63,
344         (byte) 0x63, (byte) 0x63, (byte) 0x63, (byte) 0x63, (byte) 0x63,
345         (byte) 0x63, (byte) 0x63, (byte) 0x63, (byte) 0x63, (byte) 0x63,
346         (byte) 0x63, (byte) 0x63, (byte) 0x63, (byte) 0x63,
347
348         // Start of Scan (section B.2.3)
349
(byte) 0xFF, (byte) 0xDA, // Marker
350
(byte) 0x00, (byte) 0x0C, // Length of header
351
(byte) 0x03, // Number of image components
352
// Scan 1: use DC/AC huff tables 0/0
353
(byte) 0x01, (byte) 0x00,
354         // Scan 2: use DC/AC huff tables 1/1
355
(byte) 0x02, (byte) 0x11,
356         // Scan 3: use DC/AC huff tables 1/1
357
(byte) 0x03, (byte) 0x11,
358         (byte) 0x00, (byte) 0x3F, (byte) 0x00 // Not used
359
};
360
361     private void writeJfifHuffHeader() throws IOException JavaDoc {
362         byte[] newHeader = new byte[jfifHuff100Header.length];
363
364         System.arraycopy(jfifHuff100Header, 0, newHeader, 0, jfifHuff100Header.length);
365
366         // Set image width in JFIF header.
367
newHeader[27] = (byte) ((width >>> 8) & 0xff);
368         newHeader[28] = (byte) (width & 0xff);
369
370         // Set image height in JFIF header.
371
newHeader[25] = (byte) ((height >>> 8) & 0xff);
372         newHeader[26] = (byte) (height & 0xff);
373
374         // Adjust the quality factor.
375
//
376
// The default quality factor is 100, therefore if
377
// our quality factor does not equal 100 we must
378
// scale the quantization matrices in the JFIF header.
379
// Note that values are clipped to a max of 255.
380
if (qfactor != 100) {
381             for (int i = 44; i < 108; ++i) {
382                 int t = (newHeader[i] * qfactor) / 100;
383                 newHeader[i] = (byte) Math.max(t, 0xff);
384             }
385             for (int i = 109; i < 173; ++i) {
386                 int t = (newHeader[i] * qfactor) / 100;
387                 newHeader[i] = (byte) Math.max(t, 0xff);
388             }
389         }
390
391         // Write out buffer.
392
out.write(newHeader);
393     }
394
395     private void writeJfifNoHuffHeader() throws IOException JavaDoc {
396         byte[] newHeader = new byte[jfifNoHuff100Header.length];
397
398         System.arraycopy(jfifNoHuff100Header, 0, newHeader, 0, jfifNoHuff100Header.length);
399
400         // Set image width in JFIF header.
401
newHeader[27] = (byte) ((width >>> 8) & 0xff);
402         newHeader[28] = (byte) (width & 0xff);
403
404         // Set image height in JFIF header.
405
newHeader[25] = (byte) ((height >>> 8) & 0xff);
406         newHeader[26] = (byte) (height & 0xff);
407
408         // Adjust the quality factor.
409
//
410
// The default quality factor is 100, therefore if
411
// our quality factor does not equal 100 we must
412
// scale the quantization matrices in the JFIF header.
413
// Note that values are clipped to a max of 255.
414
if (qfactor != 100) {
415             for (int i = 44; i < 108; ++i) {
416                 int t = (newHeader[i] * qfactor) / 100;
417                 newHeader[i] = (byte) Math.max(t, 0xff);
418             }
419             for (int i = 109; i < 173; ++i) {
420                 int t = (newHeader[i] * qfactor) / 100;
421                 newHeader[i] = (byte) Math.max(t, 0xff);
422             }
423         }
424
425         // Write out buffer.
426
out.write(newHeader);
427     }
428
429 }
430
Popular Tags