001/*
002 * (C) Copyright 2006-2008 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 *
021 */
022package org.nuxeo.ecm.platform.picture.api;
023
024import java.io.File;
025import java.io.Serializable;
026import java.util.HashMap;
027import java.util.Map;
028
029/**
030 * Wrapper class for the information returned by the Identify ImageMagick command.
031 *
032 * @author tiry
033 */
034public class ImageInfo implements Serializable {
035
036    private static final long serialVersionUID = 1L;
037
038    public static final String WIDTH = "width";
039
040    public static final String HEIGHT = "height";
041
042    public static final String DEPTH = "depth";
043
044    public static final String FORMAT = "format";
045
046    public static final String COLOR_SPACE = "colorSpace";
047
048    protected int width;
049
050    protected int height;
051
052    protected int depth;
053
054    protected String format;
055
056    /** @since 5.9.5 */
057    protected String colorSpace;
058
059    protected String filePath;
060
061    public static ImageInfo fromMap(Map<String, Serializable> map) {
062        if (map == null) {
063            return null;
064        }
065
066        ImageInfo info = new ImageInfo();
067        Long width = (Long) map.get(WIDTH);
068        if (width != null) {
069            info.width = width.intValue();
070        }
071        Long height = (Long) map.get(HEIGHT);
072        if (height != null) {
073            info.height = height.intValue();
074        }
075        Long depth = (Long) map.get(DEPTH);
076        if (depth != null) {
077            info.depth = depth.intValue();
078        }
079        info.format = (String) map.get(FORMAT);
080        info.colorSpace = (String) map.get(COLOR_SPACE);
081        return info;
082    }
083
084    public ImageInfo() {
085    }
086
087    public ImageInfo(String width, String height, String format, String filePath) {
088        this.width = Integer.parseInt(width);
089        this.height = Integer.parseInt(height);
090        this.format = format;
091        this.filePath = filePath;
092    }
093
094    public ImageInfo(String width, String height, String format, String depth, String filePath) {
095        this(width, height, format, filePath);
096        this.depth = Integer.parseInt(depth);
097    }
098
099    /** @since 5.9.5 */
100    public ImageInfo(String width, String height, String format, String depth, String colorSpace, String filePath) {
101        this(width, height, format, filePath);
102        this.depth = Integer.parseInt(depth);
103        this.colorSpace = colorSpace;
104    }
105
106    public int getWidth() {
107        return width;
108    }
109
110    public void setWidth(int width) {
111        this.width = width;
112    }
113
114    public int getHeight() {
115        return height;
116    }
117
118    public void setHeight(int height) {
119        this.height = height;
120    }
121
122    public String getFormat() {
123        return format;
124    }
125
126    public void setFormat(String format) {
127        this.format = format;
128    }
129
130    @Override
131    public String toString() {
132        return width + "x" + height + "-" + format;
133    }
134
135    public String getFilePath() {
136        return filePath;
137    }
138
139    public File getFile() {
140        return new File(filePath);
141    }
142
143    public int getDepth() {
144        return depth;
145    }
146
147    public void setDepth(int depth) {
148        this.depth = depth;
149    }
150
151    /** @since 5.9.5 */
152    public String getColorSpace() {
153        return colorSpace;
154    }
155
156    /** @since 5.9.5 */
157    public void setColorSpace(String colorSpace) {
158        this.colorSpace = colorSpace;
159    }
160
161    /**
162     * Returns a {@code Map} of attributes for this {@code ImageInfo}.
163     * <p>
164     * Used when saving this {@code ImageInfo} to a {@code DocumentModel} property.
165     *
166     * @since 7.1
167     */
168    public Map<String, Serializable> toMap() {
169        Map<String, Serializable> map = new HashMap<>();
170        map.put(WIDTH, width);
171        map.put(HEIGHT, height);
172        map.put(DEPTH, depth);
173        map.put(FORMAT, format);
174        map.put(COLOR_SPACE, colorSpace);
175        return map;
176    }
177}