comparison dwt/internal/image/JPEGFileFormat.d @ 213:36f5cb12e1a2

Update to SWT 3.4M7
author Frank Benoit <benoit@tionex.de>
date Sat, 17 May 2008 17:34:28 +0200
parents 1801ddeb8f32
children fd9c62a2998e
comparison
equal deleted inserted replaced
212:ab60f3309436 213:36f5cb12e1a2
23 import dwt.internal.image.JPEGRestartInterval; 23 import dwt.internal.image.JPEGRestartInterval;
24 import dwt.internal.image.JPEGQuantizationTable; 24 import dwt.internal.image.JPEGQuantizationTable;
25 import dwt.internal.image.JPEGStartOfImage; 25 import dwt.internal.image.JPEGStartOfImage;
26 import dwt.internal.image.JPEGDecoder; 26 import dwt.internal.image.JPEGDecoder;
27 import dwt.internal.image.JPEGEndOfImage; 27 import dwt.internal.image.JPEGEndOfImage;
28 import dwt.dwthelper.utils;
28 29
29 import dwt.graphics.RGB; 30 import dwt.graphics.RGB;
30 import dwt.graphics.PaletteData; 31 import dwt.graphics.PaletteData;
31 import dwt.dwthelper.System;
32 32
33 import tango.core.Exception; 33 import tango.core.Exception;
34 34
35 final class JPEGFileFormat : FileFormat { 35 final class JPEGFileFormat : FileFormat {
36 int restartInterval; 36 int restartInterval;
148 ]; 148 ];
149 149
150 public static int[] CrRTable, CbBTable, CrGTable, CbGTable; 150 public static int[] CrRTable, CbBTable, CrGTable, CbGTable;
151 public static int[] RYTable, GYTable, BYTable, 151 public static int[] RYTable, GYTable, BYTable,
152 RCbTable, GCbTable, BCbTable, RCrTable, GCrTable, BCrTable, NBitsTable; 152 RCbTable, GCbTable, BCbTable, RCrTable, GCrTable, BCrTable, NBitsTable;
153 153 //public static void static_this() {
154 private static bool static_this_completed = false; 154 static this() {
155 private static void static_this() { 155
156 if( static_this_completed ){ 156 RGB16 = [
157 return; 157 new RGB(0,0,0),
158 } 158 new RGB(0x80,0,0),
159 synchronized { 159 new RGB(0,0x80,0),
160 if( static_this_completed ){ 160 new RGB(0x80,0x80,0),
161 return; 161 new RGB(0,0,0x80),
162 } 162 new RGB(0x80,0,0x80),
163 initialize(); 163 new RGB(0,0x80,0x80),
164 RGB16 = [ 164 new RGB(0xC0,0xC0,0xC0),
165 new RGB(0,0,0), 165 new RGB(0x80,0x80,0x80),
166 new RGB(0x80,0,0), 166 new RGB(0xFF,0,0),
167 new RGB(0,0x80,0), 167 new RGB(0,0xFF,0),
168 new RGB(0x80,0x80,0), 168 new RGB(0xFF,0xFF,0),
169 new RGB(0,0,0x80), 169 new RGB(0,0,0xFF),
170 new RGB(0x80,0,0x80), 170 new RGB(0xFF,0,0xFF),
171 new RGB(0,0x80,0x80), 171 new RGB(0,0xFF,0xFF),
172 new RGB(0xC0,0xC0,0xC0), 172 new RGB(0xFF,0xFF,0xFF)
173 new RGB(0x80,0x80,0x80), 173 ];
174 new RGB(0xFF,0,0), 174 int [] rYTable = new int[256];
175 new RGB(0,0xFF,0), 175 int [] gYTable = new int[256];
176 new RGB(0xFF,0xFF,0), 176 int [] bYTable = new int[256];
177 new RGB(0,0,0xFF), 177 int [] rCbTable = new int[256];
178 new RGB(0xFF,0,0xFF), 178 int [] gCbTable = new int[256];
179 new RGB(0,0xFF,0xFF), 179 int [] bCbTable = new int[256];
180 new RGB(0xFF,0xFF,0xFF) 180 int [] rCrTable = new int[256];
181 ]; 181 int [] gCrTable = new int[256];
182 static_this_completed = true; 182 int [] bCrTable = new int[256];
183 } 183 for (int i = 0; i < 256; i++) {
184 } 184 rYTable[i] = i * 19595;
185 185 gYTable[i] = i * 38470;
186 bYTable[i] = i * 7471 + 32768;
187 rCbTable[i] = i * -11059;
188 gCbTable[i] = i * -21709;
189 bCbTable[i] = i * 32768 + 8388608;
190 gCrTable[i] = i * -27439;
191 bCrTable[i] = i * -5329;
192 }
193 RYTable = rYTable;
194 GYTable = gYTable;
195 BYTable = bYTable;
196 RCbTable = rCbTable;
197 GCbTable = gCbTable;
198 BCbTable = bCbTable;
199 RCrTable = bCbTable;
200 GCrTable = gCrTable;
201 BCrTable = bCrTable;
202
203 /* Initialize YCbCr-RGB Tables */
204 int [] crRTable = new int[256];
205 int [] cbBTable = new int[256];
206 int [] crGTable = new int[256];
207 int [] cbGTable = new int[256];
208 for (int i = 0; i < 256; i++) {
209 int x2 = 2 * i - 255;
210 crRTable[i] = (45941 * x2 + 32768) >> 16;
211 cbBTable[i] = (58065 * x2 + 32768) >> 16;
212 crGTable[i] = -23401 * x2;
213 cbGTable[i] = -11277 * x2 + 32768;
214 }
215 CrRTable = crRTable;
216 CbBTable = cbBTable;
217 CrGTable = crGTable;
218 CbGTable = cbGTable;
219
220 /* Initialize BitCount Table */
221 int nBits = 1;
222 int power2 = 2;
223 int [] nBitsTable = new int[2048];
224 nBitsTable[0] = 0;
225 for (int i = 1; i < nBitsTable.length; i++) {
226 if (!(i < power2)) {
227 nBits++;
228 power2 *= 2;
229 }
230 nBitsTable[i] = nBits;
231 }
232 NBitsTable = nBitsTable;
233 }
186 void compress(ImageData image, byte[] dataYComp, byte[] dataCbComp, byte[] dataCrComp) { 234 void compress(ImageData image, byte[] dataYComp, byte[] dataCbComp, byte[] dataCrComp) {
187 int srcWidth = image.width; 235 int srcWidth = image.width;
188 int srcHeight = image.height; 236 int srcHeight = image.height;
189 int vhFactor = maxV * maxH; 237 int vhFactor = maxV * maxH;
190 int[] frameComponent; 238 int[] frameComponent;
238 int compressedHeight = srcHeight / (maxV / vFactor); 286 int compressedHeight = srcHeight / (maxV / vFactor);
239 if (compressedWidth < componentWidth) { 287 if (compressedWidth < componentWidth) {
240 int delta = componentWidth - compressedWidth; 288 int delta = componentWidth - compressedWidth;
241 for (int yPos = 0; yPos < compressedHeight; yPos++) { 289 for (int yPos = 0; yPos < compressedHeight; yPos++) {
242 int dstOfs = ((yPos + 1) * componentWidth - delta); 290 int dstOfs = ((yPos + 1) * componentWidth - delta);
243 int dataValue = imageComponent[dstOfs - 1] & 0xFF; 291 int dataValue = imageComponent[(dstOfs > 0) ? dstOfs - 1 : 0] & 0xFF;
244 for (int i = 0; i < delta; i++) { 292 for (int i = 0; i < delta; i++) {
245 imageComponent[dstOfs + i] = cast(byte)dataValue; 293 imageComponent[dstOfs + i] = cast(byte)dataValue;
246 } 294 }
247 } 295 }
248 } 296 }
249 if (compressedHeight < componentHeight) { 297 if (compressedHeight < componentHeight) {
250 int srcOfs = (compressedHeight - 1) * componentWidth; 298 int srcOfs = (compressedHeight > 0) ? (compressedHeight - 1) * componentWidth : 1;
251 for (int yPos = compressedHeight; yPos <= componentHeight; yPos++) { 299 for (int yPos = (compressedHeight > 0) ? compressedHeight : 1; yPos <= componentHeight; yPos++) {
252 int dstOfs = (yPos - 1) * componentWidth; 300 int dstOfs = (yPos - 1) * componentWidth;
253 System.arraycopy(imageComponent, srcOfs, imageComponent, dstOfs, componentWidth); 301 System.arraycopy(imageComponent, srcOfs, imageComponent, dstOfs, componentWidth);
254 } 302 }
255 } 303 }
256 } 304 }
1168 JPEGRestartInterval dri = new JPEGRestartInterval(inputStream); 1216 JPEGRestartInterval dri = new JPEGRestartInterval(inputStream);
1169 if (!dri.verify()) { 1217 if (!dri.verify()) {
1170 DWT.error(DWT.ERROR_INVALID_IMAGE); 1218 DWT.error(DWT.ERROR_INVALID_IMAGE);
1171 } 1219 }
1172 restartInterval = dri.getRestartInterval(); 1220 restartInterval = dri.getRestartInterval();
1173 }
1174 static void initialize() {
1175 initializeRGBYCbCrTables();
1176 initializeYCbCrRGBTables();
1177 initializeBitCountTable();
1178 }
1179 static void initializeBitCountTable() {
1180 int nBits = 1;
1181 int power2 = 2;
1182 NBitsTable = new int[2048];
1183 NBitsTable[0] = 0;
1184 for (int i = 1; i < NBitsTable.length; i++) {
1185 if (!(i < power2)) {
1186 nBits++;
1187 power2 *= 2;
1188 }
1189 NBitsTable[i] = nBits;
1190 }
1191 }
1192 static void initializeRGBYCbCrTables() {
1193 RYTable = new int[256];
1194 GYTable = new int[256];
1195 BYTable = new int[256];
1196 RCbTable = new int[256];
1197 GCbTable = new int[256];
1198 BCbTable = new int[256];
1199 RCrTable = BCbTable;
1200 GCrTable = new int[256];
1201 BCrTable = new int[256];
1202 for (int i = 0; i < 256; i++) {
1203 RYTable[i] = i * 19595;
1204 GYTable[i] = i * 38470;
1205 BYTable[i] = i * 7471 + 32768;
1206 RCbTable[i] = i * -11059;
1207 GCbTable[i] = i * -21709;
1208 BCbTable[i] = i * 32768 + 8388608;
1209 GCrTable[i] = i * -27439;
1210 BCrTable[i] = i * -5329;
1211 }
1212 }
1213 static void initializeYCbCrRGBTables() {
1214 CrRTable = new int[256];
1215 CbBTable = new int[256];
1216 CrGTable = new int[256];
1217 CbGTable = new int[256];
1218 for (int i = 0; i < 256; i++) {
1219 int x2 = 2 * i - 255;
1220 CrRTable[i] = (45941 * x2 + 32768) >> 16;
1221 CbBTable[i] = (58065 * x2 + 32768) >> 16;
1222 CrGTable[i] = -23401 * x2;
1223 CbGTable[i] = -11277 * x2 + 32768;
1224 }
1225 } 1221 }
1226 void inverseDCT(int[] dataUnit) { 1222 void inverseDCT(int[] dataUnit) {
1227 for (int row = 0; row < 8; row++) { 1223 for (int row = 0; row < 8; row++) {
1228 int rIndex = row * DCTSIZE; 1224 int rIndex = row * DCTSIZE;
1229 /** 1225 /**