Skip to content
This repository has been archived by the owner on Oct 13, 2021. It is now read-only.

Commit

Permalink
Separated load and calc workers
Browse files Browse the repository at this point in the history
  • Loading branch information
bonosoft committed Jul 16, 2018
1 parent 89386b2 commit e7a60a0
Show file tree
Hide file tree
Showing 9 changed files with 224 additions and 54 deletions.
2 changes: 1 addition & 1 deletion doc/release.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Release procedure
This is a checklist for creating a release

### Verision Number
### Version Number

- Update the version number (in src/photon/application/MainForm) of frame title located in the main funtion.
- Update the version number in makeapp.sh
Expand Down
Binary file modified out/artifacts/PhotonFileValidator.jar
Binary file not shown.
20 changes: 14 additions & 6 deletions src/photon/application/base/BaseForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import photon.application.dialogs.PreviewDialog;
import photon.application.dialogs.SaveDialog;
import photon.application.utilities.MainUtils;
import photon.application.utilities.PhotonCalcWorker;
import photon.application.utilities.PhotonLoadWorker;
import photon.file.PhotonFile;
import photon.file.parts.PhotonFileLayer;
Expand Down Expand Up @@ -86,12 +87,6 @@ public boolean accept(File file, String s)
PhotonLoadWorker loadWorker = new PhotonLoadWorker(me, file);
loadWorker.execute();

if (me.photonFile!=null) {
me.saveBtn.setEnabled(true);
me.informationBtn.setEnabled(true);
me.previewLargeBtn.setEnabled(true);
me.previewSmallBtn.setEnabled(true);
}
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
Expand Down Expand Up @@ -305,4 +300,17 @@ protected void changeLayer() {
me.layerSlider.setEnabled(true);
}
}

public void calc() {
if (me.photonFile!=null) {
me.saveBtn.setEnabled(true);
me.informationBtn.setEnabled(true);
me.previewLargeBtn.setEnabled(true);
me.previewSmallBtn.setEnabled(true);

PhotonCalcWorker calcWorker = new PhotonCalcWorker(me);
calcWorker.execute();

}
}
}
81 changes: 81 additions & 0 deletions src/photon/application/utilities/PhotonCalcWorker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* MIT License
*
* Copyright (c) 2018 Bonosoft
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package photon.application.utilities;

import photon.application.MainForm;
import photon.file.PhotonFile;
import photon.file.parts.IPhotonProgress;

import javax.swing.*;
import java.awt.*;
import java.io.File;

/**
* by bn on 16/07/2018.
*/
public class PhotonCalcWorker extends SwingWorker<Integer, String> implements IPhotonProgress {
private MainForm mainForm;
private File file;

public PhotonCalcWorker(MainForm mainForm) {
this.mainForm = mainForm;
mainForm.marginInfo.setText("");
}

@Override
protected void process(java.util.List<String> chunks) {
for (String str : chunks) {
mainForm.layerInfo.setText(str);
}
}

@Override
protected void done() {
mainForm.openBtn.setEnabled(true);
if (mainForm.photonFile!=null) {
mainForm.showFileInformation();
}
}

@Override
protected Integer doInBackground() throws Exception {
publish("Calculating layers...");
try {
mainForm.photonFile.setMargin(mainForm.margin);
mainForm.photonFile.calculate(this);
publish("Calculation Complete...");
} catch (Exception e) {
mainForm.marginInfo.setForeground(Color.red);
mainForm.marginInfo.setText("Could not calculate the file.");
return 0;
}
return 1;
}

@Override
public void showInfo(String str) {
publish(str);
}
}
3 changes: 2 additions & 1 deletion src/photon/application/utilities/PhotonLoadWorker.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@ protected void process(java.util.List<String> chunks) {

@Override
protected void done() {
mainForm.openBtn.setEnabled(true);
// mainForm.openBtn.setEnabled(true);
if (mainForm.photonFile!=null) {
mainForm.saveBtn.setEnabled(true);
mainForm.showFileInformation();
mainForm.calc();
}
}

Expand Down
38 changes: 24 additions & 14 deletions src/photon/file/PhotonFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

package photon.file;

import photon.application.utilities.PhotonCalcWorker;
import photon.file.parts.*;

import java.io.*;
Expand Down Expand Up @@ -67,20 +68,7 @@ public PhotonFile readFile(byte[] file, IPhotonProgress iPhotonProgress) throws
previewTwo = new PhotonFilePreview(photonFileHeader.getPreviewTwoOffsetAddress(), file);
iPhotonProgress.showInfo("Reading photon layers information...");
layers = PhotonFileLayer.readLayers(photonFileHeader, file, margin, iPhotonProgress);
islandList = null;
islandLayerCount = 0;
islandLayers = new ArrayList<>();

if (margin > 0) {
marginLayers = new ArrayList<>();
int i = 0;
for (PhotonFileLayer layer : layers) {
if (layer.doExtendMargin()) {
marginLayers.add(i);
}
i++;
}
}
resetMarginAndIslandInfo();

return this;
}
Expand Down Expand Up @@ -358,5 +346,27 @@ private int fixit(IPhotonProgress progres, PhotonLayer layer, PhotonFileLayer fi
}
return changed;
}

