altosuilib: Decompress map images asynchronously and in parallel
[fw/altos] / altosuilib / AltosSiteMapImage.java
1 /*
2  * Copyright © 2014 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package org.altusmetrum.altosuilib_2;
19
20 import java.awt.*;
21 import java.awt.image.*;
22 import javax.imageio.ImageIO;
23 import javax.swing.*;
24 import java.io.*;
25 import org.altusmetrum.altoslib_4.*;
26
27 public class AltosSiteMapImage {
28         AltosSiteMapTile        tile;
29         File                    file;
30         BufferedImage           image;
31         int                     width;
32         int                     height;
33         long                    used;
34
35         Thread  load_thread;
36
37         public boolean validate() {
38                 if (image != null) {
39                         AltosSiteMap.debug_component(tile, "valid");
40                         return true;
41                 } else {
42                         AltosSiteMap.debug_component(tile, "loading");
43                         load_thread = new Thread() {
44                                         public void run() {
45                                                 image = null;
46                                                 try {
47                                                         image = ImageIO.read(file);
48                                                 } catch (Exception e) {
49                                                 }
50                                                 SwingUtilities.invokeLater( new Runnable() {
51                                                                 public void run() {
52                                                                         AltosSiteMap.debug_component(tile, "later");
53                                                                         Graphics2D g2d = (Graphics2D) tile.getGraphics();
54                                                                         tile.paint_graphics(g2d, image);
55                                                                         load_thread = null;
56                                                                 }
57                                                         });
58                                         }
59                                 };
60                         load_thread.start();
61                         return false;
62                 }
63         }
64
65         public void flush() {
66                 if (load_thread == null) {
67                         AltosSiteMap.debug_component(tile, "flush");
68                         image.flush();
69                         image = null;
70                 }
71         }
72
73         public AltosSiteMapImage (AltosSiteMapTile tile, File file, int w, int h) throws IOException {
74                 this.tile = tile;
75                 this.file = file;
76                 width = w;
77                 height = h;
78                 image = null;
79                 used = 0;
80         }
81 }
82