png_jpg => png_gif_jpg, because an swf can contain gif, too, and ffdec already expoted them as gif when png_jpg was selected

This commit is contained in:
honfika@gmail.com
2015-05-15 20:14:18 +02:00
parent 6cf53543d1
commit 8fa0698d59
21 changed files with 488 additions and 470 deletions

View File

@@ -1,107 +1,108 @@
/*
* Copyright (C) 2010-2015 JPEXS, All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library.
*/
package com.jpexs.decompiler.flash.exporters;
import com.jpexs.decompiler.flash.AbortRetryIgnoreHandler;
import com.jpexs.decompiler.flash.EventListener;
import com.jpexs.decompiler.flash.RetryTask;
import com.jpexs.decompiler.flash.exporters.modes.ImageExportMode;
import com.jpexs.decompiler.flash.exporters.settings.ImageExportSettings;
import com.jpexs.decompiler.flash.helpers.BMPFile;
import com.jpexs.decompiler.flash.helpers.ImageHelper;
import com.jpexs.decompiler.flash.tags.Tag;
import com.jpexs.decompiler.flash.tags.base.ImageTag;
import com.jpexs.helpers.Helper;
import com.jpexs.helpers.Path;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
/**
*
* @author JPEXS
*/
public class ImageExporter {
public List<File> exportImages(AbortRetryIgnoreHandler handler, String outdir, List<Tag> tags, ImageExportSettings settings, EventListener evl) throws IOException {
List<File> ret = new ArrayList<>();
if (tags.isEmpty()) {
return ret;
}
File foutdir = new File(outdir);
Path.createDirectorySafe(foutdir);
int count = 0;
for (Tag t : tags) {
if (t instanceof ImageTag) {
count++;
}
}
if (count == 0) {
return ret;
}
int currentIndex = 1;
for (Tag t : tags) {
if (t instanceof ImageTag) {
if (evl != null) {
evl.handleExportingEvent("image", currentIndex, count, t.getName());
}
final ImageTag imageTag = (ImageTag) t;
String fileFormat = imageTag.getImageFormat().toUpperCase(Locale.ENGLISH);
if (settings.mode == ImageExportMode.PNG) {
fileFormat = "png";
}
if (settings.mode == ImageExportMode.JPEG) {
fileFormat = "jpg";
}
if (settings.mode == ImageExportMode.BMP) {
fileFormat = "bmp";
}
{
final File file = new File(outdir + File.separator + Helper.makeFileName(imageTag.getCharacterExportFileName() + "." + fileFormat));
final String ffileFormat = fileFormat;
new RetryTask(() -> {
if (ffileFormat.equals("bmp")) {
BMPFile.saveBitmap(imageTag.getImage().getBufferedImage(), file);
} else {
ImageHelper.write(imageTag.getImage().getBufferedImage(), ffileFormat.toUpperCase(Locale.ENGLISH), file);
}
}, handler).run();
ret.add(file);
}
if (evl != null) {
evl.handleExportedEvent("image", currentIndex, count, t.getName());
}
currentIndex++;
}
}
return ret;
}
}
/*
* Copyright (C) 2010-2015 JPEXS, All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library.
*/
package com.jpexs.decompiler.flash.exporters;
import com.jpexs.decompiler.flash.AbortRetryIgnoreHandler;
import com.jpexs.decompiler.flash.EventListener;
import com.jpexs.decompiler.flash.RetryTask;
import com.jpexs.decompiler.flash.exporters.modes.ImageExportMode;
import com.jpexs.decompiler.flash.exporters.settings.ImageExportSettings;
import com.jpexs.decompiler.flash.helpers.BMPFile;
import com.jpexs.decompiler.flash.helpers.ImageHelper;
import com.jpexs.decompiler.flash.tags.Tag;
import com.jpexs.decompiler.flash.tags.base.ImageTag;
import com.jpexs.helpers.Helper;
import com.jpexs.helpers.Path;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
/**
*
* @author JPEXS
*/
public class ImageExporter {
public List<File> exportImages(AbortRetryIgnoreHandler handler, String outdir, List<Tag> tags, ImageExportSettings settings, EventListener evl) throws IOException {
List<File> ret = new ArrayList<>();
if (tags.isEmpty()) {
return ret;
}
File foutdir = new File(outdir);
Path.createDirectorySafe(foutdir);
int count = 0;
for (Tag t : tags) {
if (t instanceof ImageTag) {
count++;
}
}
if (count == 0) {
return ret;
}
int currentIndex = 1;
for (Tag t : tags) {
if (t instanceof ImageTag) {
if (evl != null) {
evl.handleExportingEvent("image", currentIndex, count, t.getName());
}
final ImageTag imageTag = (ImageTag) t;
String fileFormat = imageTag.getImageFormat().toUpperCase(Locale.ENGLISH);
if (settings.mode == ImageExportMode.PNG) {
fileFormat = "png";
}
if (settings.mode == ImageExportMode.JPEG) {
fileFormat = "jpg";
}
if (settings.mode == ImageExportMode.BMP) {
fileFormat = "bmp";
}
{
final File file = new File(outdir + File.separator + Helper.makeFileName(imageTag.getCharacterExportFileName() + "." + fileFormat));
final String ffileFormat = fileFormat;
new RetryTask(() -> {
if (ffileFormat.equals("bmp")) {
BMPFile.saveBitmap(imageTag.getImage().getBufferedImage(), file);
} else {
ImageHelper.write(imageTag.getImage().getBufferedImage(), ffileFormat.toUpperCase(Locale.ENGLISH), file);
}
}, handler).run();
ret.add(file);
}
if (evl != null) {
evl.handleExportedEvent("image", currentIndex, count, t.getName());
}
currentIndex++;
}
}
return ret;
}
}