public void calculate(IPhotonProgress progres) throws Exception {
PhotonFileLayer.calculateLayers(photonFileHeader, layers, margin, progres);
resetMarginAndIslandInfo();
}

private void resetMarginAndIslandInfo() {
islandList = null;
islandLayerCount = 0;
islandLayers = new ArrayList<>();

if (margin > 0) {
marginLayers = new ArrayList<>();
int i = 0;
for (PhotonFileLayer layer : layers) {
if (layer.doExtendMargin()) {
marginLayers.add(i);
}
i++;
}
}
}
}

77 changes: 66 additions & 11 deletions src/photon/file/parts/PhotonFileLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class PhotonFileLayer {

private boolean extendsMargin;
private PhotonFileHeader photonFileHeader;
public boolean isCalculated;

private PhotonFileLayer(PhotonInputStream ds) throws Exception {
layerPositionZ = ds.readFloat();
Expand Down Expand Up @@ -132,6 +133,22 @@ public ArrayList<BitSet> unpackImage(int resolutionX) {
return unpackedImage;
}


private void unknownPixels(ArrayList<BitSet> unpackedImage, PhotonLayer photonLayer) {
photonLayer.clear();

for (int y = 0; y < unpackedImage.size(); y++) {
BitSet currentRow = unpackedImage.get(y);
if (currentRow != null) {
for (int x = 0; x < currentRow.length(); x++) {
if (currentRow.get(x)) {
photonLayer.supported(x, y);
}
}
}
}
}

private void calculate(ArrayList<BitSet> unpackedImage, ArrayList<BitSet> previousUnpackedImage, PhotonLayer photonLayer) {
islandRows = new ArrayList<>();
isLandsCount = 0;
Expand Down Expand Up @@ -159,14 +176,15 @@ private void calculate(ArrayList<BitSet> unpackedImage, ArrayList<BitSet> previo
isLandsCount = photonLayer.setIslands(islandRows);
}


public static List<PhotonFileLayer> readLayers(PhotonFileHeader photonFileHeader, byte[] file, int margin, IPhotonProgress iPhotonProgress) throws Exception {
PhotonLayer photonLayer = new PhotonLayer(photonFileHeader.getResolutionX(), photonFileHeader.getResolutionY());

List<PhotonFileLayer> layers = new ArrayList<>();


try (PhotonInputStream ds = new PhotonInputStream(new ByteArrayInputStream(file, photonFileHeader.getLayersDefinitionOffsetAddress(), file.length))) {
ArrayList<BitSet> previousUnpackedImage = null;
// ArrayList<BitSet> previousUnpackedImage = null;
for (int i = 0; i < photonFileHeader.getNumberOfLayers(); i++) {

iPhotonProgress.showInfo("Reading photon file layer " + i + "/" + photonFileHeader.getNumberOfLayers());
Expand All @@ -177,19 +195,21 @@ public static List<PhotonFileLayer> readLayers(PhotonFileHeader photonFileHeader

ArrayList<BitSet> unpackedImage = layer.unpackImage(photonFileHeader.getResolutionX());

if (margin > 0) {
layer.extendsMargin = layer.checkMagin(unpackedImage, margin);
}
// if (margin > 0) {
// layer.extendsMargin = layer.checkMagin(unpackedImage, margin);
// }

layer.calculate(unpackedImage, previousUnpackedImage, photonLayer);
// layer.unknownPixels(unpackedImage, photonLayer);

// layer.calculate(unpackedImage, previousUnpackedImage, photonLayer);

layers.add(layer);
if (previousUnpackedImage != null) {
previousUnpackedImage.clear();
}
previousUnpackedImage = unpackedImage;
// if (previousUnpackedImage != null) {
// previousUnpackedImage.clear();
// }
// previousUnpackedImage = unpackedImage;

layer.packedLayerImage = photonLayer.packLayerImage();
// layer.packedLayerImage = photonLayer.packLayerImage();

}
}
Expand All @@ -202,8 +222,40 @@ public static List<PhotonFileLayer> readLayers(PhotonFileHeader photonFileHeader
return layers;
}

public static void calculateLayers(PhotonFileHeader photonFileHeader, List<PhotonFileLayer> layers, int margin, IPhotonProgress iPhotonProgress) throws Exception {
PhotonLayer photonLayer = new PhotonLayer(photonFileHeader.getResolutionX(), photonFileHeader.getResolutionY());
ArrayList<BitSet> previousUnpackedImage = null;
int i = 0;
for (PhotonFileLayer layer : layers) {
ArrayList<BitSet> unpackedImage = layer.unpackImage(photonFileHeader.getResolutionX());

iPhotonProgress.showInfo("Calculating photon file layer " + i + "/" + photonFileHeader.getNumberOfLayers());

if (margin > 0) {
layer.extendsMargin = layer.checkMagin(unpackedImage, margin);
}

layer.unknownPixels(unpackedImage, photonLayer);

layer.calculate(unpackedImage, previousUnpackedImage, photonLayer);

if (previousUnpackedImage != null) {
previousUnpackedImage.clear();
}
previousUnpackedImage = unpackedImage;

layer.packedLayerImage = photonLayer.packLayerImage();
layer.isCalculated = true;

i++;
}
photonLayer.unLink();
System.gc();
}


public ArrayList<PhotonRow> getRows() {
return PhotonLayer.getRows(packedLayerImage, photonFileHeader.getResolutionX());
return PhotonLayer.getRows(packedLayerImage, photonFileHeader.getResolutionX(), isCalculated);
}

public ArrayList<BitSet> getIslandRows() {
Expand Down Expand Up @@ -303,4 +355,7 @@ public void saveLayer(PhotonLayer photonLayer) throws Exception {
isLandsCount = photonLayer.setIslands(islandRows);
}

public ArrayList<BitSet> getUnknownRows() {
return unpackImage(photonFileHeader.getResolutionX());
}
}
8 changes: 6 additions & 2 deletions src/photon/file/parts/PhotonLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,14 @@ private void add(ByteArrayOutputStream baos, byte current, int length) throws IO
* @param width The width of the current layer, used to change rows
* @return A list with the
*/
public static ArrayList<PhotonRow> getRows(byte[] packedLayerImage, int width) {
public static ArrayList<PhotonRow> getRows(byte[] packedLayerImage, int width, boolean isCalculated) {
Hashtable<Byte, Color> colors = new Hashtable<>();
colors.put(OFF, Color.black);
colors.put(SUPPORTED, Color.decode("#008800"));
if (isCalculated) {
colors.put(SUPPORTED, Color.decode("#008800"));
} else {
colors.put(SUPPORTED, Color.decode("#000088"));
}
colors.put(CONNECTED, Color.decode("#FFFF00"));
colors.put(ISLAND, Color.decode("#FF0000"));
ArrayList<PhotonRow> rows = new ArrayList<>();
Expand Down
Loading

0 comments on commit e7a60a0

Please sign in to comment.