2 * Copyright (c) 2018-2019. Developed by Hedgecode.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.hedgecode.chess.img.bitmap;
19 import java.awt.Graphics;
20 import java.awt.image.BufferedImage;
22 import java.io.IOException;
23 import java.io.InputStream;
26 import javax.imageio.ImageIO;
28 import org.hedgecode.chess.img.ImageException;
29 import org.hedgecode.chess.img.ImageShaper;
32 * Shaper for bitmap images.
34 * @author Dmitry Samoshin aka gotty
36 public class BitmapImageShaper implements ImageShaper {
38 private BitmapImageShaper() {
42 public BufferedImage shape(InputStream imageStream, int size) throws ImageException {
45 ImageIO.read(imageStream),
48 } catch (IOException e) {
49 throw new ImageException("image.unable.load.bitmap", e.getMessage());
54 public BufferedImage shape(URL imageUrl, int size) throws ImageException {
57 ImageIO.read(imageUrl),
60 } catch (IOException e) {
61 throw new ImageException("image.unable.load.bitmap", e.getMessage());
66 public BufferedImage shape(File imageFile, int size) throws ImageException {
69 ImageIO.read(imageFile),
72 } catch (IOException e) {
73 throw new ImageException("image.unable.load.bitmap", e.getMessage());
77 private BufferedImage scaledImage(BufferedImage image, int size) {
78 if (image.getWidth() == size && image.getHeight() == size) {
81 BufferedImage scaledImage = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
82 Graphics graphics = scaledImage.createGraphics();
84 image.getScaledInstance(size, size, BufferedImage.SCALE_SMOOTH),
91 public static ImageShaper create() {
92 return new BitmapImageShaper();