deadbeef.SupTools
Class Bitmap

java.lang.Object
  extended by deadbeef.SupTools.Bitmap

public class Bitmap
extends java.lang.Object

Class to handle byte based bitmaps with a separate palette up to 256 colors.

Author:
0xdeadbeef

Constructor Summary
Bitmap(Bitmap bm)
          Constructor (to create deep copy from existing Bitmap).
Bitmap(int w, int h)
          Constructor (without clearing of bitmap).
Bitmap(int w, int h, byte[] buf)
          Constructor (without clearing of bitmap).
Bitmap(int w, int h, int col)
          Constructor (with clearing of bitmap).
 
Method Summary
 void clear(int color)
          Clear bitmap with color index.
 Bitmap convertLm(Palette pal, int alphaThr, int[] lumThr)
          Convert a palletized Bitmap (where each palette entry has individual alpha) to a Bitmap with N color palette, where:
index0 = transparent, index1 = light color, ... , indexN-2 = dark color, indexN-1 = black.
 Bitmap crop(int x, int y, int w, int h)
          Create cropped Bitmap.
 void fillRect(int x, int y, int w, int h, int color)
          Fill rectangular range inside bitmap with color index.
 BitmapBounds getBounds(Palette pal, int alphaThr)
          Get cropping bounds of Bitmap (first/last x/y coordinates that contain visible pixels).
 int getHeight()
          Get height of bitmap.
 int getHighestColorIndex(Palette p)
          Return the highest used palette entry.
 java.awt.image.BufferedImage getImage(Palette pal)
          Convert Bitmap to buffered image that can be used for display.
 byte[] getImg()
          Get image buffer of bitmap.
 byte getPixel(int x, int y)
          Get color index of pixel at position x,y.
 int getPrimaryColorIndex(Palette pal, int alphaThr)
          Find the most common color that is as light and opaque as possible
(the darker and more transparent a color is, the smaller is its influence).
 int getWidth()
          Get width of bitmap.
 Bitmap scaleBilinear(int sizeX, int sizeY, Palette pal)
          Scales a palletized Bitmap to a Bitmap with the same palette using bilinear filtering.
 PaletteBitmap scaleBilinear(int sizeX, int sizeY, Palette pal, boolean dither)
          Scales a palletized Bitmap to a Bitmap with a new quantized Palette using bilinear filtering.
 Bitmap scaleBilinearLm(int sizeX, int sizeY, Palette pal, int alphaThr, int[] lumThr)
          Scales a palletized Bitmap (where each palette entry has individual alpha) using bilinear filtering to a Bitmap with N color palette, where:
index0 = transparent, index1 = light color, ... , indexN-2 = dark color, indexN-1 = black.
 Bitmap scaleFilter(int sizeX, int sizeY, Palette pal, Filter f)
          Scales a palletized Bitmap to a Bitmap with the same Palette using a given scaling filter.
 PaletteBitmap scaleFilter(int sizeX, int sizeY, Palette pal, Filter f, boolean dither)
          Scales a palletized Bitmap to a Bitmap with a new quantized Palette using a given scaling filter.
 Bitmap scaleFilterLm(int sizeX, int sizeY, Palette pal, int alphaThr, int[] lumThr, Filter f)
          Scales a palletized Bitmap (where each palette entry has individual alpha) using a given scaling filter to a Bitmap with N color palette, where:
index0 = transparent, index1 = light color, ... , indexN-2 = dark color, indexN-1 = black
 void setPixel(int x, int y, byte color)
          Set color index of pixel at position x,y.
 int[] toARGB(Palette pal)
          Convert Bitmap to Integer array filled with ARGB values.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Bitmap

public Bitmap(int w,
              int h,
              int col)
Constructor (with clearing of bitmap).

Parameters:
w - Width
h - Height
col - Color index to use for initial clearing

Bitmap

public Bitmap(int w,
              int h)
Constructor (without clearing of bitmap).

Parameters:
w - Width
h - Height

Bitmap

public Bitmap(int w,
              int h,
              byte[] buf)
Constructor (without clearing of bitmap).

Parameters:
w - Width
h - Height
buf - Image buffer

Bitmap

public Bitmap(Bitmap bm)
Constructor (to create deep copy from existing Bitmap).

Parameters:
bm - Bitmap to create deep copy from
Method Detail

clear

public void clear(int color)
Clear bitmap with color index.

Parameters:
color - Color index to use for clearing

fillRect

public void fillRect(int x,
                     int y,
                     int w,
                     int h,
                     int color)
Fill rectangular range inside bitmap with color index.

Parameters:
x - X coordinate of rectangle
y - Y coordinate of rectangle
w - Width of rectangle
h - Height of rectangle
color - Color index to use for filling.

setPixel

public void setPixel(int x,
                     int y,
                     byte color)
Set color index of pixel at position x,y.

Parameters:
x - X position
y - Y position
color - Color index

getPixel

public byte getPixel(int x,
                     int y)
Get color index of pixel at position x,y.

Parameters:
x - X position
y - Y position
Returns:
Color index pi pixel at position x,y

getImage

public java.awt.image.BufferedImage getImage(Palette pal)
Convert Bitmap to buffered image that can be used for display.

Parameters:
pal - Palette
Returns:
Image as BufferedImage

getPrimaryColorIndex

public int getPrimaryColorIndex(Palette pal,
                                int alphaThr)
Find the most common color that is as light and opaque as possible
(the darker and more transparent a color is, the smaller is its influence).

Parameters:
pal - Palette
alphaThr - Alpha threshold (all colors with alpha < alphaThr will be ignored)
Returns:
Color index of the most common, lightest and most opaque color

