[LIB-9] Add functional for transcoding vector images
[chesshog.git] / chesshog-graphics / src / main / java / org / hedgecode / chess / img / bitmap / BitmapImageShaper.java
1 /*
2  * Copyright (c) 2018-2019. Developed by Hedgecode.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.hedgecode.chess.img.bitmap;
18
19 import java.awt.Graphics;
20 import java.awt.image.BufferedImage;
21 import java.io.File;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.net.URL;
25
26 import javax.imageio.ImageIO;
27
28 import org.hedgecode.chess.img.ImageException;
29 import org.hedgecode.chess.img.ImageShaper;
30
31 /**
32  * Shaper for bitmap images.
33  *
34  * @author Dmitry Samoshin aka gotty
35  */
36 public class BitmapImageShaper implements ImageShaper {
37
38     private BitmapImageShaper() {
39     }
40
41     @Override
42     public BufferedImage shape(InputStream imageStream, int size) throws ImageException {
43         try {
44             return scaledImage(
45                     ImageIO.read(imageStream),
46                     size
47             );
48         } catch (IOException e) {
49             throw new ImageException("image.unable.load.bitmap", e.getMessage());
50         }
51     }
52
53     @Override
54     public BufferedImage shape(URL imageUrl, int size) throws ImageException {
55         try {
56             return scaledImage(
57                     ImageIO.read(imageUrl),
58                     size
59             );
60         } catch (IOException e) {
61             throw new ImageException("image.unable.load.bitmap", e.getMessage());
62         }
63     }
64
65     @Override
66     public BufferedImage shape(File imageFile, int size) throws ImageException {
67         try {
68             return scaledImage(
69                     ImageIO.read(imageFile),
70                     size
71             );
72         } catch (IOException e) {
73             throw new ImageException("image.unable.load.bitmap", e.getMessage());
74         }
75     }
76
77     private BufferedImage scaledImage(BufferedImage image, int size) {
78         if (image.getWidth() == size && image.getHeight() == size) {
79             return image;
80         }
81         BufferedImage scaledImage = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
82         Graphics graphics = scaledImage.createGraphics();
83         graphics.drawImage(
84                 image.getScaledInstance(size, size, BufferedImage.SCALE_SMOOTH),
85                 0, 0, null
86         );
87         graphics.dispose();
88         return scaledImage;
89     }
90
91     public static ImageShaper create() {
92         return new BitmapImageShaper();
93     }
94
95 }