Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package bz;
- import java.awt.Graphics2D;
- import java.awt.RenderingHints;
- import java.awt.image.BufferedImage;
- import java.awt.image.DirectColorModel;
- import java.awt.image.IndexColorModel;
- import java.awt.image.WritableRaster;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import org.eclipse.swt.SWT;
- import org.eclipse.swt.SWTException;
- import org.eclipse.swt.graphics.ImageData;
- import org.eclipse.swt.graphics.ImageLoader;
- import org.eclipse.swt.graphics.PaletteData;
- import org.eclipse.swt.graphics.RGB;
- import bz.command.OpenErrorWindow;
- /**
- * Represents a Thumbnail in the workspace. Wraps an Image object.
- *
- * @author Jonah Dahlquist <jonah@nucleussystems.com>
- * @license public domain
- */
- public class Thumbnail implements Comparable<Thumbnail>
- {
- // Contains the thumbnail data if it has been generated and not cached
- protected ImageData thumbData;
- // Contains the location of the thumbnail data if it has been cached
- protected File thumbFile;
- // Indicates if the thumbnail is in memory. Read by getThumbData()
- protected boolean thumbDataInMemory = false;
- // Indicates if the thumbnail is cached
- protected boolean thumbDataCached = false;
- // Flag if file could not be loaded
- protected boolean isCorrupt = false;
- // Flag if cursor is hovering over
- private boolean over_ = false;
- // Flag if thumbnail is selected
- private boolean selected_ = false;
- // Wrapped image
- private Image image_;
- // Thumbnail area
- private Rectangle eventBox_ = new Rectangle(0, 0, 0, 0);
- /**
- * Create new thumbnail
- *
- * @param Image
- */
- public Thumbnail(Image image)
- {
- image_ = image;
- }
- /**
- * Cursor is (not) over
- *
- * @param boolean
- */
- public void setOver(boolean over)
- {
- over_ = over;
- }
- /**
- * Cursor is (not) selected
- * @param selected
- */
- public void setSelected(boolean selected)
- {
- selected_ = selected;
- }
- /**
- * Set thumbnail area
- *
- * @param Rectangle
- */
- public void setArea(Rectangle eventBox)
- {
- eventBox_ = eventBox;
- }
- /**
- * Is cursor over?
- *
- * @return boolean
- */
- public boolean getOver()
- {
- return over_;
- }
- /**
- * Is thumbnail selected?
- *
- * @return boolean
- */
- public boolean getSelected()
- {
- return selected_;
- }
- /**
- * Get wrapped image
- *
- * @return Image
- */
- public Image getImage()
- {
- return image_;
- }
- /**
- * Get thumbnail area
- *
- * @return Rectangle
- */
- public Rectangle getArea()
- {
- return eventBox_;
- }
- /**
- * Gets the image data from the file. This allows generation of
- * thumbnails, view of the image, etc.
- *
- * @see java.awt.Image
- * @return The image data as java.awt.Image
- */
- public BufferedImage getImageData()
- {
- return image_.getImageData();
- }
- /**
- * Gets the image data at the specified resolution
- *
- * @param Integer width
- * @return BufferedImage resized image
- */
- public ImageData getImageData(Integer width)
- {
- return resizeImage(getImageData(), width);
- }
- /**
- * Gets the image thumbnail data from the image file, memory, or the cache.
- *
- * @see java.awt.Image
- * @return The thumbnail data as java.awt.Image
- */
- public ImageData getThumbData()
- {
- generateThumb();
- if (thumbDataCached && !thumbDataInMemory){
- try {
- FileInputStream fileStream = new FileInputStream(thumbFile);
- thumbData = new ImageData(fileStream);
- fileStream.close();
- thumbDataInMemory = true;
- } catch (IOException exception) {
- thumbFile.delete();
- thumbDataCached = false;
- isCorrupt = true;
- Logger.log("Failed to load thumbnail from cache");
- } catch (SWTException exception) {
- thumbFile.delete();
- thumbDataCached = false;
- isCorrupt = true;
- Logger.log("Failed to load thumbnail from cache");
- } catch (NullPointerException exception) {
- thumbDataCached = false;
- Logger.log("Failed to load thumbnail from cache");
- }
- }
- return thumbData;
- }
- /**
- * Generates the thumbnail data from the image file and saves it to memory
- */
- private void generateThumb()
- {
- if (!thumbDataInMemory && !thumbDataCached) {
- Integer width = Integer.valueOf(Config.getInstance().getInt(Config.THUMB_SIZE, Config.THUMB_SIZE_DEFAULT));
- thumbData = getImageData(width);
- thumbDataInMemory = true;
- }
- }
- /**
- * Resized the passed BufferedImage to the specified width and height
- *
- * @param BufferedImage image
- * @param Integer width
- * @return BufferedImage the resized image
- */
- private ImageData resizeImage(BufferedImage imageData, Integer width)
- {
- if (imageData == null) return null;
- //org.eclipse.swt.graphics.Image image = new org.eclipse.swt.graphics.Image(null, imageData);
- BufferedImage tmp = new BufferedImage(width, width, BufferedImage.TYPE_INT_RGB);
- Graphics2D g2 = tmp.createGraphics();
- g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
- g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
- if (imageData.getWidth() < imageData.getHeight()) {
- Integer diff = imageData.getHeight() - imageData.getWidth();
- Integer halfDiff = diff / 2;
- g2.drawImage(imageData, 0, 0, width, width, 0, halfDiff, imageData.getWidth(), halfDiff + (imageData.getHeight() - diff), null);
- } else if (imageData.getWidth() > imageData.getHeight()) {
- Integer diff = imageData.getWidth() - imageData.getHeight();
- Integer halfDiff = diff / 2;
- g2.drawImage(imageData, 0, 0, width, width, halfDiff, 0, halfDiff + (imageData.getWidth() - diff), imageData.getHeight(), null);
- } else {
- g2.drawImage(imageData, 0, 0, width, width, 0, 0, imageData.getWidth(), imageData.getHeight(), null);
- }
- g2.dispose();
- return convertToSWT(tmp);
- }
- /**
- * Takes the thumbnail and places it in a temporary file. A reference to
- * the file is placed in private
- */
- public void cacheThumb()
- {
- if (!thumbDataCached) {
- createCacheFile();
- try {
- ImageLoader loader = new ImageLoader();
- loader.data = new ImageData[] {getThumbData()};
- FileOutputStream outputStream = new FileOutputStream(thumbFile);
- loader.save(outputStream, SWT.IMAGE_JPEG);
- outputStream.close();
- thumbDataCached = true;
- } catch (IOException exception) {
- Logger.log("Failed to save thumbnail cache");
- } catch (NullPointerException exception) {
- Logger.log("Failed to save thumbnail cache");
- } catch (SWTException exception) {
- Logger.log("Failed to save thumbnail cache");
- }
- }
- }
- /**
- * Create a new unique file in the THUMB_DIR for this thumbnail cache
- */
- protected void createCacheFile()
- {
- if (!thumbDataCached) {
- try {
- thumbFile = File.createTempFile("bz_", ".jpg");
- thumbFile.deleteOnExit();
- } catch (IOException exception) {
- Logger.log("Failed to create thumbnail cache file");
- Registry.getCommandManager().executeCommand(new OpenErrorWindow(exception));
- }
- }
- }
- /**
- * Makes sure the thumbnail has been cached, then removes thumbnail data
- * from memory.
- */
- public void flushThumbData()
- {
- cacheThumb();
- destroyMemory();
- }
- /**
- * Deletes thumbnail file from cache, and destroys file object
- */
- public void destroyCache()
- {
- if (thumbDataCached) {
- if (thumbFile != null) {
- thumbFile.delete();
- }
- thumbFile = null;
- thumbDataCached = false;
- }
- }
- /**
- * Deletes thumbnail from memory
- */
- public void destroyMemory()
- {
- if (thumbDataInMemory) {
- thumbData = null;
- thumbDataInMemory = false;
- }
- }
- /**
- * Check if thumbnail has been generated yet
- *
- * @return boolean
- */
- public boolean thumbIsGenerated()
- {
- return ((thumbDataInMemory && (thumbData != null)) || thumbDataCached);
- }
- /**
- * Check if thumbnail is stored in memory
- *
- * @return boolean
- */
- public boolean thumbInMemory()
- {
- return thumbDataInMemory;
- }
- /**
- * Convert AWT to SWT
- *
- * @param BufferedImage
- * @return ImageData
- */
- public static ImageData convertToSWT(BufferedImage bufferedImage) {
- if (bufferedImage.getColorModel() instanceof DirectColorModel) {
- DirectColorModel colorModel = (DirectColorModel) bufferedImage.getColorModel();
- PaletteData palette = new PaletteData(
- colorModel.getRedMask(),
- colorModel.getGreenMask(),
- colorModel.getBlueMask()
- );
- ImageData data = new ImageData(
- bufferedImage.getWidth(),
- bufferedImage.getHeight(), colorModel.getPixelSize(),
- palette
- );
- WritableRaster raster = bufferedImage.getRaster();
- int[] pixelArray = new int[3];
- for (int y = 0; y < data.height; y++) {
- for (int x = 0; x < data.width; x++) {
- raster.getPixel(x, y, pixelArray);
- int pixel = palette.getPixel(
- new RGB(pixelArray[0], pixelArray[1], pixelArray[2])
- );
- data.setPixel(x, y, pixel);
- }
- }
- return data;
- } else if (bufferedImage.getColorModel() instanceof IndexColorModel) {
- IndexColorModel colorModel = (IndexColorModel) bufferedImage.getColorModel();
- int size = colorModel.getMapSize();
- byte[] reds = new byte[size];
- byte[] greens = new byte[size];
- byte[] blues = new byte[size];
- colorModel.getReds(reds);
- colorModel.getGreens(greens);
- colorModel.getBlues(blues);
- RGB[] rgbs = new RGB[size];
- for (int i = 0; i < rgbs.length; i++) {
- rgbs[i] = new RGB(reds[i] & 0xFF, greens[i] & 0xFF, blues[i] & 0xFF);
- }
- PaletteData palette = new PaletteData(rgbs);
- ImageData data = new ImageData(
- bufferedImage.getWidth(),
- bufferedImage.getHeight(),
- colorModel.getPixelSize(),
- palette
- );
- data.transparentPixel = colorModel.getTransparentPixel();
- WritableRaster raster = bufferedImage.getRaster();
- int[] pixelArray = new int[1];
- for (int y = 0; y < data.height; y++) {
- for (int x = 0; x < data.width; x++) {
- raster.getPixel(x, y, pixelArray);
- data.setPixel(x, y, pixelArray[0]);
- }
- }
- return data;
- }
- return null;
- }
- /**
- * Check if file couldn't be loaded
- *
- * @return boolean
- */
- public boolean isCorrupt()
- {
- return isCorrupt;
- }
- /**
- * Comparison function, for sorting
- *
- * @return int
- */
- public int compareTo(Thumbnail thumbnail2) {
- return image_.compareTo(thumbnail2.getImage());
- }
- public boolean equals(Object item2) {
- return image_.equals(item2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement