Image Processing Basics 3 - Image Buffering


Buffered Image :- As the name suggests it is the buffering of image data.

java.awt.BufferedImage is responsible for Buffering of images in Java
Actually BuffreedImage is nothing but collection of image data,pixels,RGB Colors etc. BuffredImage basically contains two parst. Raster part and Color part. Color part is responsible for storing color of the image to be buffered. Raster part holds the raw image and their representation. Rater images are stored in image files in different formats.Some of the formats are :-

     1)  BitMap :-  a bitmap or pixmap is a type of memory organization or image file format used to store digital images
     2)  OpenRaster :- Open Raster is a file format proposed for the common exchange of layered images between raster graphics editors. It is meant as a replacement for later versions of the Adobe PSD format. OpenRaster is still in development and so far is supported by a few programs.The default file extension for OpenRaster files is ".ora"
      3)  .ico :- This is a format for icons in Microsoft
      4)  . .ICO files contain one or more small images at multiple sizes and color depths
      5)  Cur :- The CUR file format is an almost identical image file format for non-animated cursors  in Microsoft Windows
      6)  Gif
      7)  Jpeg etc

Vector File Formats-They normally deal with size and shapes

  • AI (Adobe Illustrator)
  • CDR (CorelDRAW)
  • EPS (Encapsulated PostScript)
  • WMF (Windows Metafile)
  • DXF (AutoCAD)
  • SVG (Scalable Vector Graphics)

Ok. Lets forget these bull shit theories which can be learned when need arises.( Google’s Help). Coming to the programming part i will demonstrate a simple example of Image buffering and displaying the buffered image.

Actual Program :- 

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class ImageBufferingExample {

Image image;
JFrame frame = new JFrame();
public ImageBufferingExample() {
image = loadImage();
BufferedImage bufferedImage = new BufferedImage(100, 100,BufferedImage.TYPE_INT_RGB);
JLabel label = new JLabel(new ImageIcon(bufferedImage));
Graphics g = bufferedImage.getGraphics();
g.drawImage(image, 0, 0, null);
frame.getContentPane().add(label);
frame.setVisible(true);

}

public static void main(String args[]) {
ImageBufferingExample imageBufferingExample = new ImageBufferingExample();
}

public Image loadImage() {
return new ImageIcon("c:\\Sunset.gif").getImage();
}

}


Some concepts to see before explaining program :-

JFrame :- The JFrame class is slightly incompatible with Frame. Like all other JFC/Swing top-level containers, a JFrame contains a JRootPane as its only child. The content panec provided by the root pane should, as a rule, contain all the non-menu components displayed by the JFrame. This is different from the AWT Frame case. As a conveniance add and its variants, remove and setLayout have been overridden to forward to the contentPane as necessary. This means you can write:
       frame.add(child);

BufferedImage :- The BufferedImage subclass describes an Image  with an accessible buffer of image data. A BufferedImage is comprised of a Color Model and a Raster of image data. The number and types of bands in the Sample Model of the Raster must match the number and types required by the ColorModel to represent its color and alpha components. All BufferedImage objects have an upper left corner coordinate of (0, 0). Any Raster used to construct a BufferedImage must therefore have minX=0 and minY=0.
This class relies on the data fetching and setting methods of Raster, and on the color characterization methods of ColorModel.
public BufferedImage(int width, int height, int imageType)
JLabel :- A JLabel object can display either text, an image, or both. You can specify where in the label's display area the label's contents are aligned by setting the vertical and horizontal alignment. By default, labels are vertically centered in their display area. Text-only labels are leading edge aligned, by default; image-only labels are horizontally centered, by default.
You can also specify the position of the text relative to the image. By default, text is on the trailing edge of the image, with the text and image vertically aligned.
A label's leading and trailing edge are determined from the value of its Component Orientation  property. At present, the default Component Orientation setting maps the leading edge to left and the trailing edge to right.
Finally, you can use the setIconTextGap method to specify how many pixels should appear between the text and the image. The default is 4 pixels. 

Java.awt.Graphics :- The Graphics class is the abstract base class for all graphics contexts that allow an application to draw onto components that are realized on various devices, as well as onto off-screen images.
A Graphics object encapsulates state information needed for the basic rendering operations that Java supports. This state information includes the following properties:

  • The Component object on which to draw.
  • A translation origin for rendering and clipping coordinates.
  • The current clip.
  • The current color.
  • The current font.
  • The current logical pixel operation function (XOR or Paint).
  • The current XOR alternation color
Now coming to the actual program, BufferedImage has three arguments which includes height and width of image. The third argument is image Type.  I will explain each of them in detail at the end of this blog. Next create a JLabel which contains an BufferedImage. draw the image using graphics API and add them to content pane of JFrame. Make sure to Set Visibility to true to see the image on the screen. I believe the program is self explainatory.

