App

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

AppCore.java
Go to the documentation of this file.
1 package mhr.appcore;
2
3 import mhr.appcore.bitmap.BitmapInfo;
4 import mhr.appcore.bitmap.NBitmap;
5 import mhr.appcore.commands.AppCommand;
6 import mhr.appcore.commands.core.ResizeImageCommand;
7 import mhr.appcore.commands.core.ResizeImageCommand.ResizeAction;
8 import mhr.appcore.image.Image;
9 import mhr.appcore.image.exceptions.ImageLoadingException;
10 import mhr.appcore.interfaces.ImageFile;
11 import mhr.appcore.interfaces.PDBitmap;
12 import mhr.appcore.interfaces.PDDisplayer;
13 import mhr.appcore.interfaces.PDExceptionFeedback;
14 import mhr.appcore.interfaces.PDImageDataPresentation;
15 import mhr.appcore.interpolators.Interpolator;
16 import mhr.appcore.tasks.RenderingTask;
17 import mhr.appcore.tasks.WorkingTask;
18 import mhr.appcore.tools.Tool;
19 import mhr.appcore.utils.Rect;
20
28 public class AppCore {
29
30 //===== INTERFACES, CLASSES, ENUMS ==========================================================================================================//
31 //----- NON-PUBLIC --------------------------------------------------------------------------------------------------------------------------//
32 //----- PUBLIC ------------------------------------------------------------------------------------------------------------------------------//
33
34 //===== FIELDS ==============================================================================================================================//
35 //----- NON-PUBLIC --------------------------------------------------------------------------------------------------------------------------//
36 protected PDDisplayer disp = null;
37 protected PDExceptionFeedback feedback = null;
38 protected Image img = null;
39
40 protected WorkingTask worker = null;
41 protected RenderingTask renderer = null;
42 protected NBitmap wrOutput = null;
43 protected PDBitmap output = null;
44
45 private boolean disposed = false;
46
47 protected Tool selectedTool;
48
49 protected ResizeAction resizeAction = null;
50 protected PDBitmap origOut = null;
51 protected NBitmap wrOrigOut = null;
52
53 //----- PUBLIC ------------------------------------------------------------------------------------------------------------------------------//
54
55 //===== CONSTRUCTORS, DESTRUCTORS, RELATED METHODS ==========================================================================================//
56 //----- NON-PUBLIC --------------------------------------------------------------------------------------------------------------------------//
57 @Override
58 protected void finalize() throws Throwable {
59 dispose();
60 super.finalize();
61 }
62
63 //----- PUBLIC ------------------------------------------------------------------------------------------------------------------------------//
70 public AppCore(PDDisplayer disp) {
71 if (disp == null) {
72 throw new NullPointerException("disp should not be null");
73 }
74 this.disp = disp;
75 }
76
80 public void dispose() {
81 if(worker != null) {
82 worker.dispose();
83 worker = null;
84 }
85
86 if (renderer != null) {
87 renderer.dispose();
88 renderer = null;
89 }
90
91 if (img != null) {
92 img.dispose();
93 img = null;
94 }
95
96 if (selectedTool != null) {
97 selectedTool.dispose();
98 selectedTool = null;
99 }
100
101 if (wrOutput != null) {
102 wrOutput.dispose();
103 wrOutput = null;
104 }
105 }
106
107 //===== METHODS =============================================================================================================================//
108 //----- NON-PUBLIC --------------------------------------------------------------------------------------------------------------------------//
109 //----- PUBLIC ------------------------------------------------------------------------------------------------------------------------------//
110
115 public void setWorkingThreadEH(Thread.UncaughtExceptionHandler handler) {
116 worker.setThreadEH(handler);
117 }
118
123 public void setRenderingThreadEH(Thread.UncaughtExceptionHandler handler) {
124 renderer.setThreadEH(handler);
125 }
126
131 public PDDisplayer getDisp() {
132 return disp;
133 }
134
139 public Image getImg() {
140 return img;
141 }
142
143 public NBitmap getWrOutput() {
144 return wrOutput;
145 }
146
151 public PDBitmap getOutput() {
152 return output;
153 }
154
159 public void setTool(Tool t) {
160 if (selectedTool != null) {
161 if (img.getCanvasOwner() == selectedTool) {
162 img.submitCanvas(selectedTool, false); // Pro případ, že by nástroj ještě byl asociován s obrazem.
163 }
164 selectedTool.dispose();
165 }
166 selectedTool = t;
167 }
168
173 public Tool getTool() {
174 return selectedTool;
175 }
176
181 public RenderingTask getRenderer() {
182 return renderer;
183 }
184
188 public void unsetExceptionFlag() {
189 worker.unsetExceptionFlag();
190 }
191
196 public void setImagePresentation(PDImageDataPresentation pres) {
197 img.setPresentation(pres);
198 }
199
204 public void setExceptionFeedback(PDExceptionFeedback feedback) {
205 this.feedback = feedback;
206 }
207
212 public PDExceptionFeedback getExceptionFeedback() {
213 return feedback;
214 }
215
220 public void addCommand(AppCommand command) {
221 worker.addCommand(command);
222 }
223
228 public void cropImage(Rect r) {
229 renderer.stop();
230 img.cropTo(r);
231 BitmapInfo oldi = output.getInfo();
232 PDBitmap oldOut = output;
233 output = oldOut.createBitmap(new BitmapInfo(img.getWidth(), img.getHeight(), oldi.channelCount, oldi.depth, oldi.colorMode, oldi.premultiplied));
234 wrOutput.dispose();
235 oldOut.dispose();
236 wrOutput = new NBitmap(output);
237 disp.setInput(output);
238 renderer.init();
239 renderer.start();
240 renderer.render();
241 }
242
247 public void resizeImage(ResizeImageCommand cmd) {
248 switch (cmd.action) {
249 case INIT:
250 if (resizeAction != null) {
251 throw new IllegalStateException("Bad resize state");
252 }
253 renderer.stop();
254 origOut = output;
255 wrOrigOut = wrOutput;
256 resizeAction = ResizeAction.INIT;
257 break;
258 case PREVIEW:
259 if (resizeAction != ResizeAction.INIT) {
260 throw new IllegalStateException("Bad resize state");
261 }
262 BitmapInfo oldi = origOut.getInfo();
263 output = origOut.createBitmap(new BitmapInfo(cmd.newWidth, cmd.newHeight,oldi.channelCount, oldi.depth, oldi.colorMode, oldi.premultiplied));
264 wrOutput = new NBitmap(output);
265 Interpolator.resampleTo(wrOrigOut, wrOutput, cmd.iType, cmd.force);
266 wrOutput.dispose();
267 wrOutput = null;
268 disp.setInput(output);
269 disp.redraw();
270 break;
271
272 case COMMIT:
273 if (resizeAction != ResizeAction.INIT) {
274 throw new IllegalStateException("Bad resize state");
275 }
276 img.resizeTo(cmd.newWidth, cmd.newHeight, cmd.iType, cmd.force);
277 BitmapInfo oi = origOut.getInfo();
278 output = origOut.createBitmap(new BitmapInfo(cmd.newWidth, cmd.newHeight,oi.channelCount, oi.depth, oi.colorMode, oi.premultiplied));
279 wrOutput = new NBitmap(output);
280 wrOrigOut.dispose();
281 wrOrigOut = null;
282 disp.setInput(output);
283 renderer.init();
284 renderer.start();
285 renderer.render();
286 resizeAction = null;
287 break;
288
289 case CANCEL:
290 if (resizeAction == null) {
291 return;
292 }
293 wrOutput = wrOrigOut;
294 output = origOut;
295 disp.setInput(output);
296 renderer.init();
297 renderer.start();
298 renderer.render();
299 resizeAction = null;
300 break;
301 }
302 }
303
308 public void openImage(PDBitmap bitmap) {
309 dispose(); // Zastaví a uklidí veškeré zbytky...
310 img = new Image(bitmap);
311 output = bitmap;
312 wrOutput = new NBitmap(output);
313 disp.setInput(output);
314 start();
315 }
316
321 public void openImage(ImageFile f) {
322 dispose(); // Zastaví a uklidí veškeré zbytky...
323 img = new Image(f);
324 output = f.loadOutput();
325 wrOutput = new NBitmap(output);
326 disp.setInput(output);
327 start();
328 }
329
330 public void suspend() {
331 }
332
333 public void wake() {
334 }
335
339 public void start() {
340 if (worker != null) {
341 worker.dispose();
342 }
343 if (renderer != null) {
344 renderer.dispose();
345 }
346 worker = new WorkingTask(this);
347 worker.start();
348 renderer = new RenderingTask(this);
349 renderer.init();
350 renderer.start();
351 }
352
356 public void stop() {
357 if (worker != null) {
358 worker.stop();
359 }
360 if (renderer != null) {
361 renderer.stop();
362 }
363 }
364
369 public void saveToFile(ImageFile f) {
370 suspend();
371 try {
372 img.exportToFile(f);
373 } catch (ImageLoadingException e) {
374 f.deleteFile();
375 wake();
376 return;
377 }
378 f.saveOutput(output);
379 wake();
380 }
381
382 //===== CALLBACKS ===========================================================================================================================//
383
384 //===== DEPRECATED ==========================================================================================================================//
389 public String getDump() {
390 // String retVal = dump.toString();
391 // dump.setLength(0);
392 // return retVal;
393 return "DUMP NOT SUPPORTED";
394 }
395
396 // public void addDump(String str) {
399 // }
400 }