Changed license to CC0
[imagesqueeze.git] / src / main / java / eu / svjatoslav / imagesqueeze / codec / Image.java
1 /*
2  * Image codec. Author: Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
3  * This project is released under Creative Commons Zero (CC0) license.
4  */
5 package eu.svjatoslav.imagesqueeze.codec;
6
7 import eu.svjatoslav.commons.data.BitInputStream;
8 import eu.svjatoslav.commons.data.BitOutputStream;
9
10 import java.awt.image.BufferedImage;
11 import java.io.*;
12
13 /**
14  * Main class representing compressed image.
15  */
16
17 public class Image {
18
19     public ImageMetaData metaData;
20
21     public BufferedImage bufferedImage;
22
23     private ImageEncoder encoder;
24
25     public Image() {
26     }
27
28     /**
29      * Initialize imagesqueeze image based on {@link BufferedImage}.
30      * {@link BufferedImage} must be of type BufferedImage.TYPE_3BYTE_BGR .
31      */
32     public Image(final BufferedImage image) {
33
34         bufferedImage = image;
35         metaData = new ImageMetaData();
36
37         metaData.version = 1;
38         metaData.width = image.getWidth();
39         metaData.height = image.getHeight();
40     }
41
42     /**
43      * Initialize empty imagesqueeze image.
44      */
45     public Image(final int width, final int height) {
46
47         bufferedImage = new BufferedImage(width, height,
48                 BufferedImage.TYPE_3BYTE_BGR);
49         metaData = new ImageMetaData();
50
51         metaData.version = 1;
52         metaData.width = width;
53         metaData.height = height;
54     }
55
56     /**
57      * Load ImgSqz image from {@link File}.
58      */
59     public void loadImage(final File source) throws IOException {
60
61         final byte[] fileContent = new byte[(int) source.length()];
62
63         try (FileInputStream fileInputStream = new FileInputStream(source)) {
64             if (fileInputStream.read(fileContent) != fileContent.length)
65                 throw new RuntimeException("Failed to read file content");
66         }
67
68         final ByteArrayInputStream inputStream = new ByteArrayInputStream(
69                 fileContent);
70
71         loadImage(inputStream);
72     }
73
74     /**
75      * Load ImgSqz image from {@link InputStream}.
76      */
77     public void loadImage(final InputStream source) throws IOException {
78         final BitInputStream bitInputStream = new BitInputStream(source);
79
80         metaData = new ImageMetaData();
81         metaData.load(bitInputStream);
82
83         bufferedImage = new BufferedImage(metaData.width, metaData.height,
84                 BufferedImage.TYPE_3BYTE_BGR);
85
86         final ImageDecoder imageDecoder = new ImageDecoder(this, bitInputStream);
87
88         imageDecoder.decode();
89     }
90
91     /**
92      * Save image into ImgSqz file format.
93      */
94     public void saveImage(final File file) throws IOException {
95         final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
96         saveImage(outputStream);
97
98         final byte[] buffer = outputStream.toByteArray();
99         try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
100             fileOutputStream.write(buffer);
101         }
102     }
103
104     /**
105      * Save image into ImgSqz file format.
106      */
107     private void saveImage(final OutputStream outputStream) throws IOException {
108
109         final BitOutputStream bitOutputStream = new BitOutputStream(
110                 outputStream);
111
112         metaData.save(bitOutputStream);
113
114         if (encoder == null) {
115             encoder = new ImageEncoder(this);
116         }
117
118         encoder.encode(bitOutputStream);
119
120         bitOutputStream.finishByte();
121     }
122
123 }