Mercurial > projects > dwt-mac
diff dwt/internal/image/JPEGFileFormat.d @ 34:5123b17c98ef
Ported dwt.events.*, dwt.graphics.GC, Region, dwt.internal.image.*
author | Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com> |
---|---|
date | Sun, 14 Sep 2008 01:45:57 +0200 |
parents | e831403a80a9 |
children | d8635bb48c7c |
line wrap: on
line diff
--- a/dwt/internal/image/JPEGFileFormat.d Fri Sep 12 13:53:21 2008 +0200 +++ b/dwt/internal/image/JPEGFileFormat.d Sun Sep 14 01:45:57 2008 +0200 @@ -1,25 +1,38 @@ /******************************************************************************* - * Copyright (c) 2000, 2006 IBM Corporation and others. + * Copyright (c) 2000, 2008 IBM Corporation and others. * All rights reserved. This source file is made available under the terms contained in the README file * accompanying this program. The README file should be located in the about_files directory of the * plug-in that contains this source file. * * Contributors: * IBM Corporation - initial API and implementation + * Port to the D programming language: + * Frank Benoit <benoit@tionex.de> *******************************************************************************/ -module dwt.internal.image; - - -import java.io.IOException; +module dwt.internal.image.JPEGFileFormat; import dwt.DWT; -import dwt.graphics.ImageData; -import dwt.graphics.ImageLoader; -import dwt.graphics.ImageLoaderEvent; +import dwt.internal.image.JPEGFrameHeader; +import dwt.internal.image.JPEGScanHeader; +import dwt.internal.image.JPEGHuffmanTable; +import dwt.internal.image.JPEGAppn; +import dwt.internal.image.JPEGSegment; +import dwt.internal.image.FileFormat; +import dwt.internal.image.JPEGComment; +import dwt.internal.image.JPEGArithmeticConditioningTable; +import dwt.internal.image.JPEGRestartInterval; +import dwt.internal.image.JPEGQuantizationTable; +import dwt.internal.image.JPEGStartOfImage; +import dwt.internal.image.JPEGDecoder; +import dwt.internal.image.JPEGEndOfImage; +import dwt.dwthelper.utils; + +import dwt.graphics.RGB; import dwt.graphics.PaletteData; -import dwt.graphics.RGB; -public final class JPEGFileFormat : FileFormat { +import tango.core.Exception; + +final class JPEGFileFormat : FileFormat { int restartInterval; JPEGFrameHeader frameHeader; int imageWidth, imageHeight; @@ -47,100 +60,83 @@ int encoderQFactor = 75; int eobrun = 0; /* JPEGConstants */ - public static final int DCTSIZE = 8; - public static final int DCTSIZESQR = 64; + public static const int DCTSIZE = 8; + public static const int DCTSIZESQR = 64; /* JPEGFixedPointConstants */ - public static final int FIX_0_899976223 = 7373; - public static final int FIX_1_961570560 = 16069; - public static final int FIX_2_053119869 = 16819; - public static final int FIX_0_298631336 = 2446; - public static final int FIX_1_847759065 = 15137; - public static final int FIX_1_175875602 = 9633; - public static final int FIX_3_072711026 = 25172; - public static final int FIX_0_765366865 = 6270; - public static final int FIX_2_562915447 = 20995; - public static final int FIX_0_541196100 = 4433; - public static final int FIX_0_390180644 = 3196; - public static final int FIX_1_501321110 = 12299; + public static const int FIX_0_899976223 = 7373; + public static const int FIX_1_961570560 = 16069; + public static const int FIX_2_053119869 = 16819; + public static const int FIX_0_298631336 = 2446; + public static const int FIX_1_847759065 = 15137; + public static const int FIX_1_175875602 = 9633; + public static const int FIX_3_072711026 = 25172; + public static const int FIX_0_765366865 = 6270; + public static const int FIX_2_562915447 = 20995; + public static const int FIX_0_541196100 = 4433; + public static const int FIX_0_390180644 = 3196; + public static const int FIX_1_501321110 = 12299; /* JPEGMarkerCodes */ - public static final int APP0 = 0xFFE0; - public static final int APP15 = 0xFFEF; - public static final int COM = 0xFFFE; - public static final int DAC = 0xFFCC; - public static final int DHP = 0xFFDE; - public static final int DHT = 0xFFC4; - public static final int DNL = 0xFFDC; - public static final int DRI = 0xFFDD; - public static final int DQT = 0xFFDB; - public static final int EOI = 0xFFD9; - public static final int EXP = 0xFFDF; - public static final int JPG = 0xFFC8; - public static final int JPG0 = 0xFFF0; - public static final int JPG13 = 0xFFFD; - public static final int RST0 = 0xFFD0; - public static final int RST1 = 0xFFD1; - public static final int RST2 = 0xFFD2; - public static final int RST3 = 0xFFD3; - public static final int RST4 = 0xFFD4; - public static final int RST5 = 0xFFD5; - public static final int RST6 = 0xFFD6; - public static final int RST7 = 0xFFD7; - public static final int SOF0 = 0xFFC0; - public static final int SOF1 = 0xFFC1; - public static final int SOF2 = 0xFFC2; - public static final int SOF3 = 0xFFC3; - public static final int SOF5 = 0xFFC5; - public static final int SOF6 = 0xFFC6; - public static final int SOF7 = 0xFFC7; - public static final int SOF9 = 0xFFC9; - public static final int SOF10 = 0xFFCA; - public static final int SOF11 = 0xFFCB; - public static final int SOF13 = 0xFFCD; - public static final int SOF14 = 0xFFCE; - public static final int SOF15 = 0xFFCF; - public static final int SOI = 0xFFD8; - public static final int SOS = 0xFFDA; - public static final int TEM = 0xFF01; + public static const int APP0 = 0xFFE0; + public static const int APP15 = 0xFFEF; + public static const int COM = 0xFFFE; + public static const int DAC = 0xFFCC; + public static const int DHP = 0xFFDE; + public static const int DHT = 0xFFC4; + public static const int DNL = 0xFFDC; + public static const int DRI = 0xFFDD; + public static const int DQT = 0xFFDB; + public static const int EOI = 0xFFD9; + public static const int EXP = 0xFFDF; + public static const int JPG = 0xFFC8; + public static const int JPG0 = 0xFFF0; + public static const int JPG13 = 0xFFFD; + public static const int RST0 = 0xFFD0; + public static const int RST1 = 0xFFD1; + public static const int RST2 = 0xFFD2; + public static const int RST3 = 0xFFD3; + public static const int RST4 = 0xFFD4; + public static const int RST5 = 0xFFD5; + public static const int RST6 = 0xFFD6; + public static const int RST7 = 0xFFD7; + public static const int SOF0 = 0xFFC0; + public static const int SOF1 = 0xFFC1; + public static const int SOF2 = 0xFFC2; + public static const int SOF3 = 0xFFC3; + public static const int SOF5 = 0xFFC5; + public static const int SOF6 = 0xFFC6; + public static const int SOF7 = 0xFFC7; + public static const int SOF9 = 0xFFC9; + public static const int SOF10 = 0xFFCA; + public static const int SOF11 = 0xFFCB; + public static const int SOF13 = 0xFFCD; + public static const int SOF14 = 0xFFCE; + public static const int SOF15 = 0xFFCF; + public static const int SOI = 0xFFD8; + public static const int SOS = 0xFFDA; + public static const int TEM = 0xFF01; /* JPEGFrameComponentParameterConstants */ - public static final int TQI = 0; - public static final int HI = 1; - public static final int VI = 2; - public static final int CW = 3; - public static final int CH = 4; + public static const int TQI = 0; + public static const int HI = 1; + public static const int VI = 2; + public static const int CW = 3; + public static const int CH = 4; /* JPEGScanComponentParameterConstants */ - public static final int DC = 0; - public static final int AC = 1; + public static const int DC = 0; + public static const int AC = 1; /* JFIF Component Constants */ - public static final int ID_Y = 1 - 1; - public static final int ID_CB = 2 - 1; - public static final int ID_CR = 3 - 1; - public static final RGB[] RGB16 = new RGB[] { - new RGB(0,0,0), - new RGB(0x80,0,0), - new RGB(0,0x80,0), - new RGB(0x80,0x80,0), - new RGB(0,0,0x80), - new RGB(0x80,0,0x80), - new RGB(0,0x80,0x80), - new RGB(0xC0,0xC0,0xC0), - new RGB(0x80,0x80,0x80), - new RGB(0xFF,0,0), - new RGB(0,0xFF,0), - new RGB(0xFF,0xFF,0), - new RGB(0,0,0xFF), - new RGB(0xFF,0,0xFF), - new RGB(0,0xFF,0xFF), - new RGB(0xFF,0xFF,0xFF), - }; - public static final int[] ExtendTest = { - 0, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, + public static const int ID_Y = 1 - 1; + public static const int ID_CB = 2 - 1; + public static const int ID_CR = 3 - 1; + public static /*const*/ RGB[] RGB16; + public static const int[] ExtendTest = [ + 0, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144 - }; - public static final int[] ExtendOffset = new int[] { - 0, -1, -3, -7, -15, -31, -63, -127, -255, -511, -1023, -2047, + ]; + public static const int[] ExtendOffset = [ + 0, -1, -3, -7, -15, -31, -63, -127, -255, -511, -1023, -2047, -4095, -8191, -16383, -32767, -65535, -131071, -262143 - }; - public static final int[] ZigZag8x8 = { + ]; + public static const int[] ZigZag8x8 = [ 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32, 25, 18, 11, 4, 5, 12, 19, 26, 33, 40, 48, 41, 34, @@ -149,19 +145,39 @@ 29, 22, 15, 23, 30, 37, 44, 51, 58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63 - }; + ]; + + public static int[] CrRTable, CbBTable, CrGTable, CbGTable; + public static int[] RYTable, GYTable, BYTable, + RCbTable, GCbTable, BCbTable, RCrTable, GCrTable, BCrTable, NBitsTable; + //public static void static_this() { + static this() { - public static final int[] CrRTable, CbBTable, CrGTable, CbGTable; - public static final int[] RYTable, GYTable, BYTable, - RCbTable, GCbTable, BCbTable, RCrTable, GCrTable, BCrTable, NBitsTable; - static { - /* Initialize RGB-YCbCr Tables */ + RGB16 = [ + new RGB(0,0,0), + new RGB(0x80,0,0), + new RGB(0,0x80,0), + new RGB(0x80,0x80,0), + new RGB(0,0,0x80), + new RGB(0x80,0,0x80), + new RGB(0,0x80,0x80), + new RGB(0xC0,0xC0,0xC0), + new RGB(0x80,0x80,0x80), + new RGB(0xFF,0,0), + new RGB(0,0xFF,0), + new RGB(0xFF,0xFF,0), + new RGB(0,0,0xFF), + new RGB(0xFF,0,0xFF), + new RGB(0,0xFF,0xFF), + new RGB(0xFF,0xFF,0xFF) + ]; int [] rYTable = new int[256]; int [] gYTable = new int[256]; int [] bYTable = new int[256]; int [] rCbTable = new int[256]; int [] gCbTable = new int[256]; int [] bCbTable = new int[256]; + int [] rCrTable = new int[256]; int [] gCrTable = new int[256]; int [] bCrTable = new int[256]; for (int i = 0; i < 256; i++) { @@ -220,7 +236,7 @@ int srcHeight = image.height; int vhFactor = maxV * maxH; int[] frameComponent; - imageComponents = new byte[nComponents][]; + imageComponents = new byte[][](nComponents); for (int i = 0; i < nComponents; i++) { frameComponent = frameComponents[componentIds[i]]; imageComponents[i] = new byte[frameComponent[CW] * frameComponent[CH]]; @@ -427,7 +443,7 @@ int g = pixel & greenMask; g = (greenShift < 0) ? g >>> -greenShift : g << greenShift; int b = pixel & blueMask; - b = (blueShift < 0) ? b >>> -blueShift : b << blueShift; + b = (blueShift < 0) ? b >>> -blueShift : b << blueShift; dataYComp[dstDataIndex] = cast(byte)((RYTable[r] + GYTable[g] + BYTable[b]) >> 16); dataCbComp[dstDataIndex] = cast(byte)((RCbTable[r] + GCbTable[g] + BCbTable[b]) >> 16); dataCrComp[dstDataIndex] = cast(byte)((RCrTable[r] + GCrTable[g] + BCrTable[b]) >> 16); @@ -488,13 +504,13 @@ * B = Y + 1.77200 * Cb * where Cb and Cr represent the incoming values less MAXJSAMPLE/2. * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.) - * + * * To avoid floating-point arithmetic, we represent the fractional constants * as integers scaled up by 2^16 (about 4 digits precision); we have to divide * the products by 2^16, with appropriate rounding, to get the correct answer. * Notice that Y, being an integral input, does not contribute any fraction * so it need not participate in the rounding. - * + * * For even more speed, we avoid doing any multiplications in the inner loop * by precalculating the constants times Cb and Cr for all possible values. * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table); @@ -824,10 +840,10 @@ if (nBits is 0) { DWT.error(DWT.ERROR_INVALID_IMAGE); } - int[] power2m1 = new int[] { - 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, + int[] power2m1 = [ + 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191, 16383, 32767, 65535, 131125 - }; + ]; int code = (huffCode & power2m1[nBits - 1]) << (24 - nBits - currentBitCount); byte[] codeBuffer = new byte[4]; codeBuffer[0] = cast(byte)(code & 0xFF); @@ -1006,7 +1022,7 @@ int tmp4 = dataUnit[rIndex + 3] - dataUnit[rIndex + 4]; /** - * Even part per LL&M figure 1 --- note that published figure + * Even part per LL&M figure 1 --- note that published figure * is faulty; rotator 'sqrt(2)*c1' should be 'sqrt(2)*c6'. */ int tmp10 = tmp0 + tmp3; @@ -1086,7 +1102,7 @@ int tmp4 = dataUnit[c3] - dataUnit[c4]; /** - * Even part per LL&M figure 1 --- note that published figure + * Even part per LL&M figure 1 --- note that published figure * is faulty; rotator 'sqrt(2)*c1' should be 'sqrt(2)*c6'. */ int tmp10 = tmp0 + tmp3; @@ -1186,7 +1202,7 @@ JPEGQuantizationTable dqt = new JPEGQuantizationTable(inputStream); int[][] currentTables = quantizationTables; if (currentTables is null) { - currentTables = new int[4][]; + currentTables = new int[][](4); } int[] dqtTablesKeys = dqt.getQuantizationTablesKeys(); int[][] dqtTablesValues = dqt.getQuantizationTablesValues(); @@ -1249,7 +1265,7 @@ z3 = tmp0 + tmp2; int z4 = tmp1 + tmp3; int z5 = (z3 + z4) * FIX_1_175875602; /* sqrt(2) * c3 */ - + tmp0 *= FIX_0_298631336; /* sqrt(2) * (-c1+c3+c5-c7) */ tmp1 *= FIX_2_053119869; /* sqrt(2) * ( c1+c3-c5+c7) */ tmp2 *= FIX_3_072711026; /* sqrt(2) * ( c1+c3+c5-c7) */ @@ -1331,7 +1347,7 @@ z3 = tmp0 + tmp2; z4 = tmp1 + tmp3; z0 = (z3 + z4) * FIX_1_175875602; /* sqrt(2) * c3 */ - + tmp0 *= FIX_0_298631336; /* sqrt(2) * (-c1+c3+c5-c7) */ tmp1 *= FIX_2_053119869; /* sqrt(2) * ( c1+c3-c5+c7) */ tmp2 *= FIX_3_072711026; /* sqrt(2) * ( c1+c3+c5-c7) */ @@ -1340,10 +1356,10 @@ z2 *= 0 - FIX_2_562915447; /* sqrt(2) * (-c1-c3) */ z3 *= 0 - FIX_1_961570560; /* sqrt(2) * (-c3-c5) */ z4 *= 0 - FIX_0_390180644; /* sqrt(2) * (c5-c3) */ - + z3 += z0; z4 += z0; - + tmp0 += z1 + z3; tmp1 += z2 + z4; tmp2 += z2 + z3; @@ -1361,7 +1377,7 @@ } } } -bool isFileFormat(LEDataInputStream stream) { +override bool isFileFormat(LEDataInputStream stream) { try { JPEGStartOfImage soi = new JPEGStartOfImage(stream); stream.unread(soi.reference); @@ -1382,9 +1398,10 @@ && dataUnit[rIndex + 5] is 0 && dataUnit[rIndex + 6] is 0 && dataUnit[rIndex + 7] is 0; } -ImageData[] loadFromByteStream() { +override ImageData[] loadFromByteStream() { //TEMPORARY CODE - if (System.getProperty("dwt.internal.image.JPEGFileFormat_3.2") is null) { + //PORTING_FIXME + if (/+System.getProperty("dwt.internal.image.JPEGFileFormat_3.2") is null+/ true ) { return JPEGDecoder.loadFromByteStream(inputStream, loader); } JPEGStartOfImage soi = new JPEGStartOfImage(inputStream); @@ -1393,7 +1410,7 @@ /* Process the tables preceding the frame header. */ processTables(); - + /* Start of Frame. */ frameHeader = new JPEGFrameHeader(inputStream); if (!frameHeader.verify()) DWT.error(DWT.ERROR_INVALID_IMAGE); @@ -1410,10 +1427,10 @@ nComponents = frameHeader.getNumberOfImageComponents(); frameComponents = frameHeader.componentParameters; componentIds = frameHeader.componentIdentifiers; - imageComponents = new byte[nComponents][]; + imageComponents = new byte[][](nComponents); if (progressive) { // Progressive jpeg: need to keep all of the data units. - dataUnits = new int[nComponents][][]; + dataUnits = new int[][][](nComponents); } else { // Sequential jpeg: only need one data unit. dataUnit = new int[8 * 8]; @@ -1423,17 +1440,17 @@ int bufferSize = frameComponent[CW] * frameComponent[CH]; imageComponents[i] = new byte[bufferSize]; if (progressive) { - dataUnits[i] = new int[bufferSize][]; + dataUnits[i] = new int[][](bufferSize); } } /* Process the tables preceding the scan header. */ processTables(); - + /* Start of Scan. */ scanHeader = new JPEGScanHeader(inputStream); if (!scanHeader.verify()) DWT.error(DWT.ERROR_INVALID_IMAGE); - + /* Process scan(s) and further tables until EOI. */ int progressiveScanCount = 0; bool done = false; @@ -1458,7 +1475,7 @@ DWT.error(DWT.ERROR_IO, e); } } - + /* Process the tables preceding the next scan header. */ JPEGSegment jpegSegment = processTables(); if (jpegSegment is null || jpegSegment.getSegmentMarker() is EOI) { @@ -1468,7 +1485,7 @@ if (!scanHeader.verify()) DWT.error(DWT.ERROR_INVALID_IMAGE); } } - + if (progressive) { for (int ymcu = 0; ymcu < interleavedMcuRows; ymcu++) { for (int xmcu = 0; xmcu < interleavedMcuCols; xmcu++) { @@ -1495,12 +1512,12 @@ if (progressive && loader.hasListeners()) { loader.notifyListeners(new ImageLoaderEvent(loader, imageData, progressiveScanCount, true)); } - return new ImageData[] {imageData}; + return [imageData]; } ImageData createImageData() { return ImageData.internal_new( imageWidth, - imageHeight, + imageHeight, nComponents * samplePrecision, setUpPalette(), nComponents is 1 ? 4 : 1, @@ -1641,7 +1658,7 @@ break; default: skipSegmentFrom(inputStream); - + } } } @@ -1729,7 +1746,7 @@ try { byte[] byteArray = new byte[4]; JPEGSegment jpegSegment = new JPEGSegment(byteArray); - + if (byteStream.read(byteArray) !is byteArray.length) { DWT.error(DWT.ERROR_INVALID_IMAGE); } @@ -1762,16 +1779,16 @@ destIndex += compWidth; } } -void unloadIntoByteStream(ImageLoader loader) { +override void unloadIntoByteStream(ImageLoader loader) { ImageData image = loader.data[0]; - if (!new JPEGStartOfImage().writeToStream(outputStream)) { + if (!(new JPEGStartOfImage()).writeToStream(outputStream)) { DWT.error(DWT.ERROR_IO); } - JPEGAppn appn = new JPEGAppn(new byte[] {cast(byte)0xFF, cast(byte)0xE0, 0, 0x10, 0x4A, 0x46, 0x49, 0x46, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0}); + JPEGAppn appn = new JPEGAppn([cast(byte)0xFF, cast(byte)0xE0, 0, 0x10, 0x4A, 0x46, 0x49, 0x46, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0]); if (!appn.writeToStream(outputStream)) { DWT.error(DWT.ERROR_IO); } - quantizationTables = new int[4][]; + quantizationTables = new int[][](4); JPEGQuantizationTable chromDQT = JPEGQuantizationTable.defaultChrominanceTable(); chromDQT.scaleBy(encoderQFactor); int[] jpegDQTKeys = chromDQT.getQuantizationTablesKeys(); @@ -1796,23 +1813,23 @@ int[][] frameParams, scanParams; if (image.depth is 1) { frameLength = 11; - frameParams = new int[1][]; - frameParams[0] = new int[] {1, 1, 1, 0, 0}; - scanParams = new int[1][]; - scanParams[0] = new int[] {0, 0}; + frameParams = new int[][](1); + frameParams[0] = [1, 1, 1, 0, 0]; + scanParams = new int[][](1); + scanParams[0] = [0, 0]; scanLength = 8; nComponents = 1; precision = 1; } else { frameLength = 17; - frameParams = new int[3][]; - frameParams[0] = new int[] {0, 2, 2, 0, 0}; - frameParams[1] = new int[] {1, 1, 1, 0, 0}; - frameParams[2] = new int[] {1, 1, 1, 0, 0}; - scanParams = new int[3][]; - scanParams[0] = new int[] {0, 0}; - scanParams[1] = new int[] {1, 1}; - scanParams[2] = new int[] {1, 1}; + frameParams = new int[][](3); + frameParams[0] = [0, 2, 2, 0, 0]; + frameParams[1] = [1, 1, 1, 0, 0]; + frameParams[2] = [1, 1, 1, 0, 0]; + scanParams = new int[][](3); + scanParams[0] = [0, 0]; + scanParams[1] = [1, 1]; + scanParams[2] = [1, 1]; scanLength = 12; nComponents = 3; precision = 8; @@ -1827,7 +1844,7 @@ frameHeader.setNumberOfLines(imageHeight); frameHeader.setNumberOfImageComponents(nComponents); frameHeader.componentParameters = frameParams; - frameHeader.componentIdentifiers = new int[] {0, 1, 2}; + frameHeader.componentIdentifiers = [0, 1, 2]; frameHeader.initializeContents(); if (!frameHeader.writeToStream(outputStream)) { DWT.error(DWT.ERROR_IO); @@ -1842,12 +1859,12 @@ interleavedMcuRows = (imageHeight + mcuHeight - 1) / mcuHeight; acHuffmanTables = new JPEGHuffmanTable[4]; dcHuffmanTables = new JPEGHuffmanTable[4]; - JPEGHuffmanTable[] dhtTables = new JPEGHuffmanTable[] { + JPEGHuffmanTable[] dhtTables = [ JPEGHuffmanTable.getDefaultDCLuminanceTable(), JPEGHuffmanTable.getDefaultDCChrominanceTable(), JPEGHuffmanTable.getDefaultACLuminanceTable(), JPEGHuffmanTable.getDefaultACChrominanceTable() - }; + ]; for (int i = 0; i < dhtTables.length; i++) { JPEGHuffmanTable dhtTable = dhtTables[i]; if (!dhtTable.writeToStream(outputStream)) { @@ -1880,7 +1897,7 @@ currentByte = 0; currentBitCount = 0; encodeScan(); - if (!new JPEGEndOfImage().writeToStream(outputStream)) { + if (!(new JPEGEndOfImage()).writeToStream(outputStream)) { DWT.error(DWT.ERROR_IO); } }