kinow commented on a change in pull request #47: IMAGING-134: Fix JPEG handling 
for files encoded with RST markers
URL: https://github.com/apache/commons-imaging/pull/47#discussion_r291793143
 
 

 ##########
 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 ) {
 
 Review comment:
   `while (pos < scanPayload.length) {`, spaces

----------------------------------------------------------------
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


With regards,
Apache Git Services

Reply via email to