001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.imaging.formats.icns;
018
019import static org.apache.commons.imaging.common.BinaryFunctions.read4Bytes;
020import static org.apache.commons.imaging.common.BinaryFunctions.readBytes;
021
022import java.awt.Dimension;
023import java.awt.image.BufferedImage;
024import java.io.IOException;
025import java.io.InputStream;
026import java.io.OutputStream;
027import java.io.PrintWriter;
028import java.util.ArrayList;
029import java.util.List;
030
031import org.apache.commons.imaging.AbstractImageParser;
032import org.apache.commons.imaging.ImageFormat;
033import org.apache.commons.imaging.ImageFormats;
034import org.apache.commons.imaging.ImageInfo;
035import org.apache.commons.imaging.ImagingException;
036import org.apache.commons.imaging.bytesource.ByteSource;
037import org.apache.commons.imaging.common.BinaryOutputStream;
038import org.apache.commons.imaging.common.ImageMetadata;
039
040public class IcnsImageParser extends AbstractImageParser<IcnsImagingParameters> {
041    private static final class IcnsContents {
042        public final IcnsHeader icnsHeader;
043        public final IcnsElement[] icnsElements;
044
045        IcnsContents(final IcnsHeader icnsHeader, final IcnsElement[] icnsElements) {
046            this.icnsHeader = icnsHeader;
047            this.icnsElements = icnsElements;
048        }
049    }
050
051    static class IcnsElement {
052        static final IcnsElement[] EMPTY_ARRAY = {};
053        public final int type;
054        public final int elementSize;
055        public final byte[] data;
056
057        IcnsElement(final int type, final int elementSize, final byte[] data) {
058            this.type = type;
059            this.elementSize = elementSize;
060            this.data = data;
061        }
062
063        public void dump(final PrintWriter pw) {
064            pw.println("IcnsElement");
065            final IcnsType icnsType = IcnsType.findAnyType(type);
066            String typeDescription;
067            if (icnsType == null) {
068                typeDescription = "";
069            } else {
070                typeDescription = " " + icnsType.toString();
071            }
072            pw.println("Type: 0x" + Integer.toHexString(type) + " (" + IcnsType.describeType(type) + ")" + typeDescription);
073            pw.println("ElementSize: " + elementSize);
074            pw.println("");
075        }
076    }
077
078    private static final class IcnsHeader {
079        public final int magic; // Magic literal (4 bytes), always "icns"
080        public final int fileSize; // Length of file (4 bytes), in bytes.
081
082        IcnsHeader(final int magic, final int fileSize) {
083            this.magic = magic;
084            this.fileSize = fileSize;
085        }
086
087        public void dump(final PrintWriter pw) {
088            pw.println("IcnsHeader");
089            pw.println("Magic: 0x" + Integer.toHexString(magic) + " (" + IcnsType.describeType(magic) + ")");
090            pw.println("FileSize: " + fileSize);
091            pw.println("");
092        }
093    }
094
095    static final int ICNS_MAGIC = IcnsType.typeAsInt("icns");
096
097    private static final String DEFAULT_EXTENSION = ImageFormats.ICNS.getDefaultExtension();
098
099    private static final String[] ACCEPTED_EXTENSIONS = ImageFormats.ICNS.getExtensions();
100
101    @Override
102    public boolean dumpImageFile(final PrintWriter pw, final ByteSource byteSource) throws ImagingException, IOException {
103        final IcnsContents icnsContents = readImage(byteSource);
104        icnsContents.icnsHeader.dump(pw);
105        for (final IcnsElement icnsElement : icnsContents.icnsElements) {
106            icnsElement.dump(pw);
107        }
108        return true;
109    }
110
111    @Override
112    protected String[] getAcceptedExtensions() {
113        return ACCEPTED_EXTENSIONS;
114    }
115
116    @Override
117    protected ImageFormat[] getAcceptedTypes() {
118        return new ImageFormat[] { ImageFormats.ICNS };
119    }
120
121    @Override
122    public List<BufferedImage> getAllBufferedImages(final ByteSource byteSource) throws ImagingException, IOException {
123        final IcnsContents icnsContents = readImage(byteSource);
124        return IcnsDecoder.decodeAllImages(icnsContents.icnsElements);
125    }
126
127    @Override
128    public final BufferedImage getBufferedImage(final ByteSource byteSource, final IcnsImagingParameters params) throws ImagingException, IOException {
129        final IcnsContents icnsContents = readImage(byteSource);
130        final List<BufferedImage> result = IcnsDecoder.decodeAllImages(icnsContents.icnsElements);
131        if (!result.isEmpty()) {
132            return result.get(0);
133        }
134        throw new ImagingException("No icons in ICNS file");
135    }
136
137    @Override
138    public String getDefaultExtension() {
139        return DEFAULT_EXTENSION;
140    }
141
142    @Override
143    public IcnsImagingParameters getDefaultParameters() {
144        return new IcnsImagingParameters();
145    }
146
147    @Override
148    public byte[] getIccProfileBytes(final ByteSource byteSource, final IcnsImagingParameters params) throws ImagingException, IOException {
149        return null;
150    }
151
152    @Override
153    public ImageInfo getImageInfo(final ByteSource byteSource, final IcnsImagingParameters params) throws ImagingException, IOException {
154        final IcnsContents contents = readImage(byteSource);
155        final List<BufferedImage> images = IcnsDecoder.decodeAllImages(contents.icnsElements);
156        if (images.isEmpty()) {
157            throw new ImagingException("No icons in ICNS file");
158        }
159        final BufferedImage image0 = images.get(0);
160        return new ImageInfo("Icns", 32, new ArrayList<>(), ImageFormats.ICNS, "ICNS Apple Icon Image", image0.getHeight(), "image/x-icns", images.size(), 0, 0,
161                0, 0, image0.getWidth(), false, true, false, ImageInfo.ColorType.RGB, ImageInfo.CompressionAlgorithm.UNKNOWN);
162    }
163
164    @Override
165    public Dimension getImageSize(final ByteSource byteSource, final IcnsImagingParameters params) throws ImagingException, IOException {
166        final IcnsContents contents = readImage(byteSource);
167        final List<BufferedImage> images = IcnsDecoder.decodeAllImages(contents.icnsElements);
168        if (images.isEmpty()) {
169            throw new ImagingException("No icons in ICNS file");
170        }
171        final BufferedImage image0 = images.get(0);
172        return new Dimension(image0.getWidth(), image0.getHeight());
173    }
174
175    // FIXME should throw UOE
176    @Override
177    public ImageMetadata getMetadata(final ByteSource byteSource, final IcnsImagingParameters params) throws ImagingException, IOException {
178        return null;
179    }
180
181    @Override
182    public String getName() {
183        return "Apple Icon Image";
184    }
185
186    private IcnsElement readIcnsElement(final InputStream is, final int remainingSize) throws IOException {
187        // Icon type (4 bytes)
188        final int type = read4Bytes("Type", is, "Not a valid ICNS file", getByteOrder());
189        // Length of data (4 bytes), in bytes, including this header
190        final int elementSize = read4Bytes("ElementSize", is, "Not a valid ICNS file", getByteOrder());
191        if (elementSize > remainingSize) {
192            throw new IOException(String.format("Corrupted ICNS file: element size %d is greater than " + "remaining size %d", elementSize, remainingSize));
193        }
194        final byte[] data = readBytes("Data", is, elementSize - 8, "Not a valid ICNS file");
195
196        return new IcnsElement(type, elementSize, data);
197    }
198
199    private IcnsHeader readIcnsHeader(final InputStream is) throws ImagingException, IOException {
200        final int magic = read4Bytes("Magic", is, "Not a Valid ICNS File", getByteOrder());
201        final int fileSize = read4Bytes("FileSize", is, "Not a Valid ICNS File", getByteOrder());
202
203        if (magic != ICNS_MAGIC) {
204            throw new ImagingException("Not a Valid ICNS File: " + "magic is 0x" + Integer.toHexString(magic));
205        }
206
207        return new IcnsHeader(magic, fileSize);
208    }
209
210    private IcnsContents readImage(final ByteSource byteSource) throws ImagingException, IOException {
211        try (InputStream is = byteSource.getInputStream()) {
212            final IcnsHeader icnsHeader = readIcnsHeader(is);
213
214            final List<IcnsElement> icnsElementList = new ArrayList<>();
215            for (int remainingSize = icnsHeader.fileSize - 8; remainingSize > 0;) {
216                final IcnsElement icnsElement = readIcnsElement(is, remainingSize);
217                icnsElementList.add(icnsElement);
218                remainingSize -= icnsElement.elementSize;
219            }
220
221            return new IcnsContents(icnsHeader, icnsElementList.toArray(IcnsElement.EMPTY_ARRAY));
222        }
223    }
224
225    @Override
226    public void writeImage(final BufferedImage src, final OutputStream os, final IcnsImagingParameters params) throws ImagingException, IOException {
227        IcnsType imageType;
228        if (src.getWidth() == 16 && src.getHeight() == 16) {
229            imageType = IcnsType.ICNS_16x16_32BIT_IMAGE;
230        } else if (src.getWidth() == 32 && src.getHeight() == 32) {
231            imageType = IcnsType.ICNS_32x32_32BIT_IMAGE;
232        } else if (src.getWidth() == 48 && src.getHeight() == 48) {
233            imageType = IcnsType.ICNS_48x48_32BIT_IMAGE;
234        } else if (src.getWidth() == 128 && src.getHeight() == 128) {
235            imageType = IcnsType.ICNS_128x128_32BIT_IMAGE;
236        } else {
237            throw new ImagingException("Invalid/unsupported source width " + src.getWidth() + " and height " + src.getHeight());
238        }
239
240        try (BinaryOutputStream bos = BinaryOutputStream.bigEndian(os)) {
241            bos.write4Bytes(ICNS_MAGIC);
242            bos.write4Bytes(4 + 4 + 4 + 4 + 4 * imageType.getWidth() * imageType.getHeight() + 4 + 4 + imageType.getWidth() * imageType.getHeight());
243
244            bos.write4Bytes(imageType.getType());
245            bos.write4Bytes(4 + 4 + 4 * imageType.getWidth() * imageType.getHeight());
246            for (int y = 0; y < src.getHeight(); y++) {
247                for (int x = 0; x < src.getWidth(); x++) {
248                    final int argb = src.getRGB(x, y);
249                    bos.write(0);
250                    bos.write(argb >> 16);
251                    bos.write(argb >> 8);
252                    bos.write(argb);
253                }
254            }
255
256            final IcnsType maskType = IcnsType.find8BPPMaskType(imageType);
257            bos.write4Bytes(maskType.getType());
258            bos.write4Bytes(4 + 4 + imageType.getWidth() * imageType.getWidth());
259            for (int y = 0; y < src.getHeight(); y++) {
260                for (int x = 0; x < src.getWidth(); x++) {
261                    final int argb = src.getRGB(x, y);
262                    bos.write(argb >> 24);
263                }
264            }
265        }
266    }
267}