From 987c05dc66d0125094bc7a7889e6ebba6c91addb Mon Sep 17 00:00:00 2001 From: Honfika Date: Wed, 23 Apr 2014 22:22:20 +0200 Subject: [PATCH] faster getUsedRatios --- trunk/src/com/jpexs/decompiler/flash/SWF.java | 219 +++++++++--------- 1 file changed, 109 insertions(+), 110 deletions(-) diff --git a/trunk/src/com/jpexs/decompiler/flash/SWF.java b/trunk/src/com/jpexs/decompiler/flash/SWF.java index 461fb22f5..e07f0d6fa 100644 --- a/trunk/src/com/jpexs/decompiler/flash/SWF.java +++ b/trunk/src/com/jpexs/decompiler/flash/SWF.java @@ -1402,66 +1402,65 @@ public final class SWF implements TreeItem, Timelined { } if (settings.mode == FramesExportMode.CANVAS) { - final Timeline ftim = tim; - final Color fbackgroundColor = backgroundColor; - new RetryTask(new RunnableIOEx() { - @Override - public void run() throws IOException { - File f = new File(foutdir + File.separator + "frames.js"); - File fmin = new File(foutdir + File.separator + "frames.min.js"); - int width = (int) (ftim.displayRect.getWidth() / SWF.unitDivisor); - int height = (int) (ftim.displayRect.getHeight() / SWF.unitDivisor); - try (FileOutputStream fos = new FileOutputStream(f)) { - fos.write(Utf8Helper.getBytes(CanvasShapeExporter.getJsPrefix())); - fos.write(Utf8Helper.getBytes("var frames = [];")); - Map library = new HashMap<>(); - framesToHtmlCanvas(SWF.unitDivisor, library, null, ftim, fframes, 0, null, 0, ftim.displayRect, new ColorTransform(), fbackgroundColor, 0); - - for (int c : library.keySet()) { - fos.write(Utf8Helper.getBytes("function character"+c+"(ctx,frame,ratio){\r\n")); - fos.write(Utf8Helper.getBytes(library.get(c))); - fos.write(Utf8Helper.getBytes("}\r\n")); - } - - - fos.write(Utf8Helper.getBytes("var frame = 0;\r\n")); - fos.write(Utf8Helper.getBytes("var frames = [];\r\n")); - for(int i:fframes){ - fos.write(Utf8Helper.getBytes("frames.push("+i+");\r\n")); - } - fos.write(Utf8Helper.getBytes("function nextFrame(ctx){\r\n")); - fos.write(Utf8Helper.getBytes("\tctx.clearRect(0,0,"+width+","+height+");\r\n")); - fos.write(Utf8Helper.getBytes("\tframe = (frame+1)%frames.length;\r\n")); - fos.write(Utf8Helper.getBytes("\tcharacter"+timeline.id+"(ctx,frames[frame],0);\r\n")); + final Timeline ftim = tim; + final Color fbackgroundColor = backgroundColor; + new RetryTask(new RunnableIOEx() { + @Override + public void run() throws IOException { + File f = new File(foutdir + File.separator + "frames.js"); + File fmin = new File(foutdir + File.separator + "frames.min.js"); + int width = (int) (ftim.displayRect.getWidth() / SWF.unitDivisor); + int height = (int) (ftim.displayRect.getHeight() / SWF.unitDivisor); + try (FileOutputStream fos = new FileOutputStream(f)) { + fos.write(Utf8Helper.getBytes(CanvasShapeExporter.getJsPrefix())); + fos.write(Utf8Helper.getBytes("var frames = [];")); + Map library = new HashMap<>(); + framesToHtmlCanvas(SWF.unitDivisor, library, null, ftim, fframes, 0, null, 0, ftim.displayRect, new ColorTransform(), fbackgroundColor, 0); + + for (int c : library.keySet()) { + fos.write(Utf8Helper.getBytes("function character" + c + "(ctx,frame,ratio){\r\n")); + fos.write(Utf8Helper.getBytes(library.get(c))); fos.write(Utf8Helper.getBytes("}\r\n")); - - fos.write(Utf8Helper.getBytes("window.setInterval(function(){nextFrame(ctx)},"+(int)(1000.0/timeline.swf.frameRate)+");\r\n")); } - - if(Configuration.packJavaScripts.get()){ - JPacker.main(new String[]{"-q","-b","62","-o",fmin.getAbsolutePath(),f.getAbsolutePath()}); - f.delete(); - }else{ - f.renameTo(fmin); + + fos.write(Utf8Helper.getBytes("var frame = 0;\r\n")); + fos.write(Utf8Helper.getBytes("var frames = [];\r\n")); + for (int i : fframes) { + fos.write(Utf8Helper.getBytes("frames.push(" + i + ");\r\n")); } - - File fh = new File(foutdir + File.separator + "frames.html"); - try (FileOutputStream fos = new FileOutputStream(fh); FileInputStream fis=new FileInputStream(fmin)) { - fos.write(Utf8Helper.getBytes(CanvasShapeExporter.getHtmlPrefix(width, height))); - byte buf[] = new byte[1000]; - int cnt; - while((cnt=fis.read(buf))>0){ - fos.write(buf,0,cnt); - } - fos.write(Utf8Helper.getBytes(";")); - fos.write(Utf8Helper.getBytes(CanvasShapeExporter.getHtmlSuffix())); - } - fmin.delete(); - - ret.add(f); + fos.write(Utf8Helper.getBytes("function nextFrame(ctx){\r\n")); + fos.write(Utf8Helper.getBytes("\tctx.clearRect(0,0," + width + "," + height + ");\r\n")); + fos.write(Utf8Helper.getBytes("\tframe = (frame+1)%frames.length;\r\n")); + fos.write(Utf8Helper.getBytes("\tcharacter" + timeline.id + "(ctx,frames[frame],0);\r\n")); + fos.write(Utf8Helper.getBytes("}\r\n")); + + fos.write(Utf8Helper.getBytes("window.setInterval(function(){nextFrame(ctx)}," + (int) (1000.0 / timeline.swf.frameRate) + ");\r\n")); } - }, handler).run(); - + + if (Configuration.packJavaScripts.get()) { + JPacker.main(new String[]{"-q", "-b", "62", "-o", fmin.getAbsolutePath(), f.getAbsolutePath()}); + f.delete(); + } else { + f.renameTo(fmin); + } + + File fh = new File(foutdir + File.separator + "frames.html"); + try (FileOutputStream fos = new FileOutputStream(fh); FileInputStream fis = new FileInputStream(fmin)) { + fos.write(Utf8Helper.getBytes(CanvasShapeExporter.getHtmlPrefix(width, height))); + byte buf[] = new byte[1000]; + int cnt; + while ((cnt = fis.read(buf)) > 0) { + fos.write(buf, 0, cnt); + } + fos.write(Utf8Helper.getBytes(";")); + fos.write(Utf8Helper.getBytes(CanvasShapeExporter.getHtmlSuffix())); + } + fmin.delete(); + + ret.add(f); + } + }, handler).run(); + return ret; } @@ -2086,53 +2085,53 @@ public final class SWF implements TreeItem, Timelined { } return ret; } - - private static void getUsedRatios(Timeline timeline, List frames,Map> usedRatios){ - if(frames == null){ + + private static void getUsedRatios(Timeline timeline, List frames, Map> usedRatios, HashSet processedCharacterIds) { + if (frames == null) { frames = new ArrayList<>(); - for(int i=0;i-1){ - if(!usedRatios.containsKey(layer.characterId)){ - usedRatios.put(layer.characterId, new ArrayList()); + if (layer.ratio > -1) { + List usedRatiosForCharacter = usedRatios.get(layer.characterId); + if (usedRatiosForCharacter == null) { + usedRatiosForCharacter = new ArrayList<>(); + usedRatios.put(layer.characterId, usedRatiosForCharacter); } - usedRatios.get(layer.characterId).add(layer.ratio); } CharacterTag character = timeline.swf.characters.get(layer.characterId); - if(character instanceof DefineSpriteTag){ - DefineSpriteTag sp = (DefineSpriteTag)character; - getUsedRatios(sp.getTimeline(),null,usedRatios); + if (character instanceof DefineSpriteTag && !processedCharacterIds.contains(character)) { + System.out.println(layer.characterId); + processedCharacterIds.add(character); + DefineSpriteTag sp = (DefineSpriteTag) character; + getUsedRatios(sp.getTimeline(), null, usedRatios, processedCharacterIds); } } } - + } - public static String framesToHtmlCanvas(double unitDivisor, Map library, Map> usedRatios,Timeline timeline, List frames, int time, DepthState stateUnderCursor, int mouseButton, RECT displayRect, ColorTransform colorTransform, Color backGroundColor, int level) throws IOException { - if(usedRatios == null){ + public static String framesToHtmlCanvas(double unitDivisor, Map library, Map> usedRatios, Timeline timeline, List frames, int time, DepthState stateUnderCursor, int mouseButton, RECT displayRect, ColorTransform colorTransform, Color backGroundColor, int level) throws IOException { + if (usedRatios == null) { usedRatios = new HashMap<>(); - getUsedRatios(timeline, frames, usedRatios); + getUsedRatios(timeline, frames, usedRatios, new HashSet()); } StringBuilder sb = new StringBuilder(); - if(frames == null){ + if (frames == null) { frames = new ArrayList<>(); - for(int i=0;i