Class FrameBuffer


  • public final class FrameBuffer
    extends Object
    A 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 FrameBuffer.Viewport is a two-dimensional sub array of a FrameBuffer.

    A FrameBuffer has a default FrameBuffer.Viewport. The current FrameBuffer.Viewport is represented by its upper-left-hand corner and its lower-right-hand corner.

    FrameBuffer and FrameBuffer.Viewport coordinates act like Java Graphics2D coordinates; the positive x direction is to the right and the positive y direction is downward.

    • Constructor Detail

      • FrameBuffer

        public FrameBuffer​(int w,
                           int h)
        Construct a FrameBuffer with the given dimensions.

        Initialize the FrameBuffer to hold all black pixels.

        The default FrameBuffer.Viewport is the whole FrameBuffer.

        Parameters:
        w - width of the FrameBuffer.
        h - height of the FrameBuffer.
      • FrameBuffer

        public FrameBuffer​(int w,
                           int h,
                           Color c)
        Construct a FrameBuffer with the given dimensions.

        Initialize the FrameBuffer to the given Color.

        The default FrameBuffer.Viewport is the whole FrameBuffer.

        Parameters:
        w - width of the FrameBuffer.
        h - height of the FrameBuffer.
        c - background Color for the FrameBuffer
      • FrameBuffer

        public FrameBuffer​(FrameBuffer sourceFB)
        Create a FraameBuffer from the pixel data of another FrameBuffer.

        The size of the new FrameBuffer will be the size of the source FrameBuffer.

        The default FrameBuffer.Viewport is the whole FrameBuffer.

        Parameters:
        sourceFB - FrameBuffer to use as the source of the pixel data
      • FrameBuffer

        public FrameBuffer​(String inputFileName)
        Construct a FrameBuffer from a PPM image file.

        The size of the FrameBuffer will be the size of the image.

        The default FrameBuffer.Viewport is the whole FrameBuffer.

        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 this FrameBuffer.
        Returns:
        width of this FrameBuffer
      • getHeightFB

        public int getHeightFB()
        Get the height of this FrameBuffer.
        Returns:
        height of this FrameBuffer
      • setViewport

        public void setViewport()
        Set the default Viewport to be this whole FrameBuffer.
      • setViewport

        public void setViewport​(int vp_ul_x,
                                int vp_ul_y,
                                int width,
                                int height)
        Set the default Viewport with the given upper-left-hand corner, width and height within this FrameBuffer.
        Parameters:
        vp_ul_x - upper left hand x-coordinate of default Viewport
        vp_ul_y - upper left hand y-coordinate of default Viewport
        width - default Viewport's width
        height - default Viewport's height
      • setBackgroundColorFB

        public void setBackgroundColorFB​(Color c)
        Set the FrameBuffer's background color.

        NOTE: This method does not clear the pixels of the FrameBuffer to the given Color. To actually change all the FrameBuffer's pixels to the given Color, use the clearFB() method.

        Parameters:
        c - FrameBuffer's new background Color
      • clearFB

        public void clearFB()
        Clear the FrameBuffer using its background color.
      • clearFB

        public void clearFB​(Color c)
        Clear the FrameBuffer using the given Color.
        Parameters:
        c - Color to clear FrameBuffer with
      • getPixelFB

        public Color getPixelFB​(int x,
                                int y)
        Get the Color of the pixel with coordinates (x,y) in the FrameBuffer.
        Parameters:
        x - horizontal coordinate within the FrameBuffer
        y - vertical coordinate within the FrameBuffer
        Returns:
        the Color of the pixel at the given pixel coordinates
      • setPixelFB

        public void setPixelFB​(int x,
                               int y,
                               Color c)
        Set the Color of the pixel with coordinates (x,y) in the FrameBuffer.
        Parameters:
        x - horizontal coordinate within the FrameBuffer
        y - vertical coordinate within the FrameBuffer
        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 the FrameBuffer.
        Parameters:
        x - horizontal coordinate within the FrameBuffer
        y - vertical coordinate within the FrameBuffer
        c - combined RGB value for the pixel at the given pixel coordinates
      • convertRed2FB

        public FrameBuffer convertRed2FB()
        Create a new FrameBuffer containing the pixel data from just the red plane of this FrameBuffer.
        Returns:
        FrameBuffer object holding just red pixel data from this FrameBuffer
      • convertGreen2FB

        public FrameBuffer convertGreen2FB()
        Create a new FrameBuffer containing the pixel data from just the green plane of this FrameBuffer.
        Returns:
        FrameBuffer object holding just green pixel data from this FrameBuffer
      • convertBlue2FB

        public FrameBuffer convertBlue2FB()
        Create a new FrameBuffer containing the pixel data from just the blue plane of this FrameBuffer.
        Returns:
        FrameBuffer object holding just blue pixel data from this FrameBuffer
      • dumpFB2File

        public void dumpFB2File​(String filename,
                                String formatName)
        Write this FrameBuffer to the specified image file using the specified file format.
        Parameters:
        filename - name of the image file to hold framebuffer data
        formatName - 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 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).

        Parameters:
        ul_x - upper left hand x-coordinate of pixel data rectangle
        ul_y - upper left hand y-coordinate of pixel data rectangle
        lr_x - lower right hand x-coordinate of pixel data rectangle
        lr_y - lower right hand y-coordinate of pixel data rectangle
        filename - name of the image file to hold pixel data
        formatName - informal name of the image format
      • toString

        public String toString()
        For debugging very small FrameBuffer objects.
        Overrides:
        toString in class Object
        Returns:
        a String representation of this FrameBuffer
      • fbTestPattern

        public void fbTestPattern()
        A simple test of the FrameBuffer class.

        It fills the framebuffer with a test pattern.

      • main

        public static void main​(String[] args)
        A main() method for testing the FrameBuffer class.
        Parameters:
        args - array of command-line arguments