[ 
https://issues.apache.org/jira/browse/IMAGING-134?focusedWorklogId=256363&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-256363
 ]

ASF GitHub Bot logged work on IMAGING-134:
------------------------------------------

                Author: ASF GitHub Bot
            Created on: 08/Jun/19 03:54
            Start Date: 08/Jun/19 03:54
    Worklog Time Spent: 10m 
      Work Description: kinow commented on pull request #47: IMAGING-134: Fix 
JPEG handling for files encoded with RST markers
URL: https://github.com/apache/commons-imaging/pull/47#discussion_r291793031
 
 

 ##########
 File path: 
src/main/java/org/apache/commons/imaging/formats/jpeg/decoder/JpegDecoder.java
 ##########
 @@ -393,6 +418,74 @@ private void readMCU(final JpegInputStream is, final 
int[] preds, final Block[]
         }
     }
 
+    /**
+     * Returns an array of JpegInputStream where each field contains the 
JpegInputStream
+     * for one interval.
+     * @param scanPayload array to read intervals from
+     * @return JpegInputStreams for all intervals, at least one stream is 
always provided
+     */
+    static JpegInputStream[] splitByRstMarkers(final int[] scanPayload) {
+        final List<Integer> intervalStarts = 
getIntervalStartPositions(scanPayload);
+        // get number of intervals in payload to init an array of approbiate 
length
+        final int intervalCount = intervalStarts.size();
+        JpegInputStream[] streams = new JpegInputStream[intervalCount];
+        for (int i = 0; i < intervalCount; i++) {
+            int from = intervalStarts.get(i);
+            int to;
+            if (i < intervalCount - 1) {
+                // because each restart marker needs two bytes the end of
+                // this interval is two bytes before the next interval starts
+                to = intervalStarts.get(i + 1) - 2;
+            } else { // the last interval ends with the array
+                to = scanPayload.length;
+            }
+            int[] interval = Arrays.copyOfRange(scanPayload, from, to);
+            streams[i] = new JpegInputStream(interval);
+        }
+        return streams;
+    }
+
+    /**
+     * Returns the positions of where each interval in the provided array 
starts.
+     * @param scanPayload array to examine
+     * @return the start positions, the number of start positions is also the 
count of intervals
+     * while the number of restart markers found = number of start positions - 
1
+     * because restart markers are between intervals
+     */
+    static List<Integer> getIntervalStartPositions(final int[] scanPayload) {
+        final List<Integer> intervalStarts = new ArrayList<Integer>();
+        intervalStarts.add( 0 );
+        boolean foundFF = false;
+        boolean foundD0toD7 = false;
+        int pos = 0;
+        while ( pos < scanPayload.length ) {
+            if ( foundFF == true ) {
+                // found 0xFF D0 .. 0xFF D7 => RST marker
+                if ( scanPayload[ pos ] >= ( 0xff & JpegConstants.RST0_MARKER 
) &&
+                    scanPayload[ pos ] <= ( 0xff & JpegConstants.RST7_MARKER ) 
) {
+                    foundD0toD7 = true;
+                } else { // found 0xFF followed by something else => no RST 
marker
+                    foundFF = false;
+                }
+            }
+
+            if ( scanPayload[ pos ] == 0xFF ) {
+                foundFF = true;
+            }
+
+            // true if one of the RST markers was found
+            if ( foundFF == true && foundD0toD7 == true ) {
 
 Review comment:
   `if (foundFF && foundD0toD7)`
 
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Issue Time Tracking
-------------------

    Worklog Id:     (was: 256363)
    Time Spent: 1h 40m  (was: 1.5h)

> Invalid marker found in entropy data
> ------------------------------------
>
>                 Key: IMAGING-134
>                 URL: https://issues.apache.org/jira/browse/IMAGING-134
>             Project: Commons Imaging
>          Issue Type: Bug
>          Components: Format: JPEG
>    Affects Versions: 1.0-alpha1
>            Reporter: John Hewson
>            Assignee: Bruno P. Kinoshita
>            Priority: Major
>             Fix For: Patch Needed
>
>         Attachments: image.jpeg
>
>          Time Spent: 1h 40m
>  Remaining Estimate: 0h
>
> I'm getting the following exception when trying to read a JPEG:
> Exception in thread "main" org.apache.commons.imaging.ImageReadException: 
> Invalid marker found in entropy data
>       at 
> org.apache.commons.imaging.formats.jpeg.decoder.JpegInputStream.nextBit(JpegInputStream.java:50)
>       at 
> org.apache.commons.imaging.formats.jpeg.decoder.JpegDecoder.decode(JpegDecoder.java:426)
>       at 
> org.apache.commons.imaging.formats.jpeg.decoder.JpegDecoder.readMCU(JpegDecoder.java:320)
>       at 
> org.apache.commons.imaging.formats.jpeg.decoder.JpegDecoder.visitSOS(JpegDecoder.java:124)
>       at 
> org.apache.commons.imaging.formats.jpeg.JpegUtils.traverseJFIF(JpegUtils.java:80)
>       at 
> org.apache.commons.imaging.formats.jpeg.decoder.JpegDecoder.decode(JpegDecoder.java:436)
>       at 
> org.apache.commons.imaging.formats.jpeg.JpegImageParser.getBufferedImage(JpegImageParser.java:98)
>       at 
> org.apache.commons.imaging.Imaging.getBufferedImage(Imaging.java:1378)
>       at 
> org.apache.commons.imaging.Imaging.getBufferedImage(Imaging.java:1341)



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to