001    /*
002     * ImageDescriptionCreator
003     *
004     * Copyright (c) 2002, 2003 Marco Schmidt.
005     * All rights reserved.
006     */
007    
008    package net.sourceforge.jiu.apps;
009    
010    import java.text.NumberFormat;
011    import java.util.Locale;
012    import net.sourceforge.jiu.data.PixelImage;
013    import net.sourceforge.jiu.data.BilevelImage;
014    import net.sourceforge.jiu.data.GrayImage;
015    import net.sourceforge.jiu.data.PalettedImage;
016    import net.sourceforge.jiu.data.RGBImage;
017    
018    /**
019     * Returns textual descriptions of the properties of JIU image objects.
020     * @author Marco Schmidt
021     * @since 0.9.0
022     */
023    public class ImageDescriptionCreator
024    {
025            private static final int TYPE = 0;
026            private static final int PIXELS = 1;
027            private static final int IMAGE_TYPE_BILEVEL = 2;
028            private static final int IMAGE_TYPE_GRAYSCALE = 3;
029            private static final int IMAGE_TYPE_PALETTED = 4;
030            private static final int IMAGE_TYPE_RGB_TRUECOLOR = 5;
031            private static final int IMAGE_TYPE_UNKNOWN = 6;
032            private static final int BITS_PER_PIXEL = 7;
033            private static final int MEMORY = 8;
034            private static final int DISK_SPACE = 9;
035    
036            private ImageDescriptionCreator()
037            {
038            }
039    
040            private static String formatNumber(long value, Locale locale)
041            {
042                    if (locale == null)
043                    {
044                            return Long.toString(value);
045                    }
046                    else
047                    {
048                            return NumberFormat.getInstance(locale).format(value);
049                    }
050            }
051    
052            /**
053             * Returns a description of the argument image using the default locale.
054             */
055            /*public static String getDescription(PixelImage image)
056            {
057                    return getDescription(image, Locale.getDefault());
058            }*/
059    
060            /**
061             * Returns a description of the argument image using the language
062             * as specified by the argument locale's two-letter language code.
063             * @param image the image for which a textual description is to be returned
064             * @param locale the Locale storing the natural language to be used for formatting
065             * @return a textual description of the image
066             */
067            public static String getDescription(PixelImage image, Locale locale, Strings strings)
068            {
069                    String languageCode = locale.getLanguage();
070                    StringBuffer result = new StringBuffer();
071                    result.append(strings.get(StringIndexConstants.IMAGE_TYPE));
072                    result.append(": ");
073                    result.append(strings.get(getImageType(image)));
074                    result.append(", ");
075                    result.append(strings.get(StringIndexConstants.PIXELS));
076                    result.append(": ");
077                    int width = image.getWidth();
078                    int height = image.getHeight();
079                    result.append(formatNumber(width, locale));
080                    result.append(" x ");
081                    result.append(formatNumber(height, locale));
082                    result.append(" (");
083                    result.append(formatNumber(width * height, locale));
084                    result.append("), ");
085                    result.append(strings.get(StringIndexConstants.BITS_PER_PIXEL));
086                    result.append(": ");
087                    result.append(formatNumber(image.getBitsPerPixel(), locale));
088                    return result.toString();
089            }
090    
091            private static int getImageType(PixelImage image)
092            {
093                    int stringIndex = StringIndexConstants.IMAGE_TYPE_UNKNOWN;
094                    if (image instanceof BilevelImage)
095                    {
096                            return stringIndex = StringIndexConstants.BILEVEL;
097                    }
098                    else
099                    if (image instanceof GrayImage)
100                    {
101                            return stringIndex = StringIndexConstants.GRAYSCALE;
102                    }
103                    else
104                    if (image instanceof PalettedImage)
105                    {
106                            return stringIndex = StringIndexConstants.PALETTED;
107                    }
108                    else
109                    if (image instanceof RGBImage)
110                    {
111                            return stringIndex = StringIndexConstants.RGB_TRUECOLOR;
112                    }
113                    return stringIndex;
114            }
115    }