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