commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dam...@apache.org
Subject svn commit: r1342971 [4/20] - in /commons/proper/imaging/trunk/src: main/java/org/apache/commons/imaging/formats/bmp/ main/java/org/apache/commons/imaging/formats/bmp/pixelparsers/ main/java/org/apache/commons/imaging/formats/bmp/writers/ main/java/org...
Date Sat, 26 May 2012 21:19:22 GMT
Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/JpegImageParser.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/JpegImageParser.java?rev=1342971&r1=1342970&r2=1342971&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/JpegImageParser.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/JpegImageParser.java Sat May 26 21:19:03 2012
@@ -55,30 +55,25 @@ import org.apache.commons.imaging.format
 import org.apache.commons.imaging.formats.tiff.constants.TiffTagConstants;
 import org.apache.commons.imaging.util.Debug;
 
-public class JpegImageParser extends ImageParser implements JpegConstants
-{
-    public JpegImageParser()
-    {
+public class JpegImageParser extends ImageParser implements JpegConstants {
+    public JpegImageParser() {
         setByteOrder(BYTE_ORDER_NETWORK);
         // setDebug(true);
     }
 
     @Override
-    protected ImageFormat[] getAcceptedTypes()
-    {
+    protected ImageFormat[] getAcceptedTypes() {
         return new ImageFormat[] { ImageFormat.IMAGE_FORMAT_JPEG, //
         };
     }
 
     @Override
-    public String getName()
-    {
+    public String getName() {
         return "Jpeg-Custom";
     }
 
     @Override
-    public String getDefaultExtension()
-    {
+    public String getDefaultExtension() {
         return DEFAULT_EXTENSION;
     }
 
@@ -87,26 +82,22 @@ public class JpegImageParser extends Ima
     private static final String ACCEPTED_EXTENSIONS[] = { ".jpg", ".jpeg", };
 
     @Override
-    protected String[] getAcceptedExtensions()
-    {
+    protected String[] getAcceptedExtensions() {
         return ACCEPTED_EXTENSIONS;
     }
 
     @Override
     public final BufferedImage getBufferedImage(ByteSource byteSource,
-            Map params) throws ImageReadException, IOException
-    {
+            Map params) throws ImageReadException, IOException {
         JpegDecoder jpegDecoder = new JpegDecoder();
         return jpegDecoder.decode(byteSource);
     }
 
-    private boolean keepMarker(int marker, int markers[])
-    {
+    private boolean keepMarker(int marker, int markers[]) {
         if (markers == null)
             return true;
 
-        for (int i = 0; i < markers.length; i++)
-        {
+        for (int i = 0; i < markers.length; i++) {
             if (markers[i] == marker)
                 return true;
         }
@@ -114,10 +105,9 @@ public class JpegImageParser extends Ima
         return false;
     }
 
-    public List<Segment> readSegments(ByteSource byteSource, final int markers[],
-            final boolean returnAfterFirst, boolean readEverything)
-            throws ImageReadException, IOException
-    {
+    public List<Segment> readSegments(ByteSource byteSource,
+            final int markers[], final boolean returnAfterFirst,
+            boolean readEverything) throws ImageReadException, IOException {
         final List<Segment> result = new ArrayList<Segment>();
         final JpegImageParser parser = this;
         final int[] sofnSegments = {
@@ -126,26 +116,22 @@ public class JpegImageParser extends Ima
 
                 SOF1Marker, SOF2Marker, SOF3Marker, SOF5Marker, SOF6Marker,
                 SOF7Marker, SOF9Marker, SOF10Marker, SOF11Marker, SOF13Marker,
-                SOF14Marker, SOF15Marker,
-        };
+                SOF14Marker, SOF15Marker, };
 
         JpegUtils.Visitor visitor = new JpegUtils.Visitor() {
             // return false to exit before reading image data.
-            public boolean beginSOS()
-            {
+            public boolean beginSOS() {
                 return false;
             }
 
             public void visitSOS(int marker, byte markerBytes[],
-                    byte imageData[])
-            {
+                    byte imageData[]) {
             }
 
             // return false to exit traversal.
             public boolean visitSegment(int marker, byte markerBytes[],
                     int markerLength, byte markerLengthBytes[],
-                    byte segmentData[]) throws ImageReadException, IOException
-            {
+                    byte segmentData[]) throws ImageReadException, IOException {
                 if (marker == EOIMarker)
                     return false;
 
@@ -158,28 +144,21 @@ public class JpegImageParser extends Ima
                 if (!keepMarker(marker, markers))
                     return true;
 
-                if (marker == JPEG_APP13_Marker)
-                {
+                if (marker == JPEG_APP13_Marker) {
                     // Debug.debug("app 13 segment data", segmentData.length);
                     result.add(new App13Segment(parser, marker, segmentData));
-                } else if (marker == JPEG_APP2_Marker)
-                {
+                } else if (marker == JPEG_APP2_Marker) {
                     result.add(new App2Segment(marker, segmentData));
-                } else if (marker == JFIFMarker)
-                {
+                } else if (marker == JFIFMarker) {
                     result.add(new JfifSegment(marker, segmentData));
-                } else if (Arrays.binarySearch(sofnSegments, marker) >= 0)
-                {
+                } else if (Arrays.binarySearch(sofnSegments, marker) >= 0) {
                     result.add(new SofnSegment(marker, segmentData));
-                } else if (marker == DQTMarker)
-                {
+                } else if (marker == DQTMarker) {
                     result.add(new DqtSegment(marker, segmentData));
                 } else if ((marker >= JPEG_APP1_Marker)
-                        && (marker <= JPEG_APP15_Marker))
-                {
+                        && (marker <= JPEG_APP15_Marker)) {
                     result.add(new UnknownSegment(marker, segmentData));
-                } else if (marker == COMMarker)
-                {
+                } else if (marker == COMMarker) {
                     result.add(new ComSegment(marker, segmentData));
                 }
 
@@ -197,20 +176,17 @@ public class JpegImageParser extends Ima
 
     public static final boolean permissive = true;
 
-    private byte[] assembleSegments(List<App2Segment> v) throws ImageReadException
-    {
-        try
-        {
+    private byte[] assembleSegments(List<App2Segment> v)
+            throws ImageReadException {
+        try {
             return assembleSegments(v, false);
-        } catch (ImageReadException e)
-        {
+        } catch (ImageReadException e) {
             return assembleSegments(v, true);
         }
     }
 
     private byte[] assembleSegments(List<App2Segment> v, boolean start_with_zero)
-            throws ImageReadException
-    {
+            throws ImageReadException {
         if (v.size() < 1)
             throw new ImageReadException("No App2 Segments Found.");
 
@@ -228,12 +204,10 @@ public class JpegImageParser extends Ima
         int offset = start_with_zero ? 0 : 1;
 
         int total = 0;
-        for (int i = 0; i < v.size(); i++)
-        {
+        for (int i = 0; i < v.size(); i++) {
             App2Segment segment = v.get(i);
 
-            if ((i + offset) != segment.cur_marker)
-            {
+            if ((i + offset) != segment.cur_marker) {
                 dumpSegments(v);
                 throw new ImageReadException(
                         "Incoherent App2 Segment Ordering.  i: " + i
@@ -241,8 +215,7 @@ public class JpegImageParser extends Ima
                                 + segment.cur_marker + ".");
             }
 
-            if (markerCount != segment.num_markers)
-            {
+            if (markerCount != segment.num_markers) {
                 dumpSegments(v);
                 throw new ImageReadException(
                         "Inconsistent App2 Segment Count info.  markerCount: "
@@ -256,8 +229,7 @@ public class JpegImageParser extends Ima
         byte result[] = new byte[total];
         int progress = 0;
 
-        for (int i = 0; i < v.size(); i++)
-        {
+        for (int i = 0; i < v.size(); i++) {
             App2Segment segment = v.get(i);
 
             System.arraycopy(segment.icc_bytes, 0, result, progress,
@@ -268,13 +240,11 @@ public class JpegImageParser extends Ima
         return result;
     }
 
-    private void dumpSegments(List<? extends Segment> v)
-    {
+    private void dumpSegments(List<? extends Segment> v) {
         Debug.debug();
         Debug.debug("dumpSegments", v.size());
 
-        for (int i = 0; i < v.size(); i++)
-        {
+        for (int i = 0; i < v.size(); i++) {
             App2Segment segment = (App2Segment) v.get(i);
 
             Debug.debug((i) + ": " + segment.cur_marker + " / "
@@ -284,24 +254,20 @@ public class JpegImageParser extends Ima
     }
 
     public List<Segment> readSegments(ByteSource byteSource, int markers[],
-            boolean returnAfterFirst) throws ImageReadException, IOException
-    {
+            boolean returnAfterFirst) throws ImageReadException, IOException {
         return readSegments(byteSource, markers, returnAfterFirst, false);
     }
 
     @Override
     public byte[] getICCProfileBytes(ByteSource byteSource, Map params)
-            throws ImageReadException, IOException
-    {
+            throws ImageReadException, IOException {
         List<Segment> segments = readSegments(byteSource,
                 new int[] { JPEG_APP2_Marker, }, false);
 
         List<App2Segment> filtered = new ArrayList<App2Segment>();
-        if (segments != null)
-        {
+        if (segments != null) {
             // throw away non-icc profile app2 segments.
-            for (int i = 0; i < segments.size(); i++)
-            {
+            for (int i = 0; i < segments.size(); i++) {
                 App2Segment segment = (App2Segment) segments.get(i);
                 if (segment.icc_bytes != null)
                     filtered.add(segment);
@@ -325,8 +291,7 @@ public class JpegImageParser extends Ima
 
     @Override
     public IImageMetadata getMetadata(ByteSource byteSource, Map params)
-            throws ImageReadException, IOException
-    {
+            throws ImageReadException, IOException {
         TiffImageMetadata exif = getExifMetadata(byteSource, params);
 
         JpegPhotoshopMetadata photoshop = getPhotoshopMetadata(byteSource,
@@ -340,17 +305,14 @@ public class JpegImageParser extends Ima
         return result;
     }
 
-    public static boolean isExifAPP1Segment(GenericSegment segment)
-    {
+    public static boolean isExifAPP1Segment(GenericSegment segment) {
         return byteArrayHasPrefix(segment.bytes, EXIF_IDENTIFIER_CODE);
     }
 
-    private List<Segment> filterAPP1Segments(List<Segment> v)
-    {
+    private List<Segment> filterAPP1Segments(List<Segment> v) {
         List<Segment> result = new ArrayList<Segment>();
 
-        for (int i = 0; i < v.size(); i++)
-        {
+        for (int i = 0; i < v.size(); i++) {
             GenericSegment segment = (GenericSegment) v.get(i);
             if (isExifAPP1Segment(segment))
                 result.add(segment);
@@ -360,8 +322,7 @@ public class JpegImageParser extends Ima
     }
 
     public TiffImageMetadata getExifMetadata(ByteSource byteSource, Map params)
-            throws ImageReadException, IOException
-    {
+            throws ImageReadException, IOException {
         byte bytes[] = getExifRawData(byteSource);
         if (null == bytes)
             return null;
@@ -376,8 +337,7 @@ public class JpegImageParser extends Ima
     }
 
     public byte[] getExifRawData(ByteSource byteSource)
-            throws ImageReadException, IOException
-    {
+            throws ImageReadException, IOException {
         List<Segment> segments = readSegments(byteSource,
                 new int[] { JPEG_APP1_Marker, }, false);
 
@@ -411,34 +371,28 @@ public class JpegImageParser extends Ima
     }
 
     public boolean hasExifSegment(ByteSource byteSource)
-            throws ImageReadException, IOException
-    {
+            throws ImageReadException, IOException {
         final boolean result[] = { false, };
 
         JpegUtils.Visitor visitor = new JpegUtils.Visitor() {
             // return false to exit before reading image data.
-            public boolean beginSOS()
-            {
+            public boolean beginSOS() {
                 return false;
             }
 
             public void visitSOS(int marker, byte markerBytes[],
-                    byte imageData[])
-            {
+                    byte imageData[]) {
             }
 
             // return false to exit traversal.
             public boolean visitSegment(int marker, byte markerBytes[],
                     int markerLength, byte markerLengthBytes[],
-                    byte segmentData[]) throws ImageReadException, IOException
-            {
+                    byte segmentData[]) throws ImageReadException, IOException {
                 if (marker == 0xffd9)
                     return false;
 
-                if (marker == JPEG_APP1_Marker)
-                {
-                    if (byteArrayHasPrefix(segmentData, EXIF_IDENTIFIER_CODE))
-                    {
+                if (marker == JPEG_APP1_Marker) {
+                    if (byteArrayHasPrefix(segmentData, EXIF_IDENTIFIER_CODE)) {
                         result[0] = true;
                         return false;
                     }
@@ -454,34 +408,28 @@ public class JpegImageParser extends Ima
     }
 
     public boolean hasIptcSegment(ByteSource byteSource)
-            throws ImageReadException, IOException
-    {
+            throws ImageReadException, IOException {
         final boolean result[] = { false, };
 
         JpegUtils.Visitor visitor = new JpegUtils.Visitor() {
             // return false to exit before reading image data.
-            public boolean beginSOS()
-            {
+            public boolean beginSOS() {
                 return false;
             }
 
             public void visitSOS(int marker, byte markerBytes[],
-                    byte imageData[])
-            {
+                    byte imageData[]) {
             }
 
             // return false to exit traversal.
             public boolean visitSegment(int marker, byte markerBytes[],
                     int markerLength, byte markerLengthBytes[],
-                    byte segmentData[]) throws ImageReadException, IOException
-            {
+                    byte segmentData[]) throws ImageReadException, IOException {
                 if (marker == 0xffd9)
                     return false;
 
-                if (marker == JPEG_APP13_Marker)
-                {
-                    if (new IptcParser().isPhotoshopJpegSegment(segmentData))
-                    {
+                if (marker == JPEG_APP13_Marker) {
+                    if (new IptcParser().isPhotoshopJpegSegment(segmentData)) {
                         result[0] = true;
                         return false;
                     }
@@ -497,34 +445,28 @@ public class JpegImageParser extends Ima
     }
 
     public boolean hasXmpSegment(ByteSource byteSource)
-            throws ImageReadException, IOException
-    {
+            throws ImageReadException, IOException {
         final boolean result[] = { false, };
 
         JpegUtils.Visitor visitor = new JpegUtils.Visitor() {
             // return false to exit before reading image data.
-            public boolean beginSOS()
-            {
+            public boolean beginSOS() {
                 return false;
             }
 
             public void visitSOS(int marker, byte markerBytes[],
-                    byte imageData[])
-            {
+                    byte imageData[]) {
             }
 
             // return false to exit traversal.
             public boolean visitSegment(int marker, byte markerBytes[],
                     int markerLength, byte markerLengthBytes[],
-                    byte segmentData[]) throws ImageReadException, IOException
-            {
+                    byte segmentData[]) throws ImageReadException, IOException {
                 if (marker == 0xffd9)
                     return false;
 
-                if (marker == JPEG_APP1_Marker)
-                {
-                    if (new JpegXmpParser().isXmpJpegSegment(segmentData))
-                    {
+                if (marker == JPEG_APP1_Marker) {
+                    if (new JpegXmpParser().isXmpJpegSegment(segmentData)) {
                         result[0] = true;
                         return false;
                     }
@@ -541,7 +483,7 @@ public class JpegImageParser extends Ima
     /**
      * Extracts embedded XML metadata as XML string.
      * <p>
-     *
+     * 
      * @param byteSource
      *            File containing image data.
      * @param params
@@ -550,35 +492,29 @@ public class JpegImageParser extends Ima
      */
     @Override
     public String getXmpXml(ByteSource byteSource, Map params)
-            throws ImageReadException, IOException
-    {
+            throws ImageReadException, IOException {
 
         final List<String> result = new ArrayList<String>();
 
         JpegUtils.Visitor visitor = new JpegUtils.Visitor() {
             // return false to exit before reading image data.
-            public boolean beginSOS()
-            {
+            public boolean beginSOS() {
                 return false;
             }
 
             public void visitSOS(int marker, byte markerBytes[],
-                    byte imageData[])
-            {
+                    byte imageData[]) {
             }
 
             // return false to exit traversal.
             public boolean visitSegment(int marker, byte markerBytes[],
                     int markerLength, byte markerLengthBytes[],
-                    byte segmentData[]) throws ImageReadException, IOException
-            {
+                    byte segmentData[]) throws ImageReadException, IOException {
                 if (marker == 0xffd9)
                     return false;
 
-                if (marker == JPEG_APP1_Marker)
-                {
-                    if (new JpegXmpParser().isXmpJpegSegment(segmentData))
-                    {
+                if (marker == JPEG_APP1_Marker) {
+                    if (new JpegXmpParser().isXmpJpegSegment(segmentData)) {
                         result.add(new JpegXmpParser()
                                 .parseXmpJpegSegment(segmentData));
                         return false;
@@ -599,8 +535,7 @@ public class JpegImageParser extends Ima
     }
 
     public JpegPhotoshopMetadata getPhotoshopMetadata(ByteSource byteSource,
-            Map params) throws ImageReadException, IOException
-    {
+            Map params) throws ImageReadException, IOException {
         List<Segment> segments = readSegments(byteSource,
                 new int[] { JPEG_APP13_Marker, }, false);
 
@@ -609,8 +544,7 @@ public class JpegImageParser extends Ima
 
         PhotoshopApp13Data photoshopApp13Data = null;
 
-        for (int i = 0; i < segments.size(); i++)
-        {
+        for (int i = 0; i < segments.size(); i++) {
             App13Segment segment = (App13Segment) segments.get(i);
 
             PhotoshopApp13Data data = segment.parsePhotoshopSegment(params);
@@ -621,15 +555,14 @@ public class JpegImageParser extends Ima
             photoshopApp13Data = data;
         }
 
-        if(null==photoshopApp13Data)
+        if (null == photoshopApp13Data)
             return null;
         return new JpegPhotoshopMetadata(photoshopApp13Data);
     }
 
     @Override
     public Dimension getImageSize(ByteSource byteSource, Map params)
-            throws ImageReadException, IOException
-    {
+            throws ImageReadException, IOException {
         List<Segment> segments = readSegments(byteSource, new int[] {
                 // kJFIFMarker,
                 SOF0Marker,
@@ -651,21 +584,18 @@ public class JpegImageParser extends Ima
         return new Dimension(fSOFNSegment.width, fSOFNSegment.height);
     }
 
-    public byte[] embedICCProfile(byte image[], byte profile[])
-    {
+    public byte[] embedICCProfile(byte image[], byte profile[]) {
         return null;
     }
 
     @Override
-    public boolean embedICCProfile(File src, File dst, byte profile[])
-    {
+    public boolean embedICCProfile(File src, File dst, byte profile[]) {
         return false;
     }
 
     @Override
     public ImageInfo getImageInfo(ByteSource byteSource, Map params)
-            throws ImageReadException, IOException
-    {
+            throws ImageReadException, IOException {
         // List allSegments = readSegments(byteSource, null, false);
 
         List<Segment> SOF_segments = readSegments(byteSource, new int[] {
@@ -712,8 +642,7 @@ public class JpegImageParser extends Ima
         // int JFIF_minor_version;
         String FormatDetails;
 
-        if (jfifSegment != null)
-        {
+        if (jfifSegment != null) {
             x_density = jfifSegment.xDensity;
             y_density = jfifSegment.yDensity;
             int density_units = jfifSegment.densityUnits;
@@ -723,8 +652,7 @@ public class JpegImageParser extends Ima
             FormatDetails = "Jpeg/JFIF v." + jfifSegment.jfifMajorVersion + "."
                     + jfifSegment.jfifMinorVersion;
 
-            switch (density_units)
-            {
+            switch (density_units) {
             case 0:
                 break;
             case 1: // inches
@@ -736,13 +664,11 @@ public class JpegImageParser extends Ima
             default:
                 break;
             }
-        } else
-        {
+        } else {
             JpegImageMetadata metadata = (JpegImageMetadata) getMetadata(
                     byteSource, params);
 
-            if (metadata != null)
-            {
+            if (metadata != null) {
                 {
                     TiffField field = metadata
                             .findEXIFValue(TiffTagConstants.TIFF_TAG_XRESOLUTION);
@@ -758,13 +684,11 @@ public class JpegImageParser extends Ima
                 {
                     TiffField field = metadata
                             .findEXIFValue(TiffTagConstants.TIFF_TAG_RESOLUTION_UNIT);
-                    if (field != null)
-                    {
+                    if (field != null) {
                         int density_units = ((Number) field.getValue())
                                 .intValue();
 
-                        switch (density_units)
-                        {
+                        switch (density_units) {
                         case 1:
                             break;
                         case 2: // inches
@@ -790,19 +714,17 @@ public class JpegImageParser extends Ima
         int PhysicalWidthDpi = -1;
         float PhysicalWidthInch = -1;
 
-        if (units_per_inch > 0)
-        {
+        if (units_per_inch > 0) {
             PhysicalWidthDpi = (int) Math.round(x_density * units_per_inch);
             PhysicalWidthInch = (float) (Width / (x_density * units_per_inch));
-            PhysicalHeightDpi = (int) Math.round(y_density     * units_per_inch);
+            PhysicalHeightDpi = (int) Math.round(y_density * units_per_inch);
             PhysicalHeightInch = (float) (Height / (y_density * units_per_inch));
         }
 
         List<String> Comments = new ArrayList<String>();
         List<Segment> commentSegments = readSegments(byteSource,
                 new int[] { COMMarker }, false);
-        for (int i = 0; i < commentSegments.size(); i++)
-        {
+        for (int i = 0; i < commentSegments.size(); i++) {
             ComSegment comSegment = (ComSegment) commentSegments.get(i);
             String comment = "";
             try {
@@ -1018,8 +940,7 @@ public class JpegImageParser extends Ima
 
     @Override
     public boolean dumpImageFile(PrintWriter pw, ByteSource byteSource)
-            throws ImageReadException, IOException
-    {
+            throws ImageReadException, IOException {
         pw.println("tiff.dumpImageFile");
 
         {
@@ -1038,8 +959,7 @@ public class JpegImageParser extends Ima
             if (segments == null)
                 throw new ImageReadException("No Segments Found.");
 
-            for (int d = 0; d < segments.size(); d++)
-            {
+            for (int d = 0; d < segments.size(); d++) {
 
                 Segment segment = segments.get(d);
 

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/JpegPhotoshopMetadata.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/JpegPhotoshopMetadata.java?rev=1342971&r1=1342970&r2=1342971&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/JpegPhotoshopMetadata.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/JpegPhotoshopMetadata.java Sat May 26 21:19:03 2012
@@ -27,28 +27,24 @@ import org.apache.commons.imaging.format
 import org.apache.commons.imaging.util.Debug;
 
 public class JpegPhotoshopMetadata extends ImageMetadata implements
-        IptcConstants
-{
+        IptcConstants {
 
     public final PhotoshopApp13Data photoshopApp13Data;
 
-    public JpegPhotoshopMetadata(final PhotoshopApp13Data photoshopApp13Data)
-    {
+    public JpegPhotoshopMetadata(final PhotoshopApp13Data photoshopApp13Data) {
         this.photoshopApp13Data = photoshopApp13Data;
 
         List<IptcRecord> records = photoshopApp13Data.getRecords();
         Collections.sort(records, IptcRecord.COMPARATOR);
-        for (int j = 0; j < records.size(); j++)
-        {
+        for (int j = 0; j < records.size(); j++) {
             IptcRecord element = records.get(j);
             if (element.iptcType != IptcTypes.RECORD_VERSION)
                 add(element.getIptcTypeName(), element.getValue());
         }
     }
 
-    public void dump()
-    {
+    public void dump() {
         Debug.debug(this.toString());
     }
 
-}
\ No newline at end of file
+}

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/JpegUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/JpegUtils.java?rev=1342971&r1=1342970&r2=1342971&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/JpegUtils.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/JpegUtils.java Sat May 26 21:19:03 2012
@@ -24,15 +24,12 @@ import org.apache.commons.imaging.common
 import org.apache.commons.imaging.common.bytesource.ByteSource;
 import org.apache.commons.imaging.util.Debug;
 
-public class JpegUtils extends BinaryFileParser implements JpegConstants
-{
-    public JpegUtils()
-    {
+public class JpegUtils extends BinaryFileParser implements JpegConstants {
+    public JpegUtils() {
         setByteOrder(BYTE_ORDER_NETWORK);
     }
 
-    public static interface Visitor
-    {
+    public static interface Visitor {
         // return false to exit before reading image data.
         public boolean beginSOS();
 
@@ -49,12 +46,10 @@ public class JpegUtils extends BinaryFil
     public void traverseJFIF(ByteSource byteSource, Visitor visitor)
             throws ImageReadException,
             // ImageWriteException,
-            IOException
-    {
+            IOException {
         InputStream is = null;
 
-        try
-        {
+        try {
             is = byteSource.getInputStream();
 
             readAndVerifyBytes(is, SOI,
@@ -62,20 +57,22 @@ public class JpegUtils extends BinaryFil
 
             int byteOrder = getByteOrder();
 
-            for (int markerCount = 0; true; markerCount++)
-            {
+            for (int markerCount = 0; true; markerCount++) {
                 byte[] markerBytes = new byte[2];
                 do {
                     markerBytes[0] = markerBytes[1];
-                    markerBytes[1] = readByte("marker", is, "Could not read marker");
-                } while ((0xff & markerBytes[0]) != 0xff || (0xff & markerBytes[1]) == 0xff);
-                int marker = ((0xff & markerBytes[0]) << 8) | (0xff & markerBytes[1]);
+                    markerBytes[1] = readByte("marker", is,
+                            "Could not read marker");
+                } while ((0xff & markerBytes[0]) != 0xff
+                        || (0xff & markerBytes[1]) == 0xff);
+                int marker = ((0xff & markerBytes[0]) << 8)
+                        | (0xff & markerBytes[1]);
+
+                // Debug.debug("marker", marker + " (0x" +
+                // Integer.toHexString(marker) + ")");
+                // Debug.debug("markerBytes", markerBytes);
 
-//                Debug.debug("marker", marker + " (0x" + Integer.toHexString(marker) + ")");
-//                Debug.debug("markerBytes", markerBytes);
-
-                if (marker == EOIMarker || marker == SOS_Marker)
-                {
+                if (marker == EOIMarker || marker == SOS_Marker) {
                     if (!visitor.beginSOS())
                         return;
 
@@ -89,8 +86,9 @@ public class JpegUtils extends BinaryFil
                 int segmentLength = convertByteArrayToShort("segmentLength",
                         segmentLengthBytes, byteOrder);
 
-//                Debug.debug("segmentLength", segmentLength + " (0x" + Integer.toHexString(segmentLength) + ")");
-//                Debug.debug("segmentLengthBytes", segmentLengthBytes);
+                // Debug.debug("segmentLength", segmentLength + " (0x" +
+                // Integer.toHexString(segmentLength) + ")");
+                // Debug.debug("segmentLengthBytes", segmentLengthBytes);
 
                 byte segmentData[] = readByteArray("Segment Data",
                         segmentLength - 2, is,
@@ -103,24 +101,19 @@ public class JpegUtils extends BinaryFil
                     return;
             }
 
-        } finally
-        {
-            try
-            {
+        } finally {
+            try {
                 if (is != null) {
                     is.close();
                 }
-            } catch (Exception e)
-            {
+            } catch (Exception e) {
                 Debug.debug(e);
             }
         }
     }
 
-    public static String getMarkerName(int marker)
-    {
-        switch (marker)
-        {
+    public static String getMarkerName(int marker) {
+        switch (marker) {
         case SOS_Marker:
             return "SOS_Marker";
             // case JPEG_APP0 :
@@ -179,18 +172,15 @@ public class JpegUtils extends BinaryFil
     }
 
     public void dumpJFIF(ByteSource byteSource) throws ImageReadException,
-            IOException
-    {
+            IOException {
         Visitor visitor = new Visitor() {
             // return false to exit before reading image data.
-            public boolean beginSOS()
-            {
+            public boolean beginSOS() {
                 return true;
             }
 
             public void visitSOS(int marker, byte markerBytes[],
-                    byte imageData[])
-            {
+                    byte imageData[]) {
                 Debug.debug("SOS marker.  " + imageData.length
                         + " bytes of image data.");
                 Debug.debug("");
@@ -199,8 +189,7 @@ public class JpegUtils extends BinaryFil
             // return false to exit traversal.
             public boolean visitSegment(int marker, byte markerBytes[],
                     int segmentLength, byte segmentLengthBytes[],
-                    byte segmentData[])
-            {
+                    byte segmentData[]) {
                 Debug.debug("Segment marker: " + Integer.toHexString(marker)
                         + " (" + getMarkerName(marker) + "), "
                         + segmentData.length + " bytes of segment data.");
@@ -210,4 +199,4 @@ public class JpegUtils extends BinaryFil
 
         traverseJFIF(byteSource, visitor);
     }
-}
\ No newline at end of file
+}

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/ZigZag.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/ZigZag.java?rev=1342971&r1=1342970&r2=1342971&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/ZigZag.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/ZigZag.java Sat May 26 21:19:03 2012
@@ -15,10 +15,8 @@
 
 package org.apache.commons.imaging.formats.jpeg;
 
-public class ZigZag
-{
-    private static final int zigZag[] =
-    {
+public class ZigZag {
+    private static final int zigZag[] = {
          0,  1,  5,  6, 14, 15, 27, 28,
          2,  4,  7, 13, 16, 26, 29, 42,
          3,  8, 12, 17, 25, 30, 41, 43,
@@ -29,18 +27,14 @@ public class ZigZag
         35, 36, 48, 49, 57, 58, 62, 63
     };
 
-    public static void zigZagToBlock(int[] zz, int[] block)
-    {
-        for (int i = 0; i < 64; i++)
-        {
+    public static void zigZagToBlock(int[] zz, int[] block) {
+        for (int i = 0; i < 64; i++) {
             block[i] = zz[zigZag[i]];
         }
     }
 
-    public static void blockToZigZag(int[] block, int[] zz)
-    {
-        for (int i = 0; i < 64; i++)
-        {
+    public static void blockToZigZag(int[] block, int[] zz) {
+        for (int i = 0; i < 64; i++) {
             zz[zigZag[i]] = block[i];
         }
     }

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/decoder/Dct.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/decoder/Dct.java?rev=1342971&r1=1342970&r2=1342971&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/decoder/Dct.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/decoder/Dct.java Sat May 26 21:19:03 2012
@@ -15,140 +15,118 @@
 
 package org.apache.commons.imaging.formats.jpeg.decoder;
 
-public class Dct
-{
+public class Dct {
     /*
-     * The book "JPEG still image data compression standard",
-     * by Pennebaker and Mitchell, Chapter 4, discusses a number of
-     * approaches to the fast DCT. Here's the cost, exluding modified
-     * (de)quantization, for transforming an 8x8 block:
-     *
-     * Algorithm         Adds Multiplies RightShifts  Total
-     * Naive              896       1024           0   1920
-     * "Symmetries"       448        224           0    672
-     * Vetterli and       464        208           0    672
-     *   Ligtenberg
-     * Arai, Agui and     464         80           0    544
-     *   Nakajima (AA&N)
-     * Feig 8x8           462         54           6    522
-     * Fused mul/add                                    416
-     *   (a pipe dream)
-     *
+     * The book "JPEG still image data compression standard", by Pennebaker and
+     * Mitchell, Chapter 4, discusses a number of approaches to the fast DCT.
+     * Here's the cost, exluding modified (de)quantization, for transforming an
+     * 8x8 block:
+     * 
+     * Algorithm Adds Multiplies RightShifts Total Naive 896 1024 0 1920
+     * "Symmetries" 448 224 0 672 Vetterli and 464 208 0 672 Ligtenberg Arai,
+     * Agui and 464 80 0 544 Nakajima (AA&N) Feig 8x8 462 54 6 522 Fused mul/add
+     * 416 (a pipe dream)
+     * 
      * IJG's libjpeg, FFmpeg, and a number of others use AA&N.
-     *
-     * It would appear that Feig does 4-5% less operations, and
-     * multiplications are reduced from 80 in AA&N to only 54.
-     * But in practice:
-     *
-     * Benchmarks, Intel Core i3 @ 2.93 GHz in long mode, 4 GB RAM
-     * Time taken to do 100 million IDCTs (less is better):
-     * Rene' Stöckel's Feig, int: 45.07 seconds
-     * My Feig, floating point: 36.252 seconds
-     * AA&N, unrolled loops, double[][] -> double[][]: 25.167 seconds
-     *
-     * Clearly Feig is hopeless. I suspect the performance killer is simply
-     * the weight of the algorithm: massive number of local variables,
-     * large code size, and lots of random array accesses.
-     *
-     * Also, AA&N can be optimized a lot:
-     * AA&N, rolled loops, double[][] -> double[][]: 21.162 seconds
-     * AA&N, rolled loops, float[][] -> float[][]: no improvement, but
-     *     at some stage Hotspot might start doing SIMD, so let's use float
-     * AA&N, rolled loops, float[] -> float[][]: 19.979 seconds
-     *     apparently 2D arrays are slow!
-     * AA&N, rolled loops, inlined 1D AA&N transform, float[]
-     *     transformed in-place: 18.5 seconds
-     * AA&N, previous version rewritten in C and compiled with "gcc -O3"
-     *     takes: 8.5 seconds (probably due to heavy use of SIMD)
-     *
-     * Other brave attempts:
-     * AA&N, best float version converted to 16:16 fixed point: 23.923 seconds
-     *
-     * Anyway the best float version stays.
-     * 18.5 seconds = 5.4 million transforms per second per core :-)
+     * 
+     * It would appear that Feig does 4-5% less operations, and multiplications
+     * are reduced from 80 in AA&N to only 54. But in practice:
+     * 
+     * Benchmarks, Intel Core i3 @ 2.93 GHz in long mode, 4 GB RAM Time taken to
+     * do 100 million IDCTs (less is better): Rene' Stöckel's Feig, int: 45.07
+     * seconds My Feig, floating point: 36.252 seconds AA&N, unrolled loops,
+     * double[][] -> double[][]: 25.167 seconds
+     * 
+     * Clearly Feig is hopeless. I suspect the performance killer is simply the
+     * weight of the algorithm: massive number of local variables, large code
+     * size, and lots of random array accesses.
+     * 
+     * Also, AA&N can be optimized a lot: AA&N, rolled loops, double[][] ->
+     * double[][]: 21.162 seconds AA&N, rolled loops, float[][] -> float[][]: no
+     * improvement, but at some stage Hotspot might start doing SIMD, so let's
+     * use float AA&N, rolled loops, float[] -> float[][]: 19.979 seconds
+     * apparently 2D arrays are slow! AA&N, rolled loops, inlined 1D AA&N
+     * transform, float[] transformed in-place: 18.5 seconds AA&N, previous
+     * version rewritten in C and compiled with "gcc -O3" takes: 8.5 seconds
+     * (probably due to heavy use of SIMD)
+     * 
+     * Other brave attempts: AA&N, best float version converted to 16:16 fixed
+     * point: 23.923 seconds
+     * 
+     * Anyway the best float version stays. 18.5 seconds = 5.4 million
+     * transforms per second per core :-)
      */
 
-    private static final float[] dctScalingFactors =
-    {
-        (float)(0.5 / Math.sqrt(2.0)),
-        (float)(0.25 / Math.cos(Math.PI/16.0)),
-        (float)(0.25 / Math.cos(2.0*Math.PI/16.0)),
-        (float)(0.25 / Math.cos(3.0*Math.PI/16.0)),
-        (float)(0.25 / Math.cos(4.0*Math.PI/16.0)),
-        (float)(0.25 / Math.cos(5.0*Math.PI/16.0)),
-        (float)(0.25 / Math.cos(6.0*Math.PI/16.0)),
-        (float)(0.25 / Math.cos(7.0*Math.PI/16.0)),
-    };
-
-    private static final float[] idctScalingFactors =
-    {
-        (float)(2.0 * 4.0 / Math.sqrt(2.0) * 0.0625),
-        (float)(4.0 * Math.cos(Math.PI/16.0) * 0.125),
-        (float)(4.0 * Math.cos(2.0*Math.PI/16.0) * 0.125),
-        (float)(4.0 * Math.cos(3.0*Math.PI/16.0) * 0.125),
-        (float)(4.0 * Math.cos(4.0*Math.PI/16.0) * 0.125),
-        (float)(4.0 * Math.cos(5.0*Math.PI/16.0) * 0.125),
-        (float)(4.0 * Math.cos(6.0*Math.PI/16.0) * 0.125),
-        (float)(4.0 * Math.cos(7.0*Math.PI/16.0) * 0.125),
-    };
-
-    private static final float A1 = (float)(Math.cos(2.0*Math.PI/8.0));
-    private static final float A2 = (float)(Math.cos(Math.PI/8.0) - Math.cos(3.0*Math.PI/8.0));
+    private static final float[] dctScalingFactors = {
+            (float) (0.5 / Math.sqrt(2.0)),
+            (float) (0.25 / Math.cos(Math.PI / 16.0)),
+            (float) (0.25 / Math.cos(2.0 * Math.PI / 16.0)),
+            (float) (0.25 / Math.cos(3.0 * Math.PI / 16.0)),
+            (float) (0.25 / Math.cos(4.0 * Math.PI / 16.0)),
+            (float) (0.25 / Math.cos(5.0 * Math.PI / 16.0)),
+            (float) (0.25 / Math.cos(6.0 * Math.PI / 16.0)),
+            (float) (0.25 / Math.cos(7.0 * Math.PI / 16.0)), };
+
+    private static final float[] idctScalingFactors = {
+            (float) (2.0 * 4.0 / Math.sqrt(2.0) * 0.0625),
+            (float) (4.0 * Math.cos(Math.PI / 16.0) * 0.125),
+            (float) (4.0 * Math.cos(2.0 * Math.PI / 16.0) * 0.125),
+            (float) (4.0 * Math.cos(3.0 * Math.PI / 16.0) * 0.125),
+            (float) (4.0 * Math.cos(4.0 * Math.PI / 16.0) * 0.125),
+            (float) (4.0 * Math.cos(5.0 * Math.PI / 16.0) * 0.125),
+            (float) (4.0 * Math.cos(6.0 * Math.PI / 16.0) * 0.125),
+            (float) (4.0 * Math.cos(7.0 * Math.PI / 16.0) * 0.125), };
+
+    private static final float A1 = (float) (Math.cos(2.0 * Math.PI / 8.0));
+    private static final float A2 = (float) (Math.cos(Math.PI / 8.0) - Math
+            .cos(3.0 * Math.PI / 8.0));
     private static final float A3 = A1;
-    private static final float A4 = (float)(Math.cos(Math.PI/8.0) + Math.cos(3.0*Math.PI/8.0));
-    private static final float A5 = (float)(Math.cos(3.0*Math.PI/8.0));
-
-    private static final float C2 = (float)(2.0 * Math.cos(Math.PI/8));
-    private static final float C4 = (float)(2.0 * Math.cos(2*Math.PI/8));
-    private static final float C6 = (float)(2.0 * Math.cos(3*Math.PI/8));
+    private static final float A4 = (float) (Math.cos(Math.PI / 8.0) + Math
+            .cos(3.0 * Math.PI / 8.0));
+    private static final float A5 = (float) (Math.cos(3.0 * Math.PI / 8.0));
+
+    private static final float C2 = (float) (2.0 * Math.cos(Math.PI / 8));
+    private static final float C4 = (float) (2.0 * Math.cos(2 * Math.PI / 8));
+    private static final float C6 = (float) (2.0 * Math.cos(3 * Math.PI / 8));
     private static final float Q = C2 - C6;
     private static final float R = C2 + C6;
 
-    public static void scaleQuantizationVector(float[] vector)
-    {
-        for (int x = 0; x < 8; x++)
-        {
+    public static void scaleQuantizationVector(float[] vector) {
+        for (int x = 0; x < 8; x++) {
             vector[x] *= dctScalingFactors[x];
         }
     }
 
-    public static void scaleDequantizationVector(float[] vector)
-    {
-        for (int x = 0; x < 8; x++)
-        {
+    public static void scaleDequantizationVector(float[] vector) {
+        for (int x = 0; x < 8; x++) {
             vector[x] *= idctScalingFactors[x];
         }
     }
 
-    public static void scaleQuantizationMatrix(float[] matrix)
-    {
-        for (int y = 0; y < 8; y++)
-        {
-            for (int x = 0; x < 8; x++)
-            {
-                matrix[8 * y + x] *= dctScalingFactors[y] * dctScalingFactors[x];
+    public static void scaleQuantizationMatrix(float[] matrix) {
+        for (int y = 0; y < 8; y++) {
+            for (int x = 0; x < 8; x++) {
+                matrix[8 * y + x] *= dctScalingFactors[y]
+                        * dctScalingFactors[x];
             }
         }
     }
 
-    public static void scaleDequantizationMatrix(float[] matrix)
-    {
-        for (int y = 0; y < 8; y++)
-        {
-            for (int x = 0; x < 8; x++)
-            {
-                matrix[8 * y + x] *= idctScalingFactors[y] * idctScalingFactors[x];
+    public static void scaleDequantizationMatrix(float[] matrix) {
+        for (int y = 0; y < 8; y++) {
+            for (int x = 0; x < 8; x++) {
+                matrix[8 * y + x] *= idctScalingFactors[y]
+                        * idctScalingFactors[x];
             }
         }
     }
 
     /**
-     * Fast forward Dct using AA&N.
-     * Taken from the book "JPEG still image data compression standard",
-     * by Pennebaker and Mitchell, chapter 4, figure "4-8".
+     * Fast forward Dct using AA&N. Taken from the book
+     * "JPEG still image data compression standard", by Pennebaker and Mitchell,
+     * chapter 4, figure "4-8".
      */
-    public static void forwardDCT8(float[] vector)
-    {
+    public static void forwardDCT8(float[] vector) {
         float a00 = vector[0] + vector[7];
         float a10 = vector[1] + vector[6];
         float a20 = vector[2] + vector[5];
@@ -170,11 +148,11 @@ public class Dct
 
         float a22 = a21 + a31;
 
-        float a23 = a22*A1;
-        float mul5 = (a61 - neg_a41)*A5;
-        float a43 = neg_a41*A2 - mul5;
-        float a53 = a51*A3;
-        float a63 = a61*A4 - mul5;
+        float a23 = a22 * A1;
+        float mul5 = (a61 - neg_a41) * A5;
+        float a43 = neg_a41 * A2 - mul5;
+        float a53 = a51 * A3;
+        float a63 = a61 * A4 - mul5;
 
         float a54 = a70 + a53;
         float a74 = a70 - a53;
@@ -189,23 +167,21 @@ public class Dct
         vector[3] = a74 - a43;
     }
 
-    public static void forwardDCT8x8(float[] matrix)
-    {
+    public static void forwardDCT8x8(float[] matrix) {
         float a00, a10, a20, a30, a40, a50, a60, a70;
         float a01, a11, a21, a31, neg_a41, a51, a61;
         float a22, a23, mul5, a43, a53, a63;
         float a54, a74;
 
-        for (int i = 0; i < 8; i++)
-        {
-            a00 = matrix[8*i] + matrix[8*i + 7];
-            a10 = matrix[8*i + 1] + matrix[8*i + 6];
-            a20 = matrix[8*i + 2] + matrix[8*i + 5];
-            a30 = matrix[8*i + 3] + matrix[8*i + 4];
-            a40 = matrix[8*i + 3] - matrix[8*i + 4];
-            a50 = matrix[8*i + 2] - matrix[8*i + 5];
-            a60 = matrix[8*i + 1] - matrix[8*i + 6];
-            a70 = matrix[8*i] - matrix[8*i + 7];
+        for (int i = 0; i < 8; i++) {
+            a00 = matrix[8 * i] + matrix[8 * i + 7];
+            a10 = matrix[8 * i + 1] + matrix[8 * i + 6];
+            a20 = matrix[8 * i + 2] + matrix[8 * i + 5];
+            a30 = matrix[8 * i + 3] + matrix[8 * i + 4];
+            a40 = matrix[8 * i + 3] - matrix[8 * i + 4];
+            a50 = matrix[8 * i + 2] - matrix[8 * i + 5];
+            a60 = matrix[8 * i + 1] - matrix[8 * i + 6];
+            a70 = matrix[8 * i] - matrix[8 * i + 7];
             a01 = a00 + a30;
             a11 = a10 + a20;
             a21 = a10 - a20;
@@ -214,25 +190,24 @@ public class Dct
             a51 = a50 + a60;
             a61 = a60 + a70;
             a22 = a21 + a31;
-            a23 = a22*A1;
-            mul5 = (a61 - neg_a41)*A5;
-            a43 = neg_a41*A2 - mul5;
-            a53 = a51*A3;
-            a63 = a61*A4 - mul5;
+            a23 = a22 * A1;
+            mul5 = (a61 - neg_a41) * A5;
+            a43 = neg_a41 * A2 - mul5;
+            a53 = a51 * A3;
+            a63 = a61 * A4 - mul5;
             a54 = a70 + a53;
             a74 = a70 - a53;
-            matrix[8*i] = a01 + a11;
-            matrix[8*i + 4] = a01 - a11;
-            matrix[8*i + 2] = a31 + a23;
-            matrix[8*i + 6] = a31 - a23;
-            matrix[8*i + 5] = a74 + a43;
-            matrix[8*i + 1] = a54 + a63;
-            matrix[8*i + 7] = a54 - a63;
-            matrix[8*i + 3] = a74 - a43;
+            matrix[8 * i] = a01 + a11;
+            matrix[8 * i + 4] = a01 - a11;
+            matrix[8 * i + 2] = a31 + a23;
+            matrix[8 * i + 6] = a31 - a23;
+            matrix[8 * i + 5] = a74 + a43;
+            matrix[8 * i + 1] = a54 + a63;
+            matrix[8 * i + 7] = a54 - a63;
+            matrix[8 * i + 3] = a74 - a43;
         }
 
-        for (int i = 0; i < 8; i++)
-        {
+        for (int i = 0; i < 8; i++) {
             a00 = matrix[i] + matrix[56 + i];
             a10 = matrix[8 + i] + matrix[48 + i];
             a20 = matrix[16 + i] + matrix[40 + i];
@@ -249,11 +224,11 @@ public class Dct
             a51 = a50 + a60;
             a61 = a60 + a70;
             a22 = a21 + a31;
-            a23 = a22*A1;
-            mul5 = (a61 - neg_a41)*A5;
-            a43 = neg_a41*A2 - mul5;
-            a53 = a51*A3;
-            a63 = a61*A4 - mul5;
+            a23 = a22 * A1;
+            mul5 = (a61 - neg_a41) * A5;
+            a43 = neg_a41 * A2 - mul5;
+            a53 = a51 * A3;
+            a63 = a61 * A4 - mul5;
             a54 = a70 + a53;
             a74 = a70 - a53;
             matrix[i] = a01 + a11;
@@ -269,13 +244,11 @@ public class Dct
 
     /**
      * Fast inverse Dct using AA&N. This is taken from the beautiful
-     * http://vsr.informatik.tu-chemnitz.de/~jan/MPEG/HTML/IDCT.html
-     * which gives easy equations and properly explains constants and
-     * scaling factors. Terms have been inlined and the negation
-     * optimized out of existence.
+     * http://vsr.informatik.tu-chemnitz.de/~jan/MPEG/HTML/IDCT.html which gives
+     * easy equations and properly explains constants and scaling factors. Terms
+     * have been inlined and the negation optimized out of existence.
      */
-    public static void inverseDCT8(float[] vector)
-    {
+    public static void inverseDCT8(float[] vector) {
         // B1
         float a2 = vector[2] - vector[6];
         float a3 = vector[2] + vector[6];
@@ -287,13 +260,13 @@ public class Dct
         float a7 = tmp1 + tmp2;
 
         // M
-        float tmp4 = C6*(a4 + a6);
+        float tmp4 = C6 * (a4 + a6);
         // Eliminate the negative:
         // float b4 = -Q*a4 - tmp4;
-        float neg_b4 = Q*a4 + tmp4;
-        float b6 = R*a6 - tmp4;
-        float b2 = a2*C4;
-        float b5 = a5*C4;
+        float neg_b4 = Q * a4 + tmp4;
+        float b6 = R * a6 - tmp4;
+        float b2 = a2 * C4;
+        float b5 = a5 * C4;
 
         // A1
         float tmp3 = b6 - a7;
@@ -322,51 +295,48 @@ public class Dct
         vector[7] = m4 - a7;
     }
 
-    public static void inverseDCT8x8(float[] matrix)
-    {
+    public static void inverseDCT8x8(float[] matrix) {
         float a2, a3, a4, tmp1, tmp2, a5, a6, a7;
         float tmp4, neg_b4, b6, b2, b5;
         float tmp3, n0, n1, n2, n3, neg_n5;
         float m3, m4, m5, m6, neg_m7;
 
-        for (int i = 0; i < 8; i++)
-        {
-            a2 = matrix[8*i + 2] - matrix[8*i + 6];
-            a3 = matrix[8*i + 2] + matrix[8*i + 6];
-            a4 = matrix[8*i + 5] - matrix[8*i + 3];
-            tmp1 = matrix[8*i + 1] + matrix[8*i + 7];
-            tmp2 = matrix[8*i + 3] + matrix[8*i + 5];
+        for (int i = 0; i < 8; i++) {
+            a2 = matrix[8 * i + 2] - matrix[8 * i + 6];
+            a3 = matrix[8 * i + 2] + matrix[8 * i + 6];
+            a4 = matrix[8 * i + 5] - matrix[8 * i + 3];
+            tmp1 = matrix[8 * i + 1] + matrix[8 * i + 7];
+            tmp2 = matrix[8 * i + 3] + matrix[8 * i + 5];
             a5 = tmp1 - tmp2;
-            a6 = matrix[8*i + 1] - matrix[8*i + 7];
+            a6 = matrix[8 * i + 1] - matrix[8 * i + 7];
             a7 = tmp1 + tmp2;
-            tmp4 = C6*(a4 + a6);
-            neg_b4 = Q*a4 + tmp4;
-            b6 = R*a6 - tmp4;
-            b2 = a2*C4;
-            b5 = a5*C4;
+            tmp4 = C6 * (a4 + a6);
+            neg_b4 = Q * a4 + tmp4;
+            b6 = R * a6 - tmp4;
+            b2 = a2 * C4;
+            b5 = a5 * C4;
             tmp3 = b6 - a7;
             n0 = tmp3 - b5;
-            n1 = matrix[8*i] - matrix[8*i + 4];
+            n1 = matrix[8 * i] - matrix[8 * i + 4];
             n2 = b2 - a3;
-            n3 = matrix[8*i] + matrix[8*i + 4];
+            n3 = matrix[8 * i] + matrix[8 * i + 4];
             neg_n5 = neg_b4;
             m3 = n1 + n2;
             m4 = n3 + a3;
             m5 = n1 - n2;
             m6 = n3 - a3;
             neg_m7 = neg_n5 + n0;
-            matrix[8*i] = m4 + a7;
-            matrix[8*i + 1] = m3 + tmp3;
-            matrix[8*i + 2] = m5 - n0;
-            matrix[8*i + 3] = m6 + neg_m7;
-            matrix[8*i + 4] = m6 - neg_m7;
-            matrix[8*i + 5] = m5 + n0;
-            matrix[8*i + 6] = m3 - tmp3;
-            matrix[8*i + 7] = m4 - a7;
+            matrix[8 * i] = m4 + a7;
+            matrix[8 * i + 1] = m3 + tmp3;
+            matrix[8 * i + 2] = m5 - n0;
+            matrix[8 * i + 3] = m6 + neg_m7;
+            matrix[8 * i + 4] = m6 - neg_m7;
+            matrix[8 * i + 5] = m5 + n0;
+            matrix[8 * i + 6] = m3 - tmp3;
+            matrix[8 * i + 7] = m4 - a7;
         }
 
-        for (int i = 0; i < 8; i++)
-        {
+        for (int i = 0; i < 8; i++) {
             a2 = matrix[16 + i] - matrix[48 + i];
             a3 = matrix[16 + i] + matrix[48 + i];
             a4 = matrix[40 + i] - matrix[24 + i];
@@ -375,11 +345,11 @@ public class Dct
             a5 = tmp1 - tmp2;
             a6 = matrix[8 + i] - matrix[56 + i];
             a7 = tmp1 + tmp2;
-            tmp4 = C6*(a4 + a6);
-            neg_b4 = Q*a4 + tmp4;
-            b6 = R*a6 - tmp4;
-            b2 = a2*C4;
-            b5 = a5*C4;
+            tmp4 = C6 * (a4 + a6);
+            neg_b4 = Q * a4 + tmp4;
+            b6 = R * a6 - tmp4;
+            b2 = a2 * C4;
+            b5 = a5 * C4;
             tmp3 = b6 - a7;
             n0 = tmp3 - b5;
             n1 = matrix[i] - matrix[32 + i];

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/decoder/JpegDecoder.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/decoder/JpegDecoder.java?rev=1342971&r1=1342970&r2=1342971&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/decoder/JpegDecoder.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/decoder/JpegDecoder.java Sat May 26 21:19:03 2012
@@ -38,19 +38,14 @@ import org.apache.commons.imaging.format
 import org.apache.commons.imaging.formats.jpeg.segments.SosSegment;
 
 public class JpegDecoder extends BinaryFileParser implements JpegUtils.Visitor,
-        JpegConstants
-{
+        JpegConstants {
     /*
-     * JPEG is an advanced image format that takes
-     * significant computation to decode. Keep
-     * decoding fast:
-     * - Don't allocate memory inside loops,
-     *   allocate it once and reuse.
-     * - Minimize calculations
-     *   per pixel and per block (using lookup tables
-     *   for YCbCr->RGB conversion doubled performance).
-     * - Math.round() is slow, use (int)(x+0.5f) instead
-     *   for positive numbers.
+     * JPEG is an advanced image format that takes significant computation to
+     * decode. Keep decoding fast: - Don't allocate memory inside loops,
+     * allocate it once and reuse. - Minimize calculations per pixel and per
+     * block (using lookup tables for YCbCr->RGB conversion doubled
+     * performance). - Math.round() is slow, use (int)(x+0.5f) instead for
+     * positive numbers.
      */
 
     private DqtSegment.QuantizationTable[] quantizationTables = new DqtSegment.QuantizationTable[4];
@@ -63,17 +58,13 @@ public class JpegDecoder extends BinaryF
     private ImageReadException imageReadException = null;
     private IOException ioException = null;
 
-    public boolean beginSOS()
-    {
+    public boolean beginSOS() {
         return true;
     }
 
-    public void visitSOS(int marker, byte markerBytes[],
-                    byte imageData[])
-    {
+    public void visitSOS(int marker, byte markerBytes[], byte imageData[]) {
         ByteArrayInputStream is = new ByteArrayInputStream(imageData);
-        try
-        {
+        try {
             int segmentLength = read2Bytes("segmentLength", is,
                     "Not a Valid JPEG File");
             byte[] sosSegmentBytes = readByteArray("SosSegment",
@@ -82,13 +73,14 @@ public class JpegDecoder extends BinaryF
 
             int hMax = 0;
             int vMax = 0;
-            for (int i = 0; i < sofnSegment.numberOfComponents; i++)
-            {
-                hMax = Math.max(hMax, sofnSegment.components[i].horizontalSamplingFactor);
-                vMax = Math.max(vMax, sofnSegment.components[i].verticalSamplingFactor);
+            for (int i = 0; i < sofnSegment.numberOfComponents; i++) {
+                hMax = Math.max(hMax,
+                        sofnSegment.components[i].horizontalSamplingFactor);
+                vMax = Math.max(vMax,
+                        sofnSegment.components[i].verticalSamplingFactor);
             }
-            int hSize = 8*hMax;
-            int vSize = 8*vMax;
+            int hSize = 8 * hMax;
+            int vSize = 8 * vMax;
 
             JpegInputStream bitInputStream = new JpegInputStream(is);
             int xMCUs = (sofnSegment.width + hSize - 1) / hSize;
@@ -100,63 +92,53 @@ public class JpegDecoder extends BinaryF
             int[] preds = new int[sofnSegment.numberOfComponents];
             ColorModel colorModel;
             WritableRaster raster;
-            if (sofnSegment.numberOfComponents == 3)
-            {
-                colorModel = new DirectColorModel(24,
-                        0x00ff0000, 0x0000ff00, 0x000000ff);
+            if (sofnSegment.numberOfComponents == 3) {
+                colorModel = new DirectColorModel(24, 0x00ff0000, 0x0000ff00,
+                        0x000000ff);
                 raster = WritableRaster.createPackedRaster(DataBuffer.TYPE_INT,
-                        sofnSegment.width, sofnSegment.height,
-                        new int[]{0x00ff0000,0x0000ff00,0x000000ff}, null);
-            }
-            else if (sofnSegment.numberOfComponents == 1)
-            {
-                colorModel = new DirectColorModel(24,
-                        0x00ff0000, 0x0000ff00, 0x000000ff);
+                        sofnSegment.width, sofnSegment.height, new int[] {
+                                0x00ff0000, 0x0000ff00, 0x000000ff }, null);
+            } else if (sofnSegment.numberOfComponents == 1) {
+                colorModel = new DirectColorModel(24, 0x00ff0000, 0x0000ff00,
+                        0x000000ff);
                 raster = WritableRaster.createPackedRaster(DataBuffer.TYPE_INT,
-                        sofnSegment.width, sofnSegment.height,
-                        new int[]{0x00ff0000,0x0000ff00,0x000000ff}, null);
+                        sofnSegment.width, sofnSegment.height, new int[] {
+                                0x00ff0000, 0x0000ff00, 0x000000ff }, null);
                 // FIXME: why do images come out too bright with CS_GRAY?
-//                colorModel = new ComponentColorModel(
-//                        ColorSpace.getInstance(ColorSpace.CS_GRAY), false, true,
-//                        Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
-//                raster = colorModel.createCompatibleWritableRaster(
-//                        sofnSegment.width, sofnSegment.height);
-            }
-            else
-                throw new ImageReadException(sofnSegment.numberOfComponents +
-                        " components are invalid or unsupported");
+                // colorModel = new ComponentColorModel(
+                // ColorSpace.getInstance(ColorSpace.CS_GRAY), false, true,
+                // Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
+                // raster = colorModel.createCompatibleWritableRaster(
+                // sofnSegment.width, sofnSegment.height);
+            } else
+                throw new ImageReadException(sofnSegment.numberOfComponents
+                        + " components are invalid or unsupported");
             DataBuffer dataBuffer = raster.getDataBuffer();
 
-            
-            for (int y1 = 0; y1 < vSize*yMCUs; y1 += vSize)
-            {
-                for (int x1 = 0; x1 < hSize*xMCUs; x1 += hSize)
-                {
+            for (int y1 = 0; y1 < vSize * yMCUs; y1 += vSize) {
+                for (int x1 = 0; x1 < hSize * xMCUs; x1 += hSize) {
                     readMCU(bitInputStream, preds, mcu);
                     rescaleMCU(mcu, hSize, vSize, scaledMCU);
                     int srcRowOffset = 0;
-                    int dstRowOffset = y1*sofnSegment.width + x1;
-                    for (int y2 = 0; y2 < vSize && y1 + y2 < sofnSegment.height; y2++)
-                    {
-                        for (int x2 = 0; x2 < hSize && x1 + x2 < sofnSegment.width; x2++)
-                        {
-                            if (scaledMCU.length == 3)
-                            {
+                    int dstRowOffset = y1 * sofnSegment.width + x1;
+                    for (int y2 = 0; y2 < vSize && y1 + y2 < sofnSegment.height; y2++) {
+                        for (int x2 = 0; x2 < hSize
+                                && x1 + x2 < sofnSegment.width; x2++) {
+                            if (scaledMCU.length == 3) {
                                 int Y = scaledMCU[0].samples[srcRowOffset + x2];
                                 int Cb = scaledMCU[1].samples[srcRowOffset + x2];
                                 int Cr = scaledMCU[2].samples[srcRowOffset + x2];
-                                int rgb = YCbCrConverter.convertYCbCrToRGB(Y, Cb, Cr);
+                                int rgb = YCbCrConverter.convertYCbCrToRGB(Y,
+                                        Cb, Cr);
                                 dataBuffer.setElem(dstRowOffset + x2, rgb);
-                            }
-                            else if (mcu.length == 1)
-                            {
+                            } else if (mcu.length == 1) {
                                 int Y = scaledMCU[0].samples[srcRowOffset + x2];
-                                dataBuffer.setElem(dstRowOffset + x2,
-                                        (Y << 16) | (Y << 8) | Y);
-                            }
-                            else
-                                throw new ImageReadException("Unsupported JPEG with " +
-                                        mcu.length + " components");
+                                dataBuffer.setElem(dstRowOffset + x2, (Y << 16)
+                                        | (Y << 8) | Y);
+                            } else
+                                throw new ImageReadException(
+                                        "Unsupported JPEG with " + mcu.length
+                                                + " components");
                         }
                         srcRowOffset += hSize;
                         dstRowOffset += sofnSegment.width;
@@ -165,55 +147,46 @@ public class JpegDecoder extends BinaryF
             }
             image = new BufferedImage(colorModel, raster,
                     colorModel.isAlphaPremultiplied(), new Properties());
-            //byte[] remainder = super.getStreamBytes(is);
-            //for (int i = 0; i < remainder.length; i++)
-            //{
-            //    System.out.println("" + i + " = " + Integer.toHexString(remainder[i]));
-            //}
-        }
-        catch (ImageReadException imageReadEx)
-        {
+            // byte[] remainder = super.getStreamBytes(is);
+            // for (int i = 0; i < remainder.length; i++)
+            // {
+            // System.out.println("" + i + " = " +
+            // Integer.toHexString(remainder[i]));
+            // }
+        } catch (ImageReadException imageReadEx) {
             imageReadException = imageReadEx;
-        }
-        catch (IOException ioEx)
-        {
+        } catch (IOException ioEx) {
             ioException = ioEx;
-        }
-        catch (RuntimeException ex)
-        {
+        } catch (RuntimeException ex) {
             // Corrupt images can throw NPE and IOOBE
-            imageReadException = new ImageReadException("Error parsing JPEG", ex);
+            imageReadException = new ImageReadException("Error parsing JPEG",
+                    ex);
         }
     }
 
     public boolean visitSegment(int marker, byte[] markerBytes,
-            int segmentLength, byte[] segmentLengthBytes,
-            byte[] segmentData) throws ImageReadException, IOException
-    {
-        final int[] sofnSegments = {
-            SOF0Marker,
-            SOF1Marker, SOF2Marker, SOF3Marker, SOF5Marker, SOF6Marker,
-            SOF7Marker, SOF9Marker, SOF10Marker, SOF11Marker, SOF13Marker,
-            SOF14Marker, SOF15Marker,
-        };
+            int segmentLength, byte[] segmentLengthBytes, byte[] segmentData)
+            throws ImageReadException, IOException {
+        final int[] sofnSegments = { SOF0Marker, SOF1Marker, SOF2Marker,
+                SOF3Marker, SOF5Marker, SOF6Marker, SOF7Marker, SOF9Marker,
+                SOF10Marker, SOF11Marker, SOF13Marker, SOF14Marker,
+                SOF15Marker, };
 
-        if (Arrays.binarySearch(sofnSegments, marker) >= 0)
-        {
+        if (Arrays.binarySearch(sofnSegments, marker) >= 0) {
             if (marker != SOF0Marker)
-                throw new ImageReadException("Only sequential, baseline JPEGs " +
-                        "are supported at the moment");
+                throw new ImageReadException("Only sequential, baseline JPEGs "
+                        + "are supported at the moment");
             sofnSegment = new SofnSegment(marker, segmentData);
-        }
-        else if (marker == DQTMarker)
-        {
+        } else if (marker == DQTMarker) {
             DqtSegment dqtSegment = new DqtSegment(marker, segmentData);
-            for (int i = 0; i < dqtSegment.quantizationTables.size(); i++)
-            {
-                DqtSegment.QuantizationTable table = dqtSegment.quantizationTables.get(i);
-                if (0 > table.destinationIdentifier ||
-                        table.destinationIdentifier >= quantizationTables.length)
-                    throw new ImageReadException("Invalid quantization table identifier " +
-                            table.destinationIdentifier);
+            for (int i = 0; i < dqtSegment.quantizationTables.size(); i++) {
+                DqtSegment.QuantizationTable table = dqtSegment.quantizationTables
+                        .get(i);
+                if (0 > table.destinationIdentifier
+                        || table.destinationIdentifier >= quantizationTables.length)
+                    throw new ImageReadException(
+                            "Invalid quantization table identifier "
+                                    + table.destinationIdentifier);
                 quantizationTables[table.destinationIdentifier] = table;
                 int[] quantizationMatrixInt = new int[64];
                 ZigZag.zigZagToBlock(table.elements, quantizationMatrixInt);
@@ -221,15 +194,11 @@ public class JpegDecoder extends BinaryF
                 for (int j = 0; j < 64; j++)
                     quantizationMatrixFloat[j] = quantizationMatrixInt[j];
                 Dct.scaleDequantizationMatrix(quantizationMatrixFloat);
-                scaledQuantizationTables[table.destinationIdentifier] =
-                        quantizationMatrixFloat;
+                scaledQuantizationTables[table.destinationIdentifier] = quantizationMatrixFloat;
             }
-        }
-        else if (marker == DHTMarker)
-        {
+        } else if (marker == DHTMarker) {
             DhtSegment dhtSegment = new DhtSegment(marker, segmentData);
-            for (int i = 0; i < dhtSegment.huffmanTables.size(); i++)
-            {
+            for (int i = 0; i < dhtSegment.huffmanTables.size(); i++) {
                 DhtSegment.HuffmanTable table = dhtSegment.huffmanTables.get(i);
                 DhtSegment.HuffmanTable[] tables;
                 if (table.tableClass == 0)
@@ -237,55 +206,47 @@ public class JpegDecoder extends BinaryF
                 else if (table.tableClass == 1)
                     tables = huffmanACTables;
                 else
-                    throw new ImageReadException("Invalid huffman table class " +
-                            table.tableClass);
-                if (0 > table.destinationIdentifier ||
-                        table.destinationIdentifier >= tables.length)
-                    throw new ImageReadException("Invalid huffman table identifier " +
-                            table.destinationIdentifier);
+                    throw new ImageReadException("Invalid huffman table class "
+                            + table.tableClass);
+                if (0 > table.destinationIdentifier
+                        || table.destinationIdentifier >= tables.length)
+                    throw new ImageReadException(
+                            "Invalid huffman table identifier "
+                                    + table.destinationIdentifier);
                 tables[table.destinationIdentifier] = table;
             }
         }
         return true;
     }
 
-    private void rescaleMCU(Block[] dataUnits, int hSize, int vSize, Block[] ret)
-    {
-        for (int i = 0; i < dataUnits.length; i++)
-        {
+    private void rescaleMCU(Block[] dataUnits, int hSize, int vSize, Block[] ret) {
+        for (int i = 0; i < dataUnits.length; i++) {
             Block block = dataUnits[i];
             if (block.width == hSize && block.height == vSize)
-                System.arraycopy(block.samples, 0, ret[i].samples, 0, hSize*vSize);
-            else
-            {
+                System.arraycopy(block.samples, 0, ret[i].samples, 0, hSize
+                        * vSize);
+            else {
                 int hScale = hSize / block.width;
                 int vScale = vSize / block.height;
-                if (hScale == 2 && vScale == 2)
-                {
+                if (hScale == 2 && vScale == 2) {
                     int srcRowOffset = 0;
                     int dstRowOffset = 0;
-                    for (int y = 0; y < block.height; y++)
-                    {
-                        for (int x = 0; x < hSize; x++)
-                        {
+                    for (int y = 0; y < block.height; y++) {
+                        for (int x = 0; x < hSize; x++) {
                             int sample = block.samples[srcRowOffset + (x >> 1)];
                             ret[i].samples[dstRowOffset + x] = sample;
                             ret[i].samples[dstRowOffset + hSize + x] = sample;
                         }
                         srcRowOffset += block.width;
-                        dstRowOffset += 2*hSize;
+                        dstRowOffset += 2 * hSize;
                     }
-                }
-                else
-                {
+                } else {
                     // FIXME: optimize
                     int dstRowOffset = 0;
-                    for (int y = 0; y < vSize; y++)
-                    {
-                        for (int x = 0; x < hSize; x++)
-                        {
-                            ret[i].samples[dstRowOffset + x] =
-                                    block.samples[(y/vScale)*block.width + (x/hScale)];
+                    for (int y = 0; y < vSize; y++) {
+                        for (int x = 0; x < hSize; x++) {
+                            ret[i].samples[dstRowOffset + x] = block.samples[(y / vScale)
+                                    * block.width + (x / hScale)];
                         }
                         dstRowOffset += hSize;
                     }
@@ -293,19 +254,14 @@ public class JpegDecoder extends BinaryF
             }
         }
     }
-    
-    private Block[] allocateMCUMemory() throws ImageReadException
-    {
+
+    private Block[] allocateMCUMemory() throws ImageReadException {
         Block[] mcu = new Block[sosSegment.numberOfComponents];
-        for (int i = 0; i < sosSegment.numberOfComponents; i++)
-        {
+        for (int i = 0; i < sosSegment.numberOfComponents; i++) {
             SosSegment.Component scanComponent = sosSegment.components[i];
             SofnSegment.Component frameComponent = null;
-            for (int j = 0; j < sofnSegment.numberOfComponents; j++)
-            {
-                if (sofnSegment.components[j].componentIdentifier ==
-                    scanComponent.scanComponentSelector)
-                {
+            for (int j = 0; j < sofnSegment.numberOfComponents; j++) {
+                if (sofnSegment.components[j].componentIdentifier == scanComponent.scanComponentSelector) {
                     frameComponent = sofnSegment.components[j];
                     break;
                 }
@@ -313,8 +269,8 @@ public class JpegDecoder extends BinaryF
             if (frameComponent == null)
                 throw new ImageReadException("Invalid component");
             Block fullBlock = new Block(
-                    8*frameComponent.horizontalSamplingFactor,
-                    8*frameComponent.verticalSamplingFactor);
+                    8 * frameComponent.horizontalSamplingFactor,
+                    8 * frameComponent.verticalSamplingFactor);
             mcu[i] = fullBlock;
         }
         return mcu;
@@ -323,18 +279,14 @@ public class JpegDecoder extends BinaryF
     private int[] zz = new int[64];
     private int[] blockInt = new int[64];
     private float[] block = new float[64];
+
     private void readMCU(JpegInputStream is, int[] preds, Block[] mcu)
-            throws IOException, ImageReadException
-    {
-        for (int i = 0; i < sosSegment.numberOfComponents; i++)
-        {
+            throws IOException, ImageReadException {
+        for (int i = 0; i < sosSegment.numberOfComponents; i++) {
             SosSegment.Component scanComponent = sosSegment.components[i];
             SofnSegment.Component frameComponent = null;
-            for (int j = 0; j < sofnSegment.numberOfComponents; j++)
-            {
-                if (sofnSegment.components[j].componentIdentifier ==
-                    scanComponent.scanComponentSelector)
-                {
+            for (int j = 0; j < sofnSegment.numberOfComponents; j++) {
+                if (sofnSegment.components[j].componentIdentifier == scanComponent.scanComponentSelector) {
                     frameComponent = sofnSegment.components[j];
                     break;
                 }
@@ -342,13 +294,12 @@ public class JpegDecoder extends BinaryF
             if (frameComponent == null)
                 throw new ImageReadException("Invalid component");
             Block fullBlock = mcu[i];
-            for (int y = 0; y < frameComponent.verticalSamplingFactor; y++)
-            {
-                for (int x = 0; x < frameComponent.horizontalSamplingFactor; x++)
-                {
+            for (int y = 0; y < frameComponent.verticalSamplingFactor; y++) {
+                for (int x = 0; x < frameComponent.horizontalSamplingFactor; x++) {
                     Arrays.fill(zz, 0);
                     // page 104 of T.81
-                    int t = decode(is,
+                    int t = decode(
+                            is,
                             huffmanDCTables[scanComponent.dcCodingTableSelector]);
                     int diff = receive(t, is);
                     diff = extend(diff, t);
@@ -357,23 +308,20 @@ public class JpegDecoder extends BinaryF
 
                     // "Decode_AC_coefficients", figure F.13, page 106 of T.81
                     int k = 1;
-                    while (true)
-                    {
-                        int rs = decode(is,
+                    while (true) {
+                        int rs = decode(
+                                is,
                                 huffmanACTables[scanComponent.acCodingTableSelector]);
                         int ssss = rs & 0xf;
                         int rrrr = rs >> 4;
                         int r = rrrr;
 
-                        if (ssss == 0)
-                        {
+                        if (ssss == 0) {
                             if (r == 15)
                                 k += 16;
                             else
                                 break;
-                        }
-                        else
-                        {
+                        } else {
                             k += r;
 
                             // "Decode_ZZ(k)", figure F.14, page 107 of T.81
@@ -390,20 +338,17 @@ public class JpegDecoder extends BinaryF
                     final int shift = (1 << (sofnSegment.precision - 1));
                     final int max = (1 << sofnSegment.precision) - 1;
 
-                    float[] scaledQuantizationTable =
-                            scaledQuantizationTables[frameComponent.quantTabDestSelector];
+                    float[] scaledQuantizationTable = scaledQuantizationTables[frameComponent.quantTabDestSelector];
                     ZigZag.zigZagToBlock(zz, blockInt);
                     for (int j = 0; j < 64; j++)
                         block[j] = blockInt[j] * scaledQuantizationTable[j];
                     Dct.inverseDCT8x8(block);
 
-                    int dstRowOffset = 8*y*8*frameComponent.horizontalSamplingFactor +
-                            8*x;
+                    int dstRowOffset = 8 * y * 8
+                            * frameComponent.horizontalSamplingFactor + 8 * x;
                     int srcNext = 0;
-                    for (int yy = 0; yy < 8; yy++)
-                    {
-                        for (int xx = 0; xx < 8; xx++)
-                        {
+                    for (int yy = 0; yy < 8; yy++) {
+                        for (int xx = 0; xx < 8; xx++) {
                             float sample = block[srcNext++];
                             sample += shift;
                             int result;
@@ -415,38 +360,33 @@ public class JpegDecoder extends BinaryF
                                 result = fastRound(sample);
                             fullBlock.samples[dstRowOffset + xx] = result;
                         }
-                        dstRowOffset += 8*frameComponent.horizontalSamplingFactor;
+                        dstRowOffset += 8 * frameComponent.horizontalSamplingFactor;
                     }
                 }
             }
         }
     }
 
-    private static int fastRound(float x)
-    {
+    private static int fastRound(float x) {
         return (int) (x + 0.5f);
     }
 
-    private int extend(int v, int t)
-    {
+    private int extend(int v, int t) {
         // "EXTEND", section F.2.2.1, figure F.12, page 105 of T.81
         int vt = (1 << (t - 1));
-        while (v < vt)
-        {
+        while (v < vt) {
             vt = (-1 << t) + 1;
             v += vt;
         }
         return v;
     }
 
-    private int receive(int ssss, JpegInputStream is)
-            throws IOException, ImageReadException
-    {
+    private int receive(int ssss, JpegInputStream is) throws IOException,
+            ImageReadException {
         // "RECEIVE", section F.2.2.4, figure F.17, page 110 of T.81
         int i = 0;
         int v = 0;
-        while (i != ssss)
-        {
+        while (i != ssss) {
             i++;
             v = (v << 1) + is.nextBit();
         }
@@ -454,13 +394,11 @@ public class JpegDecoder extends BinaryF
     }
 
     private int decode(JpegInputStream is, DhtSegment.HuffmanTable huffmanTable)
-            throws IOException, ImageReadException
-    {
+            throws IOException, ImageReadException {
         // "DECODE", section F.2.2.3, figure F.16, page 109 of T.81
         int i = 1;
         int code = is.nextBit();
-        while (code > huffmanTable.maxCode[i])
-        {
+        while (code > huffmanTable.maxCode[i]) {
             i++;
             code = (code << 1) | is.nextBit();
         }
@@ -470,9 +408,8 @@ public class JpegDecoder extends BinaryF
         return value;
     }
 
-    public BufferedImage decode(ByteSource byteSource)
-            throws IOException, ImageReadException
-    {
+    public BufferedImage decode(ByteSource byteSource) throws IOException,
+            ImageReadException {
         JpegUtils jpegUtils = new JpegUtils();
         jpegUtils.traverseJFIF(byteSource, this);
         if (imageReadException != null)

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/decoder/JpegInputStream.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/decoder/JpegInputStream.java?rev=1342971&r1=1342970&r2=1342971&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/decoder/JpegInputStream.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/decoder/JpegInputStream.java Sat May 26 21:19:03 2012
@@ -21,39 +21,32 @@ import java.io.InputStream;
 import org.apache.commons.imaging.ImageReadException;
 import org.apache.commons.imaging.formats.jpeg.JpegConstants;
 
-public class JpegInputStream
-{
+public class JpegInputStream {
     // Figure F.18, F.2.2.5, page 111 of ITU-T T.81
     private final InputStream is;
     private int cnt = 0;
     private int b;
 
-    public JpegInputStream(InputStream is)
-    {
+    public JpegInputStream(InputStream is) {
         this.is = is;
     }
 
-    public int nextBit()
-            throws IOException, ImageReadException
-    {
-        if (cnt == 0)
-        {
+    public int nextBit() throws IOException, ImageReadException {
+        if (cnt == 0) {
             b = is.read();
             if (b < 0)
                 throw new ImageReadException("Premature End of File");
             cnt = 8;
-            if (b == 0xff)
-            {
+            if (b == 0xff) {
                 int b2 = is.read();
                 if (b2 < 0)
                     throw new ImageReadException("Premature End of File");
-                if (b2 != 0)
-                {
+                if (b2 != 0) {
                     if (b2 == (0xff & JpegConstants.DNLMarker))
                         throw new ImageReadException("DNL not yet supported");
                     else
-                        throw new ImageReadException("Invalid marker found " +
-                                "in entropy data");
+                        throw new ImageReadException("Invalid marker found "
+                                + "in entropy data");
                 }
             }
         }

Modified: commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/decoder/YCbCrConverter.java
URL: http://svn.apache.org/viewvc/commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/decoder/YCbCrConverter.java?rev=1342971&r1=1342970&r2=1342971&view=diff
==============================================================================
--- commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/decoder/YCbCrConverter.java (original)
+++ commons/proper/imaging/trunk/src/main/java/org/apache/commons/imaging/formats/jpeg/decoder/YCbCrConverter.java Sat May 26 21:19:03 2012
@@ -15,40 +15,36 @@
 
 package org.apache.commons.imaging.formats.jpeg.decoder;
 
-public class YCbCrConverter
-{
-    private static final int[] reds = new int[256*256];
-    private static final int[] blues = new int[256*256];
-    private static final int[] greens1 = new int[256*256];
-    private static final int[] greens2 = new int[256*512];
-    
-    static
-    {
+public class YCbCrConverter {
+    private static final int[] reds = new int[256 * 256];
+    private static final int[] blues = new int[256 * 256];
+    private static final int[] greens1 = new int[256 * 256];
+    private static final int[] greens2 = new int[256 * 512];
+
+    static {
         /*
-         * Why use (Cr << 8) | Y
-         * and not (Y << 8) | Cr as the index?
-         * Y changes often, while Cb and Cr is usually subsampled
-         * less often and repeats itself between adjacent pixels,
-         * so using it as the high order byte gives
-         * higher locality of reference.
+         * Why use (Cr << 8) | Y and not (Y << 8) | Cr as the index? Y changes
+         * often, while Cb and Cr is usually subsampled less often and repeats
+         * itself between adjacent pixels, so using it as the high order byte
+         * gives higher locality of reference.
          */
-        for (int Y = 0; Y < 256; Y++)
-        {
-            for (int Cr = 0; Cr < 256; Cr++)
-            {
-                int r = Y + fastRound(1.402f*(Cr-128));
-                if (r < 0) r = 0;
-                if (r > 255) r = 255;
+        for (int Y = 0; Y < 256; Y++) {
+            for (int Cr = 0; Cr < 256; Cr++) {
+                int r = Y + fastRound(1.402f * (Cr - 128));
+                if (r < 0)
+                    r = 0;
+                if (r > 255)
+                    r = 255;
                 reds[(Cr << 8) | Y] = r << 16;
             }
         }
-        for (int Y = 0; Y < 256; Y++)
-        {
-            for (int Cb = 0; Cb < 256; Cb++)
-            {
-                int b = Y + fastRound(1.772f*(Cb-128));
-                if (b < 0) b = 0;
-                if (b > 255) b = 255;
+        for (int Y = 0; Y < 256; Y++) {
+            for (int Cb = 0; Cb < 256; Cb++) {
+                int b = Y + fastRound(1.772f * (Cb - 128));
+                if (b < 0)
+                    b = 0;
+                if (b > 255)
+                    b = 255;
                 blues[(Cb << 8) | Y] = b;
             }
         }
@@ -61,36 +57,33 @@ public class YCbCrConverter
         // f(Cb, Cr) = 0.34414*(Cb-128) + 0.71414*(Cr-128)
         // Cb and Cr terms each vary from 255-128 = 127 to 0-128 = -128
         // Linear function, so only examine endpoints:
-        // Cb term   Cr term   Result
-        // 127       127       134.4
-        // -128      -128      -135.4
-        // 127       -128      -47.7
-        // -128      127       46.6
+        // Cb term Cr term Result
+        // 127 127 134.4
+        // -128 -128 -135.4
+        // 127 -128 -47.7
+        // -128 127 46.6
         // Thus with -135 being the minimum and 134 the maximum,
         // there is a range of 269 values,
         // and 135 needs to be added to make it zero-based.
 
         // As for Y - f(Cb, Cr)
         // the range becomes:
-        // Y     f(Cb, Cr)
-        // 255   -135
-        // 255   134
-        // 0     -135
-        // 0     134
+        // Y f(Cb, Cr)
+        // 255 -135
+        // 255 134
+        // 0 -135
+        // 0 134
         // thus the range is [-134,390] and has 524 values
         // but is clamped to [0, 255]
-        for (int Cb = 0; Cb < 256; Cb++)
-        {
-            for (int Cr = 0; Cr < 256; Cr++)
-            {
-                int value = fastRound(0.34414f*(Cb-128) + 0.71414f*(Cr-128));
+        for (int Cb = 0; Cb < 256; Cb++) {
+            for (int Cr = 0; Cr < 256; Cr++) {
+                int value = fastRound(0.34414f * (Cb - 128) + 0.71414f
+                        * (Cr - 128));
                 greens1[(Cb << 8) | Cr] = value + 135;
             }
         }
-        for (int Y = 0; Y < 256; Y++)
-        {
-            for (int value = 0; value < 270; value++)
-            {
+        for (int Y = 0; Y < 256; Y++) {
+            for (int value = 0; value < 270; value++) {
                 int green = Y - (value - 135);
                 if (green < 0)
                     green = 0;
@@ -101,14 +94,12 @@ public class YCbCrConverter
         }
     }
 
-    private static int fastRound(float x)
-    {
+    private static int fastRound(float x) {
         // Math.round() is very slow
         return (int) (x + 0.5f);
     }
 
-    public static int convertYCbCrToRGB(int Y, int Cb, int Cr)
-    {
+    public static int convertYCbCrToRGB(int Y, int Cb, int Cr) {
         int r = reds[(Cr << 8) | Y];
         int g1 = greens1[(Cb << 8) | Cr];
         int g = greens2[(g1 << 8) | Y];



Mime
View raw message