| 1 |
/* |
| 2 |
Copyright (C) 2015 Red Hat, Inc. |
| 3 |
|
| 4 |
This file is part of IcedTea. |
| 5 |
|
| 6 |
IcedTea is free software; you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU General Public License as published by |
| 8 |
the Free Software Foundation; either version 2, or (at your option) |
| 9 |
any later version. |
| 10 |
|
| 11 |
IcedTea is distributed in the hope that it will be useful, but |
| 12 |
WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU General Public License |
| 17 |
along with IcedTea; see the file COPYING. If not, write to the |
| 18 |
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 19 |
02110-1301 USA. |
| 20 |
|
| 21 |
Linking this library statically or dynamically with other modules is |
| 22 |
making a combined work based on this library. Thus, the terms and |
| 23 |
conditions of the GNU General Public License cover the whole |
| 24 |
combination. |
| 25 |
|
| 26 |
As a special exception, the copyright holders of this library give you |
| 27 |
permission to link this library with independent modules to produce an |
| 28 |
executable, regardless of the license terms of these independent |
| 29 |
modules, and to copy and distribute the resulting executable under |
| 30 |
terms of your choice, provided that you also meet, for each linked |
| 31 |
independent module, the terms and conditions of the license of that |
| 32 |
module. An independent module is a module which is not derived from |
| 33 |
or based on this library. If you modify this library, you may extend |
| 34 |
this exception to your version of the library, but you are not |
| 35 |
obligated to do so. If you do not wish to do so, delete this |
| 36 |
exception statement from your version. */ |
| 37 |
package net.sourceforge.jnlp.tools.ico.impl; |
| 38 |
|
| 39 |
import java.awt.image.BufferedImage; |
| 40 |
import java.io.ByteArrayInputStream; |
| 41 |
import java.io.EOFException; |
| 42 |
import java.io.IOException; |
| 43 |
import java.util.ArrayList; |
| 44 |
import java.util.Collections; |
| 45 |
import java.util.List; |
| 46 |
import javax.imageio.ImageIO; |
| 47 |
import javax.imageio.stream.ImageInputStream; |
| 48 |
|
| 49 |
public class ImageInputStreamIco { |
| 50 |
|
| 51 |
private final IcoHeader header; |
| 52 |
private final List<BufferedImage> images; //size 16*countOfIcons bytes |
| 53 |
|
| 54 |
public IcoHeader getHeader() { |
| 55 |
return header; |
| 56 |
} |
| 57 |
|
| 58 |
public BufferedImage getImage(int i) { |
| 59 |
return images.get(i); |
| 60 |
} |
| 61 |
|
| 62 |
public ImageInputStreamIco(ImageInputStream src) throws IOException, IcoException { |
| 63 |
this.header = new IcoHeader(src); |
| 64 |
images = new ArrayList<>(header.countOfIcons); |
| 65 |
for (IcoHeaderEntry e : header.entries) { |
| 66 |
BufferedImage image = readImage(e, src); |
| 67 |
images.add(image); |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
public List<BufferedImage> getImages() { |
| 72 |
return Collections.unmodifiableList(images); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
private static void readMask(IcoHeaderEntry e, ImageInputStream src1) throws IOException { |
| 78 |
//acording to spec, behind img bytes, should be another bytes, with AND bitmap.Hoewer, I had not found them. |
| 79 |
//however, that means, that transaprency is lost... But bit offsets mathces.. so... |
| 80 |
//IcoHeader.IcoHeaderEntry q = e.provideMonochromeHeader(); |
| 81 |
//src1.getStreamPosition(); |
| 82 |
//byte[] mask = new byte[q.sizeInBytes]; |
| 83 |
//src1.readFully(mask); |
| 84 |
} |
| 85 |
|
| 86 |
private static BufferedImage readImage(IcoHeaderEntry e, ImageInputStream src1) throws IOException { |
| 87 |
BufferedImage image; |
| 88 |
byte[] img = new byte[e.getSizeInBytes()]; |
| 89 |
if (src1.getStreamPosition() != e.getFileOffset()) { |
| 90 |
//I had never seen this thrown, Still, is it worthy to tempt it, or rather read and die later? |
| 91 |
//throw new IOException("Stream position do nto match expected position. Bmp(or png) will read wrongly"); |
| 92 |
} |
| 93 |
src1.readFully(img); |
| 94 |
try { |
| 95 |
image = parse(img, e); |
| 96 |
//readMask(e, src1); |
| 97 |
} catch (EOFException ex) { |
| 98 |
//some icons do not honour that 0 is 256. Retrying |
| 99 |
if (e.getColorCount() != 0) { |
| 100 |
e.setColorCount(0); |
| 101 |
image = parse(img, e); |
| 102 |
//readMask(e, src1); |
| 103 |
} else { |
| 104 |
throw ex; |
| 105 |
} |
| 106 |
} |
| 107 |
return image; |
| 108 |
} |
| 109 |
|
| 110 |
private static BufferedImage parse(byte[] img, IcoHeaderEntry e) throws IOException { |
| 111 |
ByteArrayInputStream bis = new ByteArrayInputStream(img); |
| 112 |
BufferedImage image = null; |
| 113 |
try { |
| 114 |
image = ImageIO.read(bis); |
| 115 |
} catch (Exception ex) { |
| 116 |
//not png |
| 117 |
} |
| 118 |
if (image != null) { |
| 119 |
fixSizesInHeader(e, image); |
| 120 |
return image; |
| 121 |
} |
| 122 |
//bmp |
| 123 |
img = prefixByFakeHeader(img, e); |
| 124 |
bis = new ByteArrayInputStream(img); |
| 125 |
//dont try catch this one. you will break it |
| 126 |
image = ImageIO.read(bis); |
| 127 |
|
| 128 |
return image; |
| 129 |
} |
| 130 |
|
| 131 |
private static void fixSizesInHeader(IcoHeaderEntry e, BufferedImage image) { |
| 132 |
//may happen for png |
| 133 |
if (e.getWidth() == 0) { |
| 134 |
e.setWidth(image.getWidth()); |
| 135 |
} |
| 136 |
if (e.getHeight() == 0) { |
| 137 |
e.setHeight(image.getHeight()); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
private static byte[] prefixByFakeHeader(final byte[] origArray, IcoHeaderEntry e) { |
| 142 |
int fakingArray = 14; |
| 143 |
byte[] img = new byte[fakingArray + e.getSizeInBytes()]; |
| 144 |
for (int i = 0; i < origArray.length; i++) { |
| 145 |
byte p = origArray[i]; |
| 146 |
img[i + 14] = p; |
| 147 |
|
| 148 |
} |
| 149 |
//fake header |
| 150 |
//http://www.daubnet.com/en/file-format-bmp |
| 151 |
int size = e.getSizeInBytes() + fakingArray; |
| 152 |
img[0] = 'B'; |
| 153 |
img[1] = 'M'; |
| 154 |
img[2] = (byte) (size & 0xFF); |
| 155 |
img[3] = (byte) ((size >> 8) & 0xFF); |
| 156 |
img[4] = (byte) ((size >> 16) & 0xFF); |
| 157 |
img[5] = (byte) ((size >> 24) & 0xFF); |
| 158 |
img[6] = 0; |
| 159 |
img[7] = 0; |
| 160 |
img[8] = 0; |
| 161 |
img[9] = 0; |
| 162 |
int ofset = fakingArray + 40 + 4 * e.getColorCount(); |
| 163 |
img[10] = (byte) (ofset & 0xFF); |
| 164 |
img[11] = (byte) ((ofset >> 8) & 0xFF); |
| 165 |
img[12] = (byte) ((ofset >> 16) & 0xFF); |
| 166 |
img[13] = (byte) ((ofset >> 24) & 0xFF); |
| 167 |
//ico is storing height as height of XOR + height of AND bitmaps |
| 168 |
//that is 2 x hight. Bitmap expects only height of single image |
| 169 |
int tmpHeight = e.getHeight(); |
| 170 |
img[fakingArray + 4/*size*/ + 4/*width*/] = (byte) (tmpHeight & 0xFF); |
| 171 |
img[fakingArray + 4/*size*/ + 4/*width*/ + 1] = (byte) ((tmpHeight >> 8) & 0xFF); |
| 172 |
img[fakingArray + 4/*size*/ + 4/*width*/ + 2] = (byte) ((tmpHeight >> 16) & 0xFF); |
| 173 |
img[fakingArray + 4/*size*/ + 4/*width*/ + 3] = (byte) ((tmpHeight >> 24) & 0xFF); |
| 174 |
return img; |
| 175 |
} |
| 176 |
|
| 177 |
} |