<commit_before>/**
 * Copyright (c) 2009 - 2010 AppWork UG(haftungsbeschränkt) <<EMAIL>>
 * 
 * This file is part of org.appwork.utils.ImageProvider
 * 
 * This software is licensed under the Artistic License 2.0,
 * see the LICENSE file or http://www.opensource.org/licenses/artistic-license-2.0.php
 * for details
 */
package org.appwork.utils.ImageProvider;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Locale;
import java.util.logging.Level;

import javax.imageio.IIOException;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.UIManager;

import org.appwork.storage.config.MinTimeWeakReference;
import org.appwork.utils.Application;
import org.appwork.utils.logging.Log;

import sun.awt.image.ToolkitImage;

/**
 * This class grants easy access to images stored in APPROOT/images/**.png
 * 
 * @author $Author: unknown$
 * 
 */
public class ImageProvider {
    private static final long                                           MIN_LIFETIME        = 20000l;
    /**
     * Hashcashmap to cache images.
     */
    private static HashMap<String, MinTimeWeakReference<BufferedImage>> IMAGE_CACHE         = new HashMap<String, MinTimeWeakReference<BufferedImage>>();
    private static HashMap<String, MinTimeWeakReference<ImageIcon>>     IMAGEICON_CACHE     = new HashMap<String, MinTimeWeakReference<ImageIcon>>();
    private static HashMap<Icon, MinTimeWeakReference<Icon>>            DISABLED_ICON_CACHE = new HashMap<Icon, MinTimeWeakReference<Icon>>();

    private static Object                                               LOCK                = new Object();
    // stringbuilder die concat strings fast

    static {
        /* we dont want images to get cached on disk */
        ImageIO.setUseCache(false);
    }

    /**
     * @param bufferedImage
     * @return
     */
    public static BufferedImage convertToGrayScale(final BufferedImage bufferedImage) {
        final BufferedImage dest = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
        final Graphics2D g2 = dest.createGraphics();
        g2.drawImage(bufferedImage, 0, 0, null);
        g2.dispose();
        return dest;

    }

    /**
     * Creates a dummy Icon
     * 
     * @param string
     * @param i
     * @param j<commit_msg>ImageProvider: convertToGrayScale fixed by flubshi to keep alpha channel<commit_after>/**
 * Copyright (c) 2009 - 2010 AppWork UG(haftungsbeschränkt) <<EMAIL>>
 * 
 * This file is part of org.appwork.utils.ImageProvider
 * 
 * This software is licensed under the Artistic License 2.0,
 * see the LICENSE file or http://www.opensource.org/licenses/artistic-license-2.0.php
 * for details
 */
package org.appwork.utils.ImageProvider;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Locale;
import java.util.logging.Level;

import javax.imageio.IIOException;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.UIManager;

import org.appwork.storage.config.MinTimeWeakReference;
import org.appwork.utils.Application;
import org.appwork.utils.logging.Log;

import sun.awt.image.ToolkitImage;

/**
 * This class grants easy access to images stored in APPROOT/images/**.png
 * 
 * @author $Author: unknown$
 * 
 */
public class ImageProvider {
    private static final long                                           MIN_LIFETIME        = 20000l;
    /**
     * Hashcashmap to cache images.
     */
    private static HashMap<String, MinTimeWeakReference<BufferedImage>> IMAGE_CACHE         = new HashMap<String, MinTimeWeakReference<BufferedImage>>();
    private static HashMap<String, MinTimeWeakReference<ImageIcon>>     IMAGEICON_CACHE     = new HashMap<String, MinTimeWeakReference<ImageIcon>>();
    private static HashMap<Icon, MinTimeWeakReference<Icon>>            DISABLED_ICON_CACHE = new HashMap<Icon, MinTimeWeakReference<Icon>>();

    private static Object                                               LOCK                = new Object();
    // stringbuilder die concat strings fast

    static {
        /* we dont want images to get cached on disk */
        ImageIO.setUseCache(false);
    }

    /* Thx to flubshi */
    public static BufferedImage convertToGrayScale(final BufferedImage bufferedImage) {
        final BufferedImage dest = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.TYPE_INT_ARGB);
        Color tmp;
        int val, alpha;
        for (int y = 0; y < dest.getHeight(); y++) {
            for (int x = 0; x < dest.getWidth(); x++) {
                alpha = bufferedImage.getRGB(x, y) & 0xFF000000;
                tmp = new Color(bufferedImage.getRGB(x, y));
                // val = (int) (tmp.getRed()+tmp.getGreen()+tmp.getBlue())/3;
                // val =
                // Math.max(tmp.getRed(),Math.max(tmp.getGreen(),tmp.getBlue()));
                val = (int) (tmp.getRed() * 0.3 + tmp.getGreen() * 0.59 + tmp.getBlue() * 0.11);
                dest.setRGB(x, y, alpha | val | val << 8 & 0x0000FF00 | val << 16 & 0x00FF0000);
            }
        }
        return dest;
    }

    /**
     * Creates a dummy Icon
     * 
     * @param string
     * @param i
     * @param j
{'fraction_non_alphanumeric': 0.10659484165850473, 'fraction_numerical': 0.01256937642833823, 'mean_word_length': 2.9300833867864013, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6033130', 'n_tokens_mistral': 1866, 'n_tokens_neox': 1755, 'n_words': 440}