getHighestColorIndex

public int getHighestColorIndex(Palette p)
Return the highest used palette entry.

Parameters:
p - Palette
Returns:
Index of highest palette entry used in Bitmap

convertLm

public Bitmap convertLm(Palette pal,
                        int alphaThr,
                        int[] lumThr)
Convert a palletized Bitmap (where each palette entry has individual alpha) to a Bitmap with N color palette, where:
index0 = transparent, index1 = light color, ... , indexN-2 = dark color, indexN-1 = black.

Parameters:
pal - Palette of the source Bitmap
alphaThr - Threshold for alpha (transparency), lower = more transparent
lumThr - Threshold for luminances. For N-1 luminances, N-2 thresholds are needed lumThr[0] is the threshold for the lightest color (-> idx 1) lumThr[N-2] is the threshold for the darkest color (-> idx N-1)
Returns:
Bitmap which uses a fixed frame Palette.

scaleBilinearLm

public Bitmap scaleBilinearLm(int sizeX,
                              int sizeY,
                              Palette pal,
                              int alphaThr,
                              int[] lumThr)
Scales a palletized Bitmap (where each palette entry has individual alpha) using bilinear filtering to a Bitmap with N color palette, where:
index0 = transparent, index1 = light color, ... , indexN-2 = dark color, indexN-1 = black.

Parameters:
sizeX - Target width
sizeY - Target height
pal - Palette of the source Bitmap
alphaThr - Threshold for alpha (transparency), lower = more transparent
lumThr - Threshold for luminances. For N-1 luminances, N-2 thresholds are needed lumThr[0] is the threshold for the lightest color (-> idx 1) lumThr[N-2] is the threshold for the darkest color (-> idx N-1)
Returns:
Scaled Bitmap which uses a fixed frame Palette.

scaleFilterLm

public Bitmap scaleFilterLm(int sizeX,
                            int sizeY,
                            Palette pal,
                            int alphaThr,
                            int[] lumThr,
                            Filter f)
Scales a palletized Bitmap (where each palette entry has individual alpha) using a given scaling filter to a Bitmap with N color palette, where:
index0 = transparent, index1 = light color, ... , indexN-2 = dark color, indexN-1 = black

Parameters:
sizeX - Target width
sizeY - Target height
pal - Palette of the source Bitmap
alphaThr - Threshold for alpha (transparency), lower = more transparent
lumThr - Threshold for luminances. For N-1 luminances, N-2 thresholds are needed lumThr[0] is the threshold for the lightest color (-> idx 1) lumThr[N-2] is the threshold for the darkest color (-> idx N-1)
f - Filter for scaling
Returns:
Scaled Bitmap which uses a fixed frame Palette.

scaleBilinear

public Bitmap scaleBilinear(int sizeX,
                            int sizeY,
                            Palette pal)
Scales a palletized Bitmap to a Bitmap with the same palette using bilinear filtering.

Parameters:
sizeX - Target width
sizeY - Target height
pal - Palette of the source Bitmap
Returns:
Scaled Bitmap which uses the same Palette as the source Bitmap.

scaleBilinear

public PaletteBitmap scaleBilinear(int sizeX,
                                   int sizeY,
                                   Palette pal,
                                   boolean dither)
Scales a palletized Bitmap to a Bitmap with a new quantized Palette using bilinear filtering.

Parameters:
sizeX - Target width
sizeY - Target height
pal - Palette of the source Bitmap
dither - True: apply dithering
Returns:
Scaled Bitmap and new Palette

scaleFilter

public Bitmap scaleFilter(int sizeX,
                          int sizeY,
                          Palette pal,
                          Filter f)
Scales a palletized Bitmap to a Bitmap with the same Palette using a given scaling filter.

Parameters:
sizeX - Target width
sizeY - Target height
pal - Palette of the source Bitmap
f - Filter for scaling
Returns:
Scaled Bitmap which uses the same Palette as the source Bitmap.

scaleFilter

public PaletteBitmap scaleFilter(int sizeX,
                                 int sizeY,
                                 Palette pal,
                                 Filter f,
                                 boolean dither)
Scales a palletized Bitmap to a Bitmap with a new quantized Palette using a given scaling filter.

Parameters:
sizeX - Target width
sizeY - Target height
pal - Palette of the source Bitmap
f - Filter for scaling
dither - True: apply dithering
Returns:
Scaled Bitmap and new Palette

toARGB

public int[] toARGB(Palette pal)
Convert Bitmap to Integer array filled with ARGB values.

Parameters:
pal - Palette
Returns:
Integer array filled with ARGB values.

crop

public Bitmap crop(int x,
                   int y,
                   int w,
                   int h)
Create cropped Bitmap.

Parameters:
x - X offset in source bitmap
y - Y offset in source bitmap
w - Width of cropping window
h - Height of cropping window
Returns:
Cropped Bitmap sized w*h

getBounds

public BitmapBounds getBounds(Palette pal,
                              int alphaThr)
Get cropping bounds of Bitmap (first/last x/y coordinates that contain visible pixels).

Parameters:
pal - Palette
alphaThr - Alpha threshold (only pixels with alpha >= alphaThr will be treated as visible)
Returns:
BitmapBounds containing bounds

getWidth

public int getWidth()
Get width of bitmap.

Returns:
Width of bitmap

getHeight

public int getHeight()
Get height of bitmap.

Returns:
Height of bitmap.

getImg

public byte[] getImg()
Get image buffer of bitmap.

Returns:
Image buffer as array of byte (internal resource, changing it will change the Bitmap)