Coming back to Image Types, They are :-
1) TYPE_3BYTE_BGR :- Represents an image with 8-bit RGB color components, corresponding to a Windows-style BGR color model) with the colors Blue, Green, and Red stored in 3 bytes. There is no alpha. The image has a ComponentColorModel. When data with non-opaque alpha is stored in an image of this type, the color data must be adjusted to a non-premultiplied form and the alpha discarded
2) TYPE_4BYTE_ABGR :- Represents an image with 8-bit RGBA color components with the colors Blue, Green, and Red stored in 3 bytes and 1 byte of alpha. The image has a ComponentColorModel with alpha. The color data in this image is considered not to be premultiplied with alpha. The byte data is interleaved in a single byte array in the order A, B, G, R from lower to higher byte addresses within each pixel.
3) TYPE_4BYTE_ABGR_PRE :- Represents an image with 8-bit RGBA color components with the colors Blue, Green, and Red stored in 3 bytes and 1 byte of alpha. The image has a ComponentColorModel with alpha. The color data in this image is considered to be premultiplied with alpha. The byte data is interleaved in a single byte array in the order A, B, G, R from lower to higher byte addresses within each pixel.
4) TYPE_BYTE_BINARY :- Represents an opaque byte-packed 1, 2, or 4 bit image. The image has an IndexColorModel without alpha. When this type is used as the imageType argument to the BufferedImage constructor that takes an imageType argument but no ColorModel argument, a 1-bit image is created with an IndexColorModel with two colors in the default sRGB ColorSpace: {0, 0, 0} and {255, 255, 255}.Images with 2 or 4 bits per pixel may be constructed via the BufferedImage constructor that takes a ColorModel argument by supplying a ColorModel with an appropriate map size.Images with 8 bits per pixel should use the image types TYPE_BYTE_INDEXED or TYPE_BYTE_GRAY depending on their ColorModel.When color data is stored in an image of this type, the closest color in the colormap is determined by the IndexColorModel and the resulting index is stored. Approximation and loss of alpha or color components can result, depending on the colors in the IndexColorModel colormap.
5) TYPE_BYTE_GRAY :- Represents a unsigned byte grayscale image, non-indexed. This image has a ComponentColorModel with a CS_GRAY colorspace. When data with non-opaque alpha is stored in an image of this type, the color data must be adjusted to a non-premultiplied form and the alpha discarded
6) TYPE_BYTE_INDEXED :- Represents an indexed byte image. When this type is used as the imageType argument to the BufferedImage constructor that takes an imageType argument but no ColorModel argument, an IndexColorModel is created with a 256-color 6/6/6 color cube palette with the rest of the colors from 216-255 populated by grayscale values in the default sRGB ColorSpace.When color data is stored in an image of this type, the closest color in the colormap is determined by the IndexColorModel and the resulting index is stored. Approximation and loss of alpha or color components can result, depending on the colors in the IndexColorModel colormap.
7) TYPE_CUSTOM :- Image type is not recognized so it must be a customized image. This type is only used as a return value for the getType() method.
8) TYPE_INT_ARGB :- Represents an image with 8-bit RGBA color components packed into integer pixels. The image has a DirectColorModel with alpha. The color data in this image is considered not to be premultiplied with alpha.
9) TYPE_INT_ARGB_PRE :-Represents an image with 8-bit RGBA color components packed into integer pixels. The image has a DirectColorModel with alpha. The color data in this image is considered to be premultiplied with alpha.
10) TYPE_INT_BGR :- Represents an image with 8-bit RGB color components, corresponding to a Windows- or Solaris- style BGR color model, with the colors Blue, Green, and Red packed into integer pixels. There is no alpha. The image has a DirectedColorModel. When data with non-opaque alpha is stored in an image of this type, the color data must be adjusted to a non-premultiplied form and the alpha discarded.
11)  TYPE_INT_RGB :-Represents an image with 8-bit RGB color components packed into integer pixels. The image has a  DirectedColorModel  without alpha. When data with non-opaque alpha is stored in an image of this type, the color data must be adjusted to a non-premultiplied form and the alpha discarded.
12) TYPE_USHORT_555_RGB :- Represents an image with 5-5-5 RGB color components (5-bits red, 5-bits green, 5-bits blue) with no alpha. This image has a DirectColorModel. When data with non-opaque alpha is stored in an image of this type, the color data must be adjusted to a non-premultiplied form and the alpha discarded.
13)TYPE_USHORT_565_RGB :- Represents an image with 5-6-5 RGB color components (5-bits red, 6-bits green, 5-bits blue) with no alpha. This image has a DirectColorModel. When data with non-opaque alpha is stored in an image of this type, the color data must be adjusted to a non-premultiplied form and the alpha discarded.
14) TYPE_USHORT_GRAY :- Represents an unsigned short grayscale image, non-indexed). This image has a ComponentColorModel with a CS_GRAY ColorSpace. When data with non-opaque alpha is stored in an image of this type, the color data must be adjusted to a non-premultiplied form and the alpha discarded.
15) BITMASK :- Represents image data that is guaranteed to be either completely opaque, with an alpha value of 1.0, or completely transparent, with an alpha value of 0.0.
16) OPAQUE :- Represents image data that is guaranteed to be completely opaque, meaning that all pixels have an alpha value of 1.0.
17) SCALE_AREA_AVERAGING :- Use the Area Averaging image scaling algorithm. The image object is free to substitute a different filter that performs the same algorithm yet integrates more efficiently into the image infrastructure supplied by the toolkit.
18) SCALE_FAST :-Choose an image-scaling algorithm that gives higher priority to scaling speed than smoothness of the scaled image
19) SCALE_REPLICATE :- Use the image scaling algorithm embodied in the ReplicateScaleFilter class. The Image object is free to substitute a different filter that performs the same algorithm yet integrates more efficiently into the imaging infrastructure supplied by the toolkit.
20) SCALE_SMOOTH :-Choose an image-scaling algorithm that gives higher priority to image smoothness than scaling speed.
21) TRANSLUCENT :- Represents image data that contains or might contain arbitrary alpha values between and including 0.0 and 1.0.
22) TRANSLUCENT :- Represents image data that contains or might contain arbitrary alpha values between and including 0.0 and 1.0.
Output Image :-  


    Comments

    Popular posts from this blog

    Mini Project for Image Processing Beginners

    Program to convert image byte array to PDF

    Program to convert base64 to Image and vice versa