From 49f23c04b108e205889469e091e3c6104dca8a98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jindra=20Pet=C5=99=C3=ADk?= Date: Sat, 5 Nov 2022 20:13:53 +0100 Subject: [PATCH] Fixed #1455 errors in JPEG - more SOI in the file --- CHANGELOG.md | 3 +- .../src/com/jpexs/helpers/JpegFixer.java | 12 +++-- .../test/com/jpexs/helpers/JpegFixerTest.java | 54 +++++++++---------- 3 files changed, 37 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1bcb6a80a..0b1c26a73 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ All notable changes to this project will be documented in this file. ### Fixed - Flash viewer - subtract blend mode -- [#1712], [#1857] JPEG images errors fixer +- [#1712], [#1857], [#1455] JPEG images errors fixer - Ignore missing font on DefineEditText - GFX: Drawing missing DefineExternalImage/2, DefineSubImage as red instead of throwing exception - GFX: DefineExternalImage2 properly saving characterId @@ -2484,6 +2484,7 @@ All notable changes to this project will be documented in this file. [#1849]: https://www.free-decompiler.com/flash/issues/1849 [#1712]: https://www.free-decompiler.com/flash/issues/1712 [#1857]: https://www.free-decompiler.com/flash/issues/1857 +[#1455]: https://www.free-decompiler.com/flash/issues/1455 [#1818]: https://www.free-decompiler.com/flash/issues/1818 [#1727]: https://www.free-decompiler.com/flash/issues/1727 [#1666]: https://www.free-decompiler.com/flash/issues/1666 diff --git a/libsrc/ffdec_lib/src/com/jpexs/helpers/JpegFixer.java b/libsrc/ffdec_lib/src/com/jpexs/helpers/JpegFixer.java index edeaef6ef..2053f5dd5 100644 --- a/libsrc/ffdec_lib/src/com/jpexs/helpers/JpegFixer.java +++ b/libsrc/ffdec_lib/src/com/jpexs/helpers/JpegFixer.java @@ -23,8 +23,10 @@ import java.io.OutputStream; /** * Fixes probles in some JPEGs to be readable by standard viewers. * - * It removes EOI markers followed by SOI markers. It does it also on the - * beginning of the file. + * It removes: + * 1) EOI markers followed by SOI markers + * 2) EOI SOI on the beginning of the file + * 3) Second or more SOI markers in the file * * @author JPEXS */ @@ -34,8 +36,8 @@ public class JpegFixer { public static final int EOI = 0xD9; public void fixJpeg(InputStream is, OutputStream os) throws IOException { - boolean prevEoi = false; - + boolean prevEoi = false; + int val = is.read(); if (val == -1) { return; @@ -119,6 +121,8 @@ public class JpegFixer { if (val == SOI && prevEoi) { //ignore, effectively removing EOI and SOI + } else if (val == SOI) { + //second or more SOI in the file, remove that too } else if (prevEoi) { os.write(0xFF); os.write(EOI); diff --git a/libsrc/ffdec_lib/test/com/jpexs/helpers/JpegFixerTest.java b/libsrc/ffdec_lib/test/com/jpexs/helpers/JpegFixerTest.java index 160e1e00b..94d9b5e46 100644 --- a/libsrc/ffdec_lib/test/com/jpexs/helpers/JpegFixerTest.java +++ b/libsrc/ffdec_lib/test/com/jpexs/helpers/JpegFixerTest.java @@ -30,51 +30,51 @@ import org.testng.annotations.Test; * @author JPEXS */ public class JpegFixerTest { - - private JpegFixer fixer = new JpegFixer(); - + + private JpegFixer fixer = new JpegFixer(); + @DataProvider(name = "bytes") public static Object[][] provideSamples() { return new Object[][]{ { - new byte[]{(byte)0xFF, (byte)EOI, (byte)0xFF, (byte)SOI, (byte)0xFF, (byte)SOI, (byte) 0x21, (byte)0xFF, (byte)EOI}, - new byte[]{(byte)0xFF, (byte)SOI, (byte) 0x21, (byte)0xFF, (byte)EOI}, - }, + new byte[]{(byte) 0xFF, (byte) EOI, (byte) 0xFF, (byte) SOI, (byte) 0xFF, (byte) SOI, (byte) 0x21, (byte) 0xFF, (byte) EOI}, + new byte[]{(byte) 0xFF, (byte) SOI, (byte) 0x21, (byte) 0xFF, (byte) EOI},}, { - new byte[]{(byte)0xFF, (byte)EOI, (byte)0xFF, (byte)SOI}, - new byte[]{(byte)0xFF, (byte)EOI, (byte)0xFF, (byte)SOI}, - }, + new byte[]{(byte) 0xFF, (byte) EOI, (byte) 0xFF, (byte) SOI}, + new byte[]{(byte) 0xFF, (byte) EOI, (byte) 0xFF, (byte) SOI},}, { - new byte[]{(byte)0xFF, (byte)EOI, (byte)0xFF, (byte)SOI, 0x23}, - new byte[]{(byte)0xFF, (byte)EOI, (byte)0xFF, (byte)SOI, 0x23}, - }, + new byte[]{(byte) 0xFF, (byte) EOI, (byte) 0xFF, (byte) SOI, 0x23}, + new byte[]{(byte) 0xFF, (byte) EOI, (byte) 0xFF, (byte) SOI, 0x23},}, { - new byte[]{(byte)0xFF, (byte)EOI}, - new byte[]{(byte)0xFF, (byte)EOI}, - }, + new byte[]{(byte) 0xFF, (byte) EOI}, + new byte[]{(byte) 0xFF, (byte) EOI},}, { - new byte[]{(byte)0xFF}, - new byte[]{(byte)0xFF}, - }, + new byte[]{(byte) 0xFF}, + new byte[]{(byte) 0xFF},}, { - new byte[]{(byte)0x26}, - new byte[]{(byte)0x26}, - }, + new byte[]{(byte) 0x26}, + new byte[]{(byte) 0x26},}, { new byte[]{}, - new byte[]{}, + new byte[]{},}, + { + new byte[]{(byte) 0xFF, (byte) SOI, 0x27, 0x37, 0x47, 0x57, (byte) 0xFF, (byte) EOI}, + new byte[]{(byte) 0xFF, (byte) SOI, 0x27, 0x37, 0x47, 0x57, (byte) 0xFF, (byte) EOI} }, { - new byte[]{(byte)0xFF, (byte)SOI, 0x27, 0x37, 0x47, 0x57, (byte)0xFF, (byte)EOI}, - new byte[]{(byte)0xFF, (byte)SOI, 0x27, 0x37, 0x47, 0x57, (byte)0xFF, (byte)EOI} + new byte[]{(byte) 0xFF, (byte) SOI, 0x28, 0x38, (byte) 0xFF, (byte) EOI, (byte) 0xFF, (byte) SOI, 0x48, 0x58, (byte) 0xFF, (byte) EOI}, + new byte[]{(byte) 0xFF, (byte) SOI, 0x28, 0x38, 0x48, 0x58, (byte) 0xFF, (byte) EOI} }, { - new byte[]{(byte)0xFF, (byte)SOI, 0x28, 0x38, (byte)0xFF, (byte)EOI, (byte)0xFF, (byte)SOI, 0x48, 0x58, (byte)0xFF, (byte)EOI}, - new byte[]{(byte)0xFF, (byte)SOI, 0x28, 0x38, 0x48, 0x58, (byte)0xFF, (byte)EOI} + new byte[]{(byte) 0xFF, (byte) SOI, 0x29, (byte) 0xFF, (byte) SOI, 0x39, (byte) 0xFF, (byte) EOI}, + new byte[]{(byte) 0xFF, (byte) SOI, 0x29, 0x39, (byte) 0xFF, (byte) EOI},}, + { + new byte[]{(byte) 0xFF, (byte) SOI, (byte) 0xFF, (byte) SOI, 0x2A, (byte) 0xFF, (byte) EOI}, + new byte[]{(byte) 0xFF, (byte) SOI, 0x2A, (byte) 0xFF, (byte) EOI} } }; } - + @Test(dataProvider = "bytes") public void testFixJpeg(byte[] inputData, byte[] expectedOutput) throws IOException { JpegFixer fixer = new JpegFixer();