Moved bit input and output streams into Svjatoslav Commons library.
[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 java.awt.image.BufferedImage;
13 import java.io.ByteArrayInputStream;
14 import java.io.ByteArrayOutputStream;
15 import java.io.File;
16 import java.io.FileInputStream;
17 import java.io.FileOutputStream;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.io.OutputStream;
21
22 import eu.svjatoslav.commons.data.BitInputStream;
23 import eu.svjatoslav.commons.data.BitOutputStream;
24
25 /**
26  * Main class representing compressed image.
27  */
28
29 public class Image {
30
31         public ImageMetaData metaData;
32
33         public BufferedImage bufferedImage;
34
35         ImageEncoder encoder;
36
37         public Image() {
38         };
39
40         /**
41          * Initialize imagesqueeze image based on {@link BufferedImage}.
42          * {@link BufferedImage} must be of type BufferedImage.TYPE_3BYTE_BGR .
43          */
44         public Image(final BufferedImage image) {
45
46                 bufferedImage = image;
47                 metaData = new ImageMetaData();
48
49                 metaData.version = 1;
50                 metaData.width = image.getWidth();
51                 metaData.height = image.getHeight();
52         }
53
54         /**
55          * Initialize empty imagesqueeze image.
56          */
57         public Image(final int width, final int height) {
58
59                 bufferedImage = new BufferedImage(width, height,
60                                 BufferedImage.TYPE_3BYTE_BGR);
61                 metaData = new ImageMetaData();
62
63                 metaData.version = 1;
64                 metaData.width = width;
65                 metaData.height = height;
66         }
67
68         /**
69          * Load ImgSqz image from {@link File}.
70          */
71         public void loadImage(final File source) throws IOException {
72
73                 final byte[] fileContent = new byte[(int) source.length()];
74
75                 final FileInputStream fileInputStream = new FileInputStream(source);
76
77                 fileInputStream.read(fileContent);
78
79                 fileInputStream.close();
80
81                 final ByteArrayInputStream inputStream = new ByteArrayInputStream(
82                                 fileContent);
83
84                 loadImage(inputStream);
85         }
86
87         /**
88          * Load ImgSqz image from {@link InputStream}.
89          */
90         public void loadImage(final InputStream source) throws IOException {
91                 final BitInputStream bitInputStream = new BitInputStream(source);
92
93                 metaData = new ImageMetaData();
94                 metaData.load(bitInputStream);
95
96                 bufferedImage = new BufferedImage(metaData.width, metaData.height,
97                                 BufferedImage.TYPE_3BYTE_BGR);
98
99                 final ImageDecoder imageDecoder = new ImageDecoder(this, bitInputStream);
100
101                 imageDecoder.decode();
102         }
103
104         /**
105          * Save image into ImgSqz file format.
106          */
107         public void saveImage(final File file) throws IOException {
108                 final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
109                 saveImage(outputStream);
110
111                 final byte[] buffer = outputStream.toByteArray();
112                 final FileOutputStream fileOutputStream = new FileOutputStream(file);
113                 fileOutputStream.write(buffer);
114                 fileOutputStream.close();
115         }
116
117         /**
118          * Save image into ImgSqz file format.
119          */
120         public void saveImage(final OutputStream outputStream) throws IOException {
121
122                 final BitOutputStream bitOutputStream = new BitOutputStream(
123                                 outputStream);
124
125                 metaData.save(bitOutputStream);
126
127                 if (encoder == null) {
128                         encoder = new ImageEncoder(this);
129                 }
130
131                 encoder.encode(bitOutputStream);
132
133                 bitOutputStream.finishByte();
134         }
135
136 }