001: // ymageICOParser.java
002: // (C) 2007 by Michael Peter Christen; mc@yacy.net, Frankfurt a. M., Germany
003: // first published 15.07.2007 on http://yacy.net
004: //
005: // $LastChangedDate: 2006-04-02 22:40:07 +0200 (So, 02 Apr 2006) $
006: // $LastChangedRevision: 1986 $
007: // $LastChangedBy: orbiter $
008: //
009: // LICENSE
010: //
011: // This program is free software; you can redistribute it and/or modify
012: // it under the terms of the GNU General Public License as published by
013: // the Free Software Foundation; either version 2 of the License, or
014: // (at your option) any later version.
015: //
016: // This program is distributed in the hope that it will be useful,
017: // but WITHOUT ANY WARRANTY; without even the implied warranty of
018: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
019: // GNU General Public License for more details.
020: //
021: // You should have received a copy of the GNU General Public License
022: // along with this program; if not, write to the Free Software
023: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: package de.anomic.ymage;
026:
027: import java.awt.image.BufferedImage;
028: import java.io.File;
029: import java.io.FileInputStream;
030: import java.io.FileNotFoundException;
031: import java.io.IOException;
032:
033: import javax.imageio.ImageIO;
034:
035: public class ymageICOParser {
036:
037: // this is a implementation of http://msdn2.microsoft.com/en-us/library/ms997538(d=printer).aspx
038:
039: public static int ICONDIRENTRY_size = 16;
040:
041: private int idCount;
042: ymageBMPParser.INFOHEADER[] infoheaders;
043: ymageBMPParser.IMAGEMAP[] imagemaps;
044:
045: public static final boolean isICO(byte[] source) {
046: // check the file magic
047: return (source != null) && (source.length >= 4)
048: && (source[0] == 0) && (source[1] == 0)
049: && (source[2] == 1) && (source[3] == 0);
050: }
051:
052: public ymageICOParser(byte[] source) {
053: // read info-header
054: idCount = ymageBMPParser.WORD(source, 4);
055:
056: // read the icon directory entry and the image entries
057: ICONDIRENTRY[] icondirentries = new ICONDIRENTRY[idCount];
058: infoheaders = new ymageBMPParser.INFOHEADER[idCount];
059: ymageBMPParser.COLORTABLE[] colortables = new ymageBMPParser.COLORTABLE[idCount];
060: imagemaps = new ymageBMPParser.IMAGEMAP[idCount];
061: for (int i = 0; i < idCount; i++) {
062: icondirentries[i] = new ICONDIRENTRY(source, 6 + i
063: * ICONDIRENTRY_size);
064: infoheaders[i] = new ymageBMPParser.INFOHEADER(source,
065: icondirentries[i].dwImageOffset);
066: colortables[i] = new ymageBMPParser.COLORTABLE(source,
067: icondirentries[i].dwImageOffset
068: + ymageBMPParser.INFOHEADER_size,
069: infoheaders[i]);
070: imagemaps[i] = new ymageBMPParser.IMAGEMAP(source,
071: icondirentries[i].dwImageOffset
072: + ymageBMPParser.INFOHEADER_size
073: + colortables[i].colorbytes,
074: icondirentries[i].bWidth,
075: icondirentries[i].bHeight,
076: infoheaders[i].biCompression,
077: infoheaders[i].biBitCount, colortables[i]);
078: }
079: }
080:
081: public class ICONDIRENTRY {
082:
083: public int bWidth, bHeight, bColorCount, bReserved, wPlanes,
084: wBitCount, dwBytesInRes, dwImageOffset;
085:
086: public ICONDIRENTRY(byte[] s, int offset) {
087: // read info-header
088: bWidth = ymageBMPParser.BYTE(s, offset + 0);
089: bHeight = ymageBMPParser.BYTE(s, offset + 1);
090: bColorCount = ymageBMPParser.BYTE(s, offset + 2);
091: bReserved = ymageBMPParser.BYTE(s, offset + 3);
092: wPlanes = ymageBMPParser.WORD(s, offset + 4);
093: wBitCount = ymageBMPParser.WORD(s, offset + 6);
094: dwBytesInRes = ymageBMPParser.DWORD(s, offset + 8);
095: dwImageOffset = ymageBMPParser.DWORD(s, offset + 12);
096: }
097: }
098:
099: public int images() {
100: // return number of images in icon
101: return idCount;
102: }
103:
104: public BufferedImage getImage(int index) {
105: return imagemaps[index].image;
106: }
107:
108: public static void main(String[] args) {
109: // read a ICO and write it as png
110: System.setProperty("java.awt.headless", "true");
111: File in = new File(args[0]);
112: File out = new File(args[1]);
113:
114: byte[] file = new byte[(int) in.length()];
115: FileInputStream fis = null;
116: try {
117: fis = new FileInputStream(in);
118: } catch (FileNotFoundException e) {
119: e.printStackTrace();
120: }
121: try {
122: fis.read(file);
123: } catch (IOException e) {
124: e.printStackTrace();
125: }
126:
127: ymageICOParser parser = new ymageICOParser(file);
128:
129: try {
130: ImageIO.write(parser.getImage(0), "PNG", out);
131: } catch (IOException e) {
132: e.printStackTrace();
133: }
134: }
135:
136: }
|