001/* 002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nuxeo - initial API and implementation 016 * 017 * $Id$ 018 * 019 */ 020package org.nuxeo.ecm.platform.picture.api; 021 022import java.io.File; 023import java.io.Serializable; 024import java.util.HashMap; 025import java.util.Map; 026 027/** 028 * Wrapper class for the information returned by the Identify ImageMagick command. 029 * 030 * @author tiry 031 */ 032public class ImageInfo implements Serializable { 033 034 private static final long serialVersionUID = 1L; 035 036 public static final String WIDTH = "width"; 037 038 public static final String HEIGHT = "height"; 039 040 public static final String DEPTH = "depth"; 041 042 public static final String FORMAT = "format"; 043 044 public static final String COLOR_SPACE = "colorSpace"; 045 046 protected int width; 047 048 protected int height; 049 050 protected int depth; 051 052 protected String format; 053 054 /** @since 5.9.5 */ 055 protected String colorSpace; 056 057 protected String filePath; 058 059 public static ImageInfo fromMap(Map<String, Serializable> map) { 060 if (map == null) { 061 return null; 062 } 063 064 ImageInfo info = new ImageInfo(); 065 Long width = (Long) map.get(WIDTH); 066 if (width != null) { 067 info.width = width.intValue(); 068 } 069 Long height = (Long) map.get(HEIGHT); 070 if (height != null) { 071 info.height = height.intValue(); 072 } 073 Long depth = (Long) map.get(DEPTH); 074 if (depth != null) { 075 info.depth = depth.intValue(); 076 } 077 info.format = (String) map.get(FORMAT); 078 info.colorSpace = (String) map.get(COLOR_SPACE); 079 return info; 080 } 081 082 public ImageInfo() { 083 } 084 085 public ImageInfo(String width, String height, String format, String filePath) { 086 this.width = Integer.parseInt(width); 087 this.height = Integer.parseInt(height); 088 this.format = format; 089 this.filePath = filePath; 090 } 091 092 public ImageInfo(String width, String height, String format, String depth, String filePath) { 093 this(width, height, format, filePath); 094 this.depth = Integer.parseInt(depth); 095 } 096 097 /** @since 5.9.5 */ 098 public ImageInfo(String width, String height, String format, String depth, String colorSpace, String filePath) { 099 this(width, height, format, filePath); 100 this.depth = Integer.parseInt(depth); 101 this.colorSpace = colorSpace; 102 } 103 104 public int getWidth() { 105 return width; 106 } 107 108 public void setWidth(int width) { 109 this.width = width; 110 } 111 112 public int getHeight() { 113 return height; 114 } 115 116 public void setHeight(int height) { 117 this.height = height; 118 } 119 120 public String getFormat() { 121 return format; 122 } 123 124 public void setFormat(String format) { 125 this.format = format; 126 } 127 128 @Override 129 public String toString() { 130 return width + "x" + height + "-" + format; 131 } 132 133 public String getFilePath() { 134 return filePath; 135 } 136 137 public File getFile() { 138 return new File(filePath); 139 } 140 141 public int getDepth() { 142 return depth; 143 } 144 145 public void setDepth(int depth) { 146 this.depth = depth; 147 } 148 149 /** @since 5.9.5 */ 150 public String getColorSpace() { 151 return colorSpace; 152 } 153 154 /** @since 5.9.5 */ 155 public void setColorSpace(String colorSpace) { 156 this.colorSpace = colorSpace; 157 } 158 159 /** 160 * Returns a {@code Map} of attributes for this {@code ImageInfo}. 161 * <p> 162 * Used when saving this {@code ImageInfo} to a {@code DocumentModel} property. 163 * 164 * @since 7.1 165 */ 166 public Map<String, Serializable> toMap() { 167 Map<String, Serializable> map = new HashMap<>(); 168 map.put(WIDTH, width); 169 map.put(HEIGHT, height); 170 map.put(DEPTH, depth); 171 map.put(FORMAT, format); 172 map.put(COLOR_SPACE, colorSpace); 173 return map; 174 } 175}