Class FrameBuffer
- java.lang.Object
-
- renderer.framebuffer.FrameBuffer
-
public final class FrameBuffer extends Object
AFrameBufferrepresents 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.Viewportis a two-dimensional sub array of aFrameBuffer.A
FrameBufferhas a defaultFrameBuffer.Viewport. The currentFrameBuffer.Viewportis represented by its upper-left-hand corner and its lower-right-hand corner.FrameBufferandFrameBuffer.Viewportcoordinates act like JavaGraphics2Dcoordinates; 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 classFrameBuffer.ViewportAViewportis an inner (non-static nested) class ofFrameBuffer.
-
Field Summary
Fields Modifier and Type Field Description ColorbgColorFBintheightint[]pixel_bufferFrameBuffer.Viewportvpintwidth
-
Constructor Summary
Constructors Constructor Description FrameBuffer(int w, int h)Construct aFrameBufferwith the given dimensions.FrameBuffer(int w, int h, Color c)Construct aFrameBufferwith the given dimensions.FrameBuffer(String inputFileName)Construct aFrameBufferfrom a PPM image file.FrameBuffer(FrameBuffer sourceFB)Create aFraameBufferfrom the pixel data of anotherFrameBuffer.FrameBuffer(FrameBuffer.Viewport sourceVP)Create aFrameBufferfrom the pixel data of aFrameBuffer.Viewport.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclearFB()Clear theFrameBufferusing its background color.voidclearFB(Color c)Clear theFrameBufferusing the givenColor.FrameBufferconvertBlue2FB()Create a newFrameBuffercontaining the pixel data from just the blue plane of thisFrameBuffer.FrameBufferconvertGreen2FB()Create a newFrameBuffercontaining the pixel data from just the green plane of thisFrameBuffer.FrameBufferconvertRed2FB()Create a newFrameBuffercontaining the pixel data from just the red plane of thisFrameBuffer.voiddumpFB2File(String filename)Write thisFrameBufferto the specified PPM file.voiddumpFB2File(String filename, String formatName)Write thisFrameBufferto the specified image file using the specified file format.voiddumpPixels2File(int ul_x, int ul_y, int lr_x, int lr_y, String filename)Write a rectangular sub array of pixels from thisFrameBufferto the specified PPM file.voiddumpPixels2File(int ul_x, int ul_y, int lr_x, int lr_y, String filename, String formatName)Write a rectangular sub array of pixels from thisFrameBufferto the specified image file using the specified file format.voidfbTestPattern()A simple test of theFrameBufferclass.ColorgetBackgroundColorFB()Get theFrameBuffer's background color.intgetHeightFB()Get the height of thisFrameBuffer.ColorgetPixelFB(int x, int y)FrameBuffer.ViewportgetViewport()Get thisFrameBuffer's defaultViewport.intgetWidthFB()Get the width of thisFrameBuffer.static voidmain(String[] args)Amain()method for testing theFrameBufferclass.voidsetBackgroundColorFB(Color c)Set theFrameBuffer's background color.voidsetPixelFB(int x, int y, int c)Set the combined RGB value of the pixel with coordinates(x,y)in theFrameBuffer.voidsetPixelFB(int x, int y, Color c)voidsetViewport()Set the defaultViewportto be this wholeFrameBuffer.voidsetViewport(int vp_ul_x, int vp_ul_y, int width, int height)Set the defaultViewportwith the given upper-left-hand corner, width and height within thisFrameBuffer.StringtoString()For debugging very smallFrameBufferobjects.
-
-
-
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 aFrameBufferwith the given dimensions.Initialize the
FrameBufferto hold all black pixels.The default
FrameBuffer.Viewportis the wholeFrameBuffer.- Parameters:
w- width of theFrameBuffer.h- height of theFrameBuffer.
-
FrameBuffer
public FrameBuffer(int w, int h, Color c)
Construct aFrameBufferwith the given dimensions.Initialize the
FrameBufferto the givenColor.The default
FrameBuffer.Viewportis the wholeFrameBuffer.- Parameters:
w- width of theFrameBuffer.h- height of theFrameBuffer.c- backgroundColorfor theFrameBuffer
-
FrameBuffer
public FrameBuffer(FrameBuffer sourceFB)
Create aFraameBufferfrom the pixel data of anotherFrameBuffer.The size of the new
FrameBufferwill be the size of the sourceFrameBuffer.The default
FrameBuffer.Viewportis the wholeFrameBuffer.- Parameters:
sourceFB-FrameBufferto use as the source of the pixel data
-
FrameBuffer
public FrameBuffer(FrameBuffer.Viewport sourceVP)
Create aFrameBufferfrom the pixel data of aFrameBuffer.Viewport.The size of the new
FrameBufferwill be the size of the sourceViewport.The default
FrameBuffer.Viewportis the wholeFrameBuffer.- Parameters:
sourceVP-FrameBuffer.Viewportto use as the source of the pixel data
-
FrameBuffer
public FrameBuffer(String inputFileName)
Construct aFrameBufferfrom a PPM image file.The size of the
FrameBufferwill be the size of the image.The default
FrameBuffer.Viewportis the wholeFrameBuffer.This can be used to initialize a
FrameBufferwith 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 defaultViewportto be this wholeFrameBuffer.
-
setViewport
public void setViewport(int vp_ul_x, int vp_ul_y, int width, int height)
Set the defaultViewportwith the given upper-left-hand corner, width and height within thisFrameBuffer.- Parameters:
vp_ul_x- upper left hand x-coordinate of defaultViewportvp_ul_y- upper left hand y-coordinate of defaultViewportwidth- 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
FrameBufferto 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 theFrameBufferusing its background color.
-
clearFB
public void clearFB(Color c)
Clear theFrameBufferusing the givenColor.- Parameters:
c-Colorto clearFrameBufferwith
-
getPixelFB
public Color getPixelFB(int x, int y)
- Parameters:
x- horizontal coordinate within theFrameBuffery- vertical coordinate within theFrameBuffer- Returns:
- the
Colorof the pixel at the given pixel coordinates
-
setPixelFB
public void setPixelFB(int x, int y, Color c)
- Parameters:
x- horizontal coordinate within theFrameBuffery- vertical coordinate within theFrameBufferc-Colorfor 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 theFrameBuffery- vertical coordinate within theFrameBufferc- combined RGB value for the pixel at the given pixel coordinates
-
convertRed2FB
public FrameBuffer convertRed2FB()
Create a newFrameBuffercontaining the pixel data from just the red plane of thisFrameBuffer.- Returns:
FrameBufferobject holding just red pixel data from thisFrameBuffer
-
convertGreen2FB
public FrameBuffer convertGreen2FB()
Create a newFrameBuffercontaining the pixel data from just the green plane of thisFrameBuffer.- Returns:
FrameBufferobject holding just green pixel data from thisFrameBuffer
-
convertBlue2FB
public FrameBuffer convertBlue2FB()
Create a newFrameBuffercontaining the pixel data from just the blue plane of thisFrameBuffer.- Returns:
FrameBufferobject holding just blue pixel data from thisFrameBuffer
-
dumpFB2File
public void dumpFB2File(String filename)
Write thisFrameBufferto the specified PPM file.- Parameters:
filename- name of PPM image file to holdFrameBufferdata
-
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 thisFrameBufferto 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 thisFrameBufferto 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 thisFrameBufferto 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 theFrameBufferclass.It fills the framebuffer with a test pattern.
-
-