Class FrameBuffer
- java.lang.Object
-
- renderer.framebuffer.FrameBuffer
-
public final class FrameBuffer extends Object
AFrameBuffer
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
FrameBuffer.Viewport
is a two-dimensional sub array of aFrameBuffer
.A
FrameBuffer
has a defaultFrameBuffer.Viewport
. The currentFrameBuffer.Viewport
is represented by its upper-left-hand corner and its lower-right-hand corner.FrameBuffer
andFrameBuffer.Viewport
coordinates act like JavaGraphics2D
coordinates; the positive x direction is to the right and the positive y direction is downward.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
FrameBuffer.Viewport
AViewport
is an inner (non-static nested) class ofFrameBuffer
.
-
Field Summary
Fields Modifier and Type Field Description Color
bgColorFB
int
height
int[]
pixel_buffer
FrameBuffer.Viewport
vp
int
width
-
Constructor Summary
Constructors Constructor Description FrameBuffer(int w, int h)
Construct aFrameBuffer
with the given dimensions.FrameBuffer(int w, int h, Color c)
Construct aFrameBuffer
with the given dimensions.FrameBuffer(String inputFileName)
Construct aFrameBuffer
from a PPM image file.FrameBuffer(FrameBuffer sourceFB)
Create aFraameBuffer
from the pixel data of anotherFrameBuffer
.FrameBuffer(FrameBuffer.Viewport sourceVP)
Create aFrameBuffer
from the pixel data of aFrameBuffer.Viewport
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clearFB()
Clear theFrameBuffer
using its background color.void
clearFB(Color c)
Clear theFrameBuffer
using the givenColor
.FrameBuffer
convertBlue2FB()
Create a newFrameBuffer
containing the pixel data from just the blue plane of thisFrameBuffer
.FrameBuffer
convertGreen2FB()
Create a newFrameBuffer
containing the pixel data from just the green plane of thisFrameBuffer
.FrameBuffer
convertRed2FB()
Create a newFrameBuffer
containing the pixel data from just the red plane of thisFrameBuffer
.void
dumpFB2File(String filename)
Write thisFrameBuffer
to the specified PPM file.void
dumpFB2File(String filename, String formatName)
Write thisFrameBuffer
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 thisFrameBuffer
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 thisFrameBuffer
to the specified image file using the specified file format.void
fbTestPattern()
A simple test of theFrameBuffer
class.Color
getBackgroundColorFB()
Get theFrameBuffer
's background color.int
getHeightFB()
Get the height of thisFrameBuffer
.Color
getPixelFB(int x, int y)
FrameBuffer.Viewport
getViewport()
Get thisFrameBuffer
's defaultViewport
.int
getWidthFB()
Get the width of thisFrameBuffer
.static void
main(String[] args)
Amain()
method for testing theFrameBuffer
class.void
setBackgroundColorFB(Color c)
Set theFrameBuffer
's background color.void
setPixelFB(int x, int y, int c)
Set the combined RGB value of the pixel with coordinates(x,y)
in theFrameBuffer
.void
setPixelFB(int x, int y, Color c)
void
setViewport()
Set the defaultViewport
to be this wholeFrameBuffer
.void
setViewport(int vp_ul_x, int vp_ul_y, int width, int height)
Set the defaultViewport
with the given upper-left-hand corner, width and height within thisFrameBuffer
.String
toString()
For debugging very smallFrameBuffer
objects.
-
-
-
Field Detail
-
width
public final int width
-
height
public final int height
-
pixel_buffer
public final int[] pixel_buffer
-
vp
public final FrameBuffer.Viewport vp
-
-
Constructor Detail
-
FrameBuffer
public FrameBuffer(int w, int h)
Construct aFrameBuffer
with the given dimensions.Initialize the
FrameBuffer
to hold all black pixels.The default
FrameBuffer.Viewport
is the wholeFrameBuffer
.- Parameters:
w
- width of theFrameBuffer
.h
- height of theFrameBuffer
.
-
FrameBuffer
public FrameBuffer(int w, int h, Color c)
Construct aFrameBuffer
with the given dimensions.Initialize the
FrameBuffer
to the givenColor
.The default
FrameBuffer.Viewport
is the wholeFrameBuffer
.- Parameters:
w
- width of theFrameBuffer
.h
- height of theFrameBuffer
.c
- backgroundColor
for theFrameBuffer
-
FrameBuffer
public FrameBuffer(FrameBuffer sourceFB)
Create aFraameBuffer
from the pixel data of anotherFrameBuffer
.The size of the new
FrameBuffer
will be the size of the sourceFrameBuffer
.The default
FrameBuffer.Viewport
is the wholeFrameBuffer
.- Parameters:
sourceFB
-FrameBuffer
to use as the source of the pixel data
-
FrameBuffer
public FrameBuffer(FrameBuffer.Viewport sourceVP)
Create aFrameBuffer
from the pixel data of aFrameBuffer.Viewport
.The size of the new
FrameBuffer
will be the size of the sourceViewport
.The default
FrameBuffer.Viewport
is the wholeFrameBuffer
.- Parameters:
sourceVP
-FrameBuffer.Viewport
to use as the source of the pixel data
-
FrameBuffer
public FrameBuffer(String inputFileName)
Construct aFrameBuffer
from a PPM image file.The size of the
FrameBuffer
will be the size of the image.The default
FrameBuffer.Viewport
is the wholeFrameBuffer
.This can be used to initialize a
FrameBuffer
with a background image.- Parameters:
inputFileName
- must name a PPM image file with magic number P6.
-
-
Method Detail
-
getWidthFB
public int getWidthFB()
Get the width of thisFrameBuffer
.- Returns:
- width of this
FrameBuffer
-
getHeightFB
public int getHeightFB()
Get the height of thisFrameBuffer
.- Returns:
- height of this
FrameBuffer
-
getViewport
public FrameBuffer.Viewport getViewport()
Get thisFrameBuffer
's defaultViewport
.- Returns:
- this
FrameBuffer
's defaultViewport
-
setViewport
public void setViewport()
Set the defaultViewport
to be this wholeFrameBuffer
.
-
setViewport
public void setViewport(int vp_ul_x, int vp_ul_y, int width, int height)
Set the defaultViewport
with the given upper-left-hand corner, width and height within thisFrameBuffer
.- Parameters:
vp_ul_x
- upper left hand x-coordinate of defaultViewport
vp_ul_y
- upper left hand y-coordinate of defaultViewport
width
- defaultViewport
's widthheight
- defaultViewport
's height
-
getBackgroundColorFB
public Color getBackgroundColorFB()
Get theFrameBuffer
's background color.- Returns:
- the
FrameBuffer
's backgroundColor
-
setBackgroundColorFB
public void setBackgroundColorFB(Color c)
Set theFrameBuffer
's background color.NOTE: This method does not clear the pixels of the
FrameBuffer
to the givenColor
. To actually change all theFrameBuffer
's pixels to the givenColor
, use theclearFB()
method.- Parameters:
c
-FrameBuffer
's new backgroundColor
-
clearFB
public void clearFB()
Clear theFrameBuffer
using its background color.
-
clearFB
public void clearFB(Color c)
Clear theFrameBuffer
using the givenColor
.- Parameters:
c
-Color
to clearFrameBuffer
with
-
getPixelFB
public Color getPixelFB(int x, int y)
- Parameters:
x
- horizontal coordinate within theFrameBuffer
y
- vertical coordinate within theFrameBuffer
- Returns:
- the
Color
of the pixel at the given pixel coordinates
-
setPixelFB
public void setPixelFB(int x, int y, Color c)
- Parameters:
x
- horizontal coordinate within theFrameBuffer
y
- vertical coordinate within theFrameBuffer
c
-Color
for the pixel at the given pixel coordinates
-
setPixelFB
public void setPixelFB(int x, int y, int c)
Set the combined RGB value of the pixel with coordinates(x,y)
in theFrameBuffer
.- Parameters:
x
- horizontal coordinate within theFrameBuffer
y
- vertical coordinate within theFrameBuffer
c
- combined RGB value for the pixel at the given pixel coordinates
-
convertRed2FB
public FrameBuffer convertRed2FB()
Create a newFrameBuffer
containing the pixel data from just the red plane of thisFrameBuffer
.- Returns:
FrameBuffer
object holding just red pixel data from thisFrameBuffer
-
convertGreen2FB
public FrameBuffer convertGreen2FB()
Create a newFrameBuffer
containing the pixel data from just the green plane of thisFrameBuffer
.- Returns:
FrameBuffer
object holding just green pixel data from thisFrameBuffer
-
convertBlue2FB
public FrameBuffer convertBlue2FB()
Create a newFrameBuffer
containing the pixel data from just the blue plane of thisFrameBuffer
.- Returns:
FrameBuffer
object holding just blue pixel data from thisFrameBuffer
-
dumpFB2File
public void dumpFB2File(String filename)
Write thisFrameBuffer
to the specified PPM file.- Parameters:
filename
- name of PPM image file to holdFrameBuffer
data
-
dumpPixels2File
public void dumpPixels2File(int ul_x, int ul_y, int lr_x, int lr_y, String filename)
Write a rectangular sub array of pixels from thisFrameBuffer
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
- Parameters:
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 data
-
dumpFB2File
public void dumpFB2File(String filename, String formatName)
Write thisFrameBuffer
to the specified image file using the specified file format.- Parameters:
filename
- name of the image file to hold framebuffer dataformatName
- informal name of the image format
-
dumpPixels2File
public 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 thisFrameBuffer
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).- Parameters:
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 format
-
fbTestPattern
public void fbTestPattern()
A simple test of theFrameBuffer
class.It fills the framebuffer with a test pattern.
-
-