public class Pixbuf extends Object
Pixbuf is just here to be efficient at handling images that are going to placed in your GTK user interfaces. If you want to draw on an image, use Cairo's ImageSurface. If you want to otherwise manipulate the image, use a dedicated image processing library to load the data as it will doubtless provide for more efficient storage anticipating the processing tasks it will facilitate.
Constructor and Description |
---|
Pixbuf(byte[] data)
Construct a new Pixbuf object from image data already in memory.
|
Pixbuf(byte[] data,
int width,
int height,
boolean preserveAspectRatio)
Construct a new Pixbuf from in-memory data and scale it.
|
Pixbuf(int width,
int height) |
Pixbuf(String filename)
Construct a new Pixbuf object from the image found in
filename . |
Pixbuf(String filename,
int width,
int height,
boolean preserveAspectRatio)
Construct a new Pixbuf object by loading an image from a given
filename, but scaling it.
|
Modifier and Type | Method and Description |
---|---|
Pixbuf |
copy()
Create an identical copy of this Pixbuf.
|
static int |
getFileInfoHeight(String filename)
Query an image on disk for its height.
|
static int |
getFileInfoWidth(String filename)
Query an image on disk for its width.
|
int |
getHeight()
Get the height of this Pixbuf, in pixels
|
int |
getNumChannels()
Get the number of colour channels in this Pixbuf.
|
byte[] |
getPixels()
Get a the individual pixel values comprising this Pixbuf.
|
int |
getWidth()
Get the width of this Pixbuf, in pixels
|
void |
save(String filename,
PixbufFormat type)
Write this Pixbuf to a file.
|
Pixbuf |
scale(int width,
int height,
InterpType algorithm)
Create a new Pixbuf, applying the scaling factors inherent in the
ratios between the size of this Pixbuf and the new image size specified
by
width and height . |
public Pixbuf(byte[] data) throws IOException
IOException
public Pixbuf(byte[] data, int width, int height, boolean preserveAspectRatio) throws IOException
See Pixbuf(byte[])
for info on in-memory data.
See Pixbuf(String, int, int, boolean)
for info on scaling.
IOException
public Pixbuf(int width, int height)
public Pixbuf(String filename) throws FileNotFoundException
filename
.FileNotFoundException
public Pixbuf(String filename, int width, int height, boolean preserveAspectRatio) throws FileNotFoundException
If preserving the aspect ratio,
-1
will cause the image to be scaled to the
exact given height
-1
will cause the image to be scaled to the
exact given width.
-1
means to not scale the image
at all in that dimension.
FileNotFoundException
public Pixbuf copy()
Since you can happily reuse a Pixbuf instance in multiple places, you generally won't need this unless doing mutating operations like rescaling.
public static int getFileInfoHeight(String filename) throws IOException
This function is the compliment of getFileInfoWidth()
; see there.
IOException
public static int getFileInfoWidth(String filename) throws IOException
This is a utility function where the minimum amount is read in order to determine metadata about the file in question. You'll get an IOException if the gdk-pixbuf library's image loaders can't figure out the format of the file.
IOException
public int getHeight()
public int getNumChannels()
3
for RGB or 4
for an RGBA image.
You don't actually need this; if you're working with the pixel data
from getPixels()
just divide the length of the
returned array by (width *
height).
public byte[] getPixels()
Image data in a Pixbuf is stored in memory in an uncompressed but
packed format. Rows in the image are stored top to bottom, and in each
row pixels are stored from left to right. The returned array is, as you
would expect, the bytes comprising each single pixel in a sequential
series. There will be either three (RGB) or four (RGBA) bytes per
pixel, see getNumChannels()
.
The return array is of type of is byte
but you can expect
unsigned values in the range 0
to 255
. If you
need the actual values you'll have to &
with
0xFF
to get yourself to the correct unsigned integer.
You should not need to call this. See the caveats at the top of this class; changing the values of this array will not change the underlying image.
The underlying library stores Pixbufs in a format which is efficient for internal representation and memory operations, though not necessarily efficient for size and frequently extra bits are added as alignment padding. We do some minor copying so that the array returned is one byte per channel and Cartesian.
In the current GDK Pixbuf library implementation, the red, green, blue and optional alpha are fixed at 8 bits per channel.
public int getWidth()
public void save(String filename, PixbufFormat type)
The various file formats that GDK is actually capable of writing to are specified by the constants on PixbufFormat. For example, you can save a screenshot as a PNG using:
Pixbuf p; ... p.save("Screenshot.png", PixbufFormat.PNG);
public Pixbuf scale(int width, int height, InterpType algorithm)
width
and height
.
In general you probably want to use the BILINEAR
interpolation algorithm.