public class FrameBuffer extends Object
FrameBuffer
represents a two-dimensional array of pixel data.
The pixel data is stored as a one dimensional array in row-major order.
The first row of data should be displayed as the top row of pixels
in the image.
A "viewport" is a two-dimensional sub array of a FrameBuffer
.
A FrameBuffer
has one current viewport. The current viewport
is represented by its upper-left-hand corner and its lower-right-hand
corner.
FrameBuffer
and viewport coordinates act like Java
Graphics2D
coordinates; the positive x direction is
to the right and the positive y direction is downward.
Modifier and Type | Field and Description |
---|---|
int[] |
pixel_buffer |
Constructor and Description |
---|
FrameBuffer(int w,
int h)
Construct a
FrameBuffer with the given dimensions. |
FrameBuffer(int w,
int h,
Color c)
Construct a
FrameBuffer with the given dimensions and
initialize the FrameBuffer to the given Color . |
FrameBuffer(String inputFileName)
Construct a
FrameBuffer from a PPM image file. |
Modifier and Type | Method and Description |
---|---|
void |
clearFB()
Clear the
FrameBuffer using its background color. |
void |
clearFB(Color c)
Clear the
FrameBuffer using the given Color . |
void |
clearVP()
Clear the viewport using its background color.
|
void |
clearVP(Color c)
Clear the viewport using the given
Color . |
FrameBuffer |
convertBlue2FB()
Create a new
FrameBuffer containing the pixel data
from just the blue plane of this FrameBuffer . |
FrameBuffer |
convertGreen2FB()
Create a new
FrameBuffer containing the pixel data
from just the green plane of this FrameBuffer . |
FrameBuffer |
convertRed2FB()
Create a new
FrameBuffer containing the pixel data
from just the red plane of this FrameBuffer . |
FrameBuffer |
convertVP2FB()
Create a new
FrameBuffer containing the pixel data
from this FrameBuffer 's current viewport rectangle. |
void |
dumpFB2File(String filename)
Write this
FrameBuffer to the specified PPM file. |
void |
dumpFB2File(String filename,
String formatName)
Write this
FrameBuffer to the specified image file
using the specified file format. |
void |
dumpPixels2File(int ul_x,
int ul_y,
int lr_x,
int lr_y,
String filename)
Write a rectangular sub array of pixels from this
FrameBuffer
to the specified PPM file. |
void |
dumpPixels2File(int ul_x,
int ul_y,
int lr_x,
int lr_y,
String filename,
String formatName)
Write a rectangular sub array of pixels from this
FrameBuffer
to the specified image file using the specified file format. |
void |
dumpVP2File(String filename)
Write the viewport to the specified PPM file.
|
void |
dumpVP2File(String filename,
String formatName)
Write the viewport to the specified image file
using the specified file format.
|
void |
fbTest()
A simple test of the
FrameBuffer class. |
Color |
getBgColorFB()
Get the background
Color of the FrameBuffer . |
Color |
getBgColorVP()
Get the background
Color of the viewport. |
int |
getHeightFB()
Get the height of the
FrameBuffer . |
int |
getHeightVP()
Get the height of the viewport.
|
Point |
getLocationVP()
Get the upper left hand corner of the viewport.
|
Color |
getPixelFB(int x,
int y)
Get the
Color of the pixel with coordinates
(x,y) in the framebuffer. |
Color |
getPixelVP(int x,
int y)
Get the
Color of the pixel with coordinates
(x,y) relative to the current viewport. |
int |
getWidthFB()
Get the width of the
FrameBuffer . |
int |
getWidthVP()
Get the width of the viewport.
|
static void |
main(String[] args)
A
main() method for testing the FrameBuffer class. |
void |
setBgColorFB(Color c)
Set the background
Color of the FrameBuffer . |
void |
setBgColorVP(Color c)
Set the background
Color of the viewport. |
void |
setPixelFB(int x,
int y,
Color c)
Set the
Color of the pixel with coordinates
(x,y) in the FrameBuffer . |
void |
setPixelVP(int x,
int y,
Color c)
Set the
Color of the pixel with coordinates
(x,y) relative to the current viewport. |
void |
setViewport()
Set the viewport to be the whole
FrameBuffer , |
void |
setViewport(int vp_ul_x,
int vp_ul_y,
FrameBuffer sourceFB)
Create a viewport from a
FrameBuffer . |
void |
setViewport(int vp_ul_x,
int vp_ul_y,
int width,
int height)
Set the coordinates, within the
FrameBuffer , of
the viewport's upper-left-hand corner, width, and height. |
void |
setViewport(int vp_ul_x,
int vp_ul_y,
String inputFileName)
Create a viewport from a PPM image file.
|
void |
vpTest()
A simple test of the viewport.
|
public int[] pixel_buffer
public FrameBuffer(int w, int h)
FrameBuffer
with the given dimensions.
Initialize the framebuffer to hold all black pixels.w
- width of the FrameBuffer
.h
- height of the FrameBuffer
.public FrameBuffer(int w, int h, Color c)
FrameBuffer
with the given dimensions and
initialize the FrameBuffer
to the given Color
.w
- width of the FrameBuffer
.h
- height of the FrameBuffer
.c
- background Color
for the FrameBuffer
public FrameBuffer(String inputFileName)
FrameBuffer
from a PPM image file.
This can be used to initialize a FrameBuffer
with a background image.
inputFileName
- must name a PPM image file with magic number P6.public void clearFB()
FrameBuffer
using its background color.public void clearFB(Color c)
FrameBuffer
using the given Color
.c
- Color
to clear FrameBuffer
withpublic void clearVP()
public void clearVP(Color c)
Color
.c
- Color
to clear current viewport withpublic FrameBuffer convertBlue2FB()
FrameBuffer
containing the pixel data
from just the blue plane of this FrameBuffer
.
The new FrameBuffer
will have the same viewport as this
FrameBuffer
.
FrameBuffer
object holding just blue pixel data from this FrameBuffer
public FrameBuffer convertGreen2FB()
FrameBuffer
containing the pixel data
from just the green plane of this FrameBuffer
.
The new FrameBuffer
will have the same viewport as this
FrameBuffer
.
FrameBuffer
object holding just green pixel data from this FrameBuffer
public FrameBuffer convertRed2FB()
FrameBuffer
containing the pixel data
from just the red plane of this FrameBuffer
.
The new FrameBuffer
will have the same viewport as this
FrameBuffer
.
FrameBuffer
object holding just red pixel data from this FrameBuffer
public FrameBuffer convertVP2FB()
FrameBuffer
containing the pixel data
from this FrameBuffer
's current viewport rectangle.FrameBuffer
object holding pixel data from the current viewport rectanglepublic void dumpFB2File(String filename)
FrameBuffer
to the specified PPM file.
filename
- name of PPM image file to hold FrameBuffer
datapublic void dumpFB2File(String filename, String formatName)
FrameBuffer
to the specified image file
using the specified file format.filename
- name of the image file to hold framebuffer dataformatName
- informal name of the image formatpublic void dumpPixels2File(int ul_x, int ul_y, int lr_x, int lr_y, String filename)
Write a rectangular sub array of pixels from this FrameBuffer
to the specified PPM file.
https://en.wikipedia.org/wiki/Netpbm_format#PPM_example
http://stackoverflow.com/questions/2693631/read-ppm-file-and-store-it-in-an-array-coded-with-c
ul_x
- upper left hand x-coordinate of pixel data rectangleul_y
- upper left hand y-coordinate of pixel data rectanglelr_x
- lower right hand x-coordinate of pixel data rectanglelr_y
- lower right hand y-coordinate of pixel data rectanglefilename
- name of PPM image file to hold pixel datapublic void dumpPixels2File(int ul_x, int ul_y, int lr_x, int lr_y, String filename, String formatName)
Write a rectangular sub array of pixels from this FrameBuffer
to the specified image file using the specified file format.
Use the static method ImageIO.getWriterFormatNames()
to find out what informal image format names can be used
(for example, png, gif, jpg, bmp).
ul_x
- upper left hand x-coordinate of pixel data rectangleul_y
- upper left hand y-coordinate of pixel data rectanglelr_x
- lower right hand x-coordinate of pixel data rectanglelr_y
- lower right hand y-coordinate of pixel data rectanglefilename
- name of the image file to hold pixel dataformatName
- informal name of the image formatpublic void dumpVP2File(String filename)
filename
- name of PPM image file to hold viewport datapublic void dumpVP2File(String filename, String formatName)
filename
- name of the image file to hold viewport dataformatName
- informal name of the image formatpublic void fbTest()
FrameBuffer
class.
It fills the framebuffer with a test pattern.
public Color getBgColorFB()
Color
of the FrameBuffer
.FrameBuffer
's background Color
public Color getBgColorVP()
Color
of the viewport.Color
of current viewportpublic int getHeightFB()
FrameBuffer
.FrameBuffer
's heightpublic int getHeightVP()
public Point getLocationVP()
public Color getPixelFB(int x, int y)
Color
of the pixel with coordinates
(x,y) in the framebuffer.x
- horizontal coordinate within the FrameBuffer
y
- vertical coordinate within the FrameBuffer
Color
of the current pixel at the given pixel coordinatespublic Color getPixelVP(int x, int y)
Color
of the pixel with coordinates
(x,y) relative to the current viewport.x
- horizontal coordinate within the current viewporty
- vertical coordinate within the current viewportColor
of the current pixel at the given viewport coordinatespublic int getWidthFB()
FrameBuffer
.FrameBuffer
's widthpublic int getWidthVP()
public static void main(String[] args)
main()
method for testing the FrameBuffer
class.args
- array of command-line argumentspublic void setBgColorFB(Color c)
Color
of the FrameBuffer
.c
- this FrameBuffer
's new background Color
public void setBgColorVP(Color c)
Color
of the viewport.c
- background Color
of current viewportpublic void setPixelFB(int x, int y, Color c)
Color
of the pixel with coordinates
(x,y) in the FrameBuffer
.x
- horizontal coordinate within the FrameBuffer
y
- vertical coordinate within the FrameBuffer
c
- Color
for the pixel at the given pixel coordinatespublic void setPixelVP(int x, int y, Color c)
Color
of the pixel with coordinates
(x,y) relative to the current viewport.x
- horizontal coordinate within the current viewporty
- vertical coordinate within the current viewportc
- Color
for the pixel at the given viewport coordinatespublic void setViewport()
FrameBuffer
,public void setViewport(int vp_ul_x, int vp_ul_y, FrameBuffer sourceFB)
FrameBuffer
.
The size of the viewport will be the size of the source FrameBuffer
.
vp_ul_x
- upper left hand x-coordinate of new viewport rectanglevp_ul_y
- upper left hand y-coordinate of new viewport rectanglesourceFB
- FrameBuffer
to use as the source of the pixel data..public void setViewport(int vp_ul_x, int vp_ul_y, int width, int height)
FrameBuffer
, of
the viewport's upper-left-hand corner, width, and height.
(Using upper-left-hand corner, width, and height is
like Java's Rectangle
class and
Graphics.drawRect(int, int, int, int)
method.)vp_ul_x
- upper left hand x-coordinate of new viewport rectanglevp_ul_y
- upper left hand y-coordinate of new viewport rectanglewidth
- viewport's widthheight
- viewport's heightpublic void setViewport(int vp_ul_x, int vp_ul_y, String inputFileName)
The size of the viewport will be the size of the image.
This can be used to initialize a viewport with a background image.
vp_ul_x
- upper left hand x-coordinate of new viewport rectanglevp_ul_y
- upper left hand y-coordinate of new viewport rectangleinputFileName
- must name a PPM image file with magic number P6.public void vpTest()
It fills the viewport with a test pattern.