View File

@@ -1,18 +1,19 @@
/*
* Copyright (C) 2010-2015 JPEXS, All rights reserved.
*
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3.0 of the License, or (at your option) any later version.
*
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library.
* License along with this library.
*/
package com.jpexs.decompiler.flash.exporters.modes;
/**
@@ -21,5 +22,5 @@ package com.jpexs.decompiler.flash.exporters.modes;
*/
public enum ImageExportMode {
PNG_GIF_JPEG, PNG, JPEG, BMP
}

View File

@@ -65,6 +65,13 @@ public class DefineBitsJPEG3Tag extends ImageTag implements AloneTag {
this.characterID = characterId;
}
private byte[] createEmptyImage() {
BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
ByteArrayOutputStream bitmapDataOS = new ByteArrayOutputStream();
ImageHelper.write(img, "JPG", bitmapDataOS);
return bitmapDataOS.toByteArray();
}
@Override
public void setImage(byte[] data) throws IOException {
if (ImageTag.getImageFormat(data).equals("jpg")) {

View File

@@ -77,6 +77,13 @@ public class DefineBitsJPEG4Tag extends ImageTag implements AloneTag {
return fmt;
}
private byte[] createEmptyImage() {
BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
ByteArrayOutputStream bitmapDataOS = new ByteArrayOutputStream();
ImageHelper.write(img, "JPG", bitmapDataOS);
return bitmapDataOS.toByteArray();
}
@Override
public void setImage(byte[] data) {
imageData = new ByteArrayRange(data);
@@ -131,28 +138,6 @@ public class DefineBitsJPEG4Tag extends ImageTag implements AloneTag {
return null;
}
/**
* Gets data bytes
*
* @return Bytes of data
*/
@Override
public byte[] getData() {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream os = baos;
SWFOutputStream sos = new SWFOutputStream(os, getVersion());
try {
sos.writeUI16(characterID);
sos.writeUI32(imageData.getLength());
sos.writeUI16(deblockParam);
sos.write(imageData);
sos.write(bitmapAlphaData);
} catch (IOException e) {
throw new Error("This should never happen.", e);
}
return baos.toByteArray();
}
/**
* Constructor
*
@@ -161,7 +146,7 @@ public class DefineBitsJPEG4Tag extends ImageTag implements AloneTag {
public DefineBitsJPEG4Tag(SWF swf) {
super(swf, ID, NAME, null);
characterID = swf.getNextCharacterId();
imageData = ByteArrayRange.EMPTY;
imageData = new ByteArrayRange(createEmptyImage());
bitmapAlphaData = ByteArrayRange.EMPTY;
}
@@ -185,4 +170,26 @@ public class DefineBitsJPEG4Tag extends ImageTag implements AloneTag {
imageData = sis.readByteRangeEx(alphaDataOffset, "imageData");
bitmapAlphaData = sis.readByteRangeEx(sis.available(), "bitmapAlphaData");
}
/**
* Gets data bytes
*
* @return Bytes of data
*/
@Override
public byte[] getData() {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream os = baos;
SWFOutputStream sos = new SWFOutputStream(os, getVersion());
try {
sos.writeUI16(characterID);
sos.writeUI32(imageData.getLength());
sos.writeUI16(deblockParam);
sos.write(imageData);
sos.write(bitmapAlphaData);
} catch (IOException e) {
throw new Error("This should never happen.", e);
}
return baos.toByteArray();
}
}

View File

@@ -74,9 +74,11 @@ public abstract class ImageTag extends CharacterTag implements DrawableTag {
if (hasErrorHeader(data)) {
return "jpg";
}
if (data.getLength() > 2 && ((data.get(0) & 0xff) == 0xff) && ((data.get(1) & 0xff) == 0xd8)) {
return "jpg";
}
if (data.getLength() > 6 && ((data.get(0) & 0xff) == 0x47) && ((data.get(1) & 0xff) == 0x49) && ((data.get(2) & 0xff) == 0x46) && ((data.get(3) & 0xff) == 0x38) && ((data.get(4) & 0xff) == 0x39) && ((data.get(5) & 0xff) == 0x61)) {
return "gif";
}