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