Image file formats - Department of Computer

Java Example
Object-Oriented Design
•Similar object classes
•Common interfaces
•Common usage
•Code reuse-- inheritance
•Defers implementation
and algorithm decisions
Procedural Design
•Algorithm centered-•forces early implementation
and algorithm decisions
•Exposes more details
•Difficult to extend
•Difficult to maintain
Polymorphism
void processShape(Shape s) {
s.setColor(Color.red);
// ... do stuff
s.draw(); }
Circle c = new Circle(x, y, r);
Point2D v1 = new Point2D(x1, y1);
Point2D v2 = new Point2D(x2, y2);
Point2D v3 = new Point2D(x3, y3);
Triangle t = new Triangle(v1, v2, v3);
Point2D c1 = new Point2D(u1, v1);
Point2D c2 = new Point2D(u2, v2);
Triangle r = new Rectangle(c1, c2);
processShape(c);
processShape(t);
processShape(r);
Variables in Java must be objects, arrays,
or one of the primitive data types
Java has 8 primitive data types:
(boolean, byte, short, int, long, double, float, char)
Notes:
•All integer types are signed (and there is no unsigned keyword)
•Unlike C and C++ char variables are 16 rather than 8 bits
•Primitive data types are passed by value
Java has 8 primitive data types
boolean like6837 = true;
{true, false}
// boolean likewise have values
byte teeth = 32;
// bytes go from -128 to 127
if (!like6837) teeth = teeth - 1;
float pi = 3.141592f;
short classes = 0xbad;
// you need the "f" here
// hexadecimal
Java Arrays
Java arrays fall between a primitive data type and an object
Properties of Java arrays:
•can contain objects, primitive types, or other arrays
•contents are accessed via zero-based positive integers enclosed within the brackets, [,
and ]
•declaration and creation of arrays are distinct operations
•created with the new operator
•uninitialized arrays have a value of null
•have a single instance variable called length
•checks at run time, that all accesses are between 0 and length - 1
•passed to methods by reference
Java Arrays
byte buffer[];
// array declaration (buffer = null)
buffer = new byte[1024];
to zeros)
// array creation (contents initialized
int table[] = new int[10];
// declaration and creation combined
int sqrs[] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81 }; // with an initializer
buffer[5] = sqrs[sqrs.length-1]; // array references
int triangle[][] = new int[3][];
triangle[0] = new int[3];
triangle[1] = new int[2];
triangle[2] = new int[1];
Java Objects
Properties of Classes
•All Java code is contained within objects
•Objects are passed to methods by reference
•Definition, declaration, and creation of objects are distinct
•Instances of objects are created with new and have a value of null
•Objects have four forms: the class, the abstract class, the inner
class, and the interface
Java Objects
class Circle {
// object definition
static double pi = 3.141592;
// class variable
double radius; // instance variable
public Circle(double r) {
// constructor method
radius = r;
}
public double circumference() { return 2*pi*radius; }
public double area() { return pi*radius*radius; } }
Circle c;
// object declaration
c = new Circle(4.0);
// object creation
double a = c.area();
Java 2 incorporates the Java2D API ( Application
Programming Interface) as part of the standard set of
APIs
The Java2D API consists of a number of classes, distributed
amongst the old packages: java.awt and java.awt.image and six
new packages:java.awt.color, java.awt.font, java.awt.geom,
java.awt.print, java.awt.image.renderable and
com.sun.image.codec.jpeg
Packages
java.awt.image Provides classes for creating and modifying images.
http://java.sun.com/products/jdk/1.2/docs/api/java/awt/image/package
-summary.html
com.sun.image.codec.jpegProvides classes for encoding and
decoding JPEG images.
The BufferedImage class
java.awt.image
Class BufferedImage
java.lang.Object
|
+--java.awt.Image
|
+--java.awt.image.BufferedImage
BufferedImage class supplies functionality to do immediate
mode processing of image data
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_BYTE_GRAY);
The BufferedImage class
- BufferedImage is a subclass of Image, and is in the
java.awt.image package
- BufferedImage objects have a Raster and a ColorModel
-Several types of image formats are provided
•BufferedImage.TYPE_BYTE_GRAY - 8 bits sample per
pixel
•BufferedImage.TYPE_INT_RGB – 8 –bit red, green and
blue samples, packed into an int .
•BufferedImage.TYPE_3BYTE_BGR - 8 bits each of
RGB
The BufferedImage class
- Methods are provided to getWidth( ), getHeight( ),
and getType( )
- Access to pixels is provided through getRGB( )
and setRGB( )
these are typically inefficient for DIP purposes
because they are packed and represented
according to the ColorModel
The BufferedImage class
-The Raster object has a DataBuffer and a SampleModel
- A Raster object is read-only, WriteableRaster allows you to modify
image data
WritableRaster raster = image.getRaster();
- allows access to samples without the overhead of the ColorModel
- many variations on getPixel( ), getSample( ), setPixel( ), setSample( )
raster.setSample(x, y, band, value);
-sets the sample at position x,y to value
- band is 0 for grayscale images
-band is 0, 1, or 2 for RBG images
Image file formats
When placing images into online or archival storage, it is important to
select an appropriate file format .
Magic
Number
bytes
Image data
header
Image file formats specify some kind of information and consists of a
header segment and data segment.
The headers begin with a signature or “magic number” – a short
sequence of bytes designed to identify the file as an image with that
specific format.
The header will contain also, the width and the height of the image.
Image File formats can be grouped
roughly into three categories :
• Device-specialized formats.
The structure of the image file may be chosen to facilitate rapid
display on a particular type of workstation. The disadvantages of
device specialized formats include their lack of portability, their
inefficiency when used with other hardware.Example: ICC - Used
for Kodak printer
• Software-specialized formats
Are those designed by a software vendor to be used with a
particular program or class of programs .Examples include the PCX
and Windwos bitmap (BMP) formats commonly found on PCs, or
the MacPaint format used on Aplet Macintosh computers.
• Interchange formats : GIF, PNG, JPEC
Graphics Formats for the World Wide Web
http://www.wdvl.com/Graphics/Formats/
There are several kinds of graphics files:•Raster files (also called bitmapped files) contain graphics information represented as
pixels, such as photographic images. These include GIF, PNG, and JPEG.
•Animation formats are usually sequences of raster data.
•Vector files contain data for mathematical operations and are typically used to store
line art and CAD information.
•Metafiles may contain either raster or vector graphics data.
•Page Description Languages (PDL) are used to describe the layout of a printed page
of graphics and text, e.g. Adobe PDF.
•Multi-dimensional object formats store graphics data as a collection of objects (data
and the code that manipulates it) that may be rendered as if seen from any viewpoint.
•Virtual Reality Modeling Language (VRML) is a 3D, object-oriented language used
for describing "virtual worlds" hyperlinked within the World Wide Web.
•Multimedia file formats are capable of storing any of the above types of data, often
including sound and/or video information.
2D Bitmap Specifications:
http://www.dcs.ed.ac.uk/home/mxr/gfx/
BIFF - Notes on the XITE 3D file format
BMP - MS-Windows bitmap format
BW - SGI Black & White Image File Format
CGM - Computer Graphics Metafile
CGM Overview & Description
CGM Technical Reference (WordPerfect 5.1)
CGM Information, Examples, and Source Code [Link]
DRAW - Acorn's object-based vector image file format [Link]
DWG - AutoCAD drawings file format information [Link]
FAX - The Group 3 Facsimile standard
Examples of common interchange formats
are:
- GIF - Graphics Interchange Format
- PNG - Portable Network Graphics
- JFIF - JPEG (Joint Photographic Experts Group) File
Interchange Format
- TIFF - Tagged Image File Format
- PGM - Portable Gray Map
-PBM - Portable Bit Map
-PPM - Portable Pix Map
- FITS - Flexible Image Transport System
interchange formats - type of compression
used is important
lossless - no image information is lost during
compression (PNG)
lossy - information is lost during compression (JPEG)
Generally speaking, a distinction can be made between methods
that compress without loss (lossless), and thus do not eliminate
any image details, and those that reduce the file size at the
expense of image information (lossy). The reference photo for the
versions compressed in different ways shows colorful fuchsia
flowers saved in the lossless PNG format.
Lossless compression methods
The original image can be restored in absolutely identical form
at any time – after decompression, every pixel has exactly the
values it had before compression
The proprietary formats of
image editing programs – such
as the *.PSD format in Adobe
Photoshop – are often equipped
with lossless compression
algorithms .
Lossless compression methods
The so-called TIFF format (Tagged Image File
Format) uses a lossless compression method
known as the Lemple-Zif-Welch (LZW)
method. However, this version, which good
image editing programs offer as an option when
saving, is only capable of significantly reducing
the file size of images containing a relatively
large proportion of plain-colored areas. With
our photo of the fuchsia flowers, it only reduces
the memory space requirement by 3 percent.
Picmaster is one example of a shareware program that offers LZW
compression for the TIFF format.
Lossless ZIP compression method
Another lossless method is ZIP
compression, which can be
applied to any text, program and
other files, and thus also to
photos. When using a standard
ZIP compression program, such
as PowerArchiver shareware, it
reduces the file size by as much
as 16 percent in the case of our
fuchsia photo. However, this
factor is far lower when applied
to previously compressed image
files.
Lossy image compression methods
JPEG is the image compression standard developed by the Joint
Photographic Experts Group. It works best on natural images (scenes). It
compresses the red-green-blue parts of a color image as three separate
greyscale images - each compressed to a different extent, if desired.
All the image details can be
recognized precisely in the
original image with a file size of
1.1 megabytes in TIFF format.
So that no image information is
lost, it is also available in PNG
format for online comparison.
The images competing with this
reference photo were saved in
JPEG format with 100, 80, 60,
40, 20 and 0 percent quality
preservation.
JPEG method
In the image compressed by the JPEG
method with 100 percent quality
preservation, you can only see the
differences compared to the reference after
taking a fairly close look. Yet, this level
already allows you to reduce the file size by
84 percent.
The differences are still within reasonable
limits if only 80 percent of the quality is
preserved. However, you can see the
regular structures caused by the grouping
of the pixels. The additional elimination of
detail information already results in hefty
memory savings of 94 percent
JPEG method
Compression with 60 percent quality preservation
makes the signs of block-by-block color
equalization more obvious. In this case, the color
of some of the pixel blocks does not match the
surroundings. In return, this version of the image
requires 95.2 percent less memory space.
The first noticeable "staircases" become visible at
40 percent quality preservation. The retention of
the differences in brightness is obviously no
longer capable of concealing them. It is perfectly
clear that saving this amount of memory space is
too much of a good thing, particularly as it doesn't
leave you with significantly more available
memory.
JPEG method
If you preserve only 20 percent of the
original quality, the tile-like structure
of the pixel block can no longer be
overlooked. In the algorithm, block
formation has clearly gained the upper
hand over the attempts to conceal it by
preserving differences in brightness.
If you totally dispense with quality
preservation, the tile structure
breaks up the image in a manner
reminiscent of abstract painting.
Now, you consistently get extreme
color differences between adjacent
pixel blocks.
How JPEG works
JPEG divides up the image into 8 by 8 pixel blocks, and then calculates
the discrete cosine transform (DCT) of each block. A quantize rounds
off the DCT coefficients according to the quantization matrix. This step
produces the "lossy" nature of JPEG, but allows for large compression
ratios. JPEG's compression techniqueuses a variable length code on
these coefficients, and then writes the compressed data stream to an
output file (*.jpg). For decompression, JPEG recovers the quantized
DCT coefficients from the compressed data stream, takes the inverse
transforms and displays the image .
JPEG Tutorial
Index of Hyperlinks
An anonymous FTP site for more JPEG documentation is:
ftp.uu.net/graphics/jpeg/
http://dynamo.ecn.purdue.edu/~ace/jpeg-tut/jpegtut1.html
GIF – Special format for the Web
The GIF format is a good candidate for special uses or
certain images that consist of large, uniform areas of color.
Although it works with the lossless LZW method, it limits
the number of colors to a maximum of 256, this being
equivalent to 8 bits (2 to the power of 8 equals 256) in the
binary system.
You should generally avoid any software that only offers a
command in the style of "Save As GIF" without any further
options. After all, the GIF format has numerous options and
settings that you should make good use of in order to obtain
optimum results.
GIF format has numerous options
•Interlaced: When you use this option, a GIF graphic builds up in
the browser in several steps, from "coarse" to "fine". In other
words: the surfer rapidly gets a rough impression of the contents of
the image, which is then displayed more accurately with each pass.
•Dither: Here, you can choose whether the image areas are to be
displayed simply in the forms of areas of color, or whether fine
dots within the areas are to simulate other shades. The dither
methods often offer additional sub-options.
•Palette: This option allows you to specify the makeup of the
color palette of the image and how many colors it is to include.
•Transparenz: This is one of the major advantages of the GIF
format for use on the Internet. You can set certain colors or the
entire background of an image to transparent. For instance, if you
position a graphic of this kind on top of a background image or in
a colored table row, these are visible outside the motif contours.
GIF format
Despite all the options that GIF offers, the consequence of this
compression method based on reducing the number of colors is
that usually substantial color falsification understandably results
in halftone images, which always contain color shades. This can
be seen in the following examples with 256, 128 and 64 colors.
SmartSaver from Ulead converts
images into these given color levels,
showing you not only the result in the
preview window, but also the color
palette used.
GIF format
Reducing the number of colors to 128
intensifies the screened effect and
increases the number of isolated pixels
of a different color. The file size
shrinks to 83 percent compared to the
original image.
Even further reduction to just 64 colors results
in a screening effect that is reminiscent of the
pop art paintings of Roy Lichtenstein. While
the distortion of the colors is already very
marked in comparison with the JPEG method,
the memory-saving effect is only moderate,
shrinking the file size by 87 percent
GIF - Additional information
•Unisys emphasizes on its website
(http://www.unisys.com/unisys/lzw/) the fact that
program manufacturers need a license to use the GIF
format.
Mark Nelson has compiled interesting link
(http://dogma.net/DataCompression/GIF.shtml) on
the GIF format.
Which compression method and when?
Even before taking the photo, and also before saving the image data
again on the computer, you should have a clear idea of what the
images are to be used for.
TIFF
The TIFF format preserves all the image information during saving,
making it the method of choice when large-format photos or
posters are to be printed, or if you are planning on making selective
enlargements, for instance because you don't have or can't use a
telephoto lens when taking the shot. Lossless LZW compression is
a good choice for image editing, while the ZIP format will
additionally reduce the size of the file if you want to send it off in
an e-mail.
Which compression method and when?
JPEG
With the JPEG format and a powerful compression program or image
editing software, you have every opportunity to compress an image
for virtually any conceivable purpose. However, you always have to
keep an eye on the reciprocal effect of file size and accuracy of detail.
If you compress too much too soon, without retaining the original file,
you will irretrievably lose the data.
GIF
The GIF format is ideal for compressing graphics and graphic-like
images characterized by sharply defined, relatively uniform areas of
color, like some comics. It is always suitable for halftone images if
you want not only a high degree of compression, but also plan to
integrate text into the photos or animate the images for use on the
Internet. The GIF format is usually also superior to the JPEG format
in terms of quality and file size when it comes to navigation buttons
on websites or graphic logos.
What do you compress with?
One very
convenient
compression
program is
SmartSaver Pro
from Ulead. It
allows you to enter
the exact quality
level you want and
check the result on
the basis of a
preview window.
What do you compress with?
Of course, the common, standard
image editing programs, from
Adobe Photoshop and Paint
Shop Pro to Ulead PhotoImpact
and MGI PhotoSuite, offer very
exact setting options. When you
use the "Save As…" command in
Adobe Photoshop, the standard
program for image editing, you
can select the compression level
using a slide control, a numerical
scale from 1 to 10 or four levels
from low to maximum.
In Paint Shop Pro, the degree of
compression is set in the "Save As"
command either by entering a
compression factor between 1 and
99 or via a slide control permitting
infinitely variable adjustment
between maximum and minimum
quality
The "Export JPEG File" command in
Paint Shop Pro additionally offers a
preview window for judging the image
quality.
Freeware and Shareware programs that
use the JPEG format
•ACDSee opens any JPEG file.
(http://www.acdsystems.com/English/Products/ImagingProducts/ACD
See/index.htm)
•IrfanView for viewing, converting and compressing.(
http://irfanview.tuwien.ac.at/deutsch.htm)
•XATJPEG(http://www.xat.com/jpeg_optimizer/jpeg_optimizer.shtml
) Optimizer makes JPEG files particularly small and can compress
certain areas of the image less than others.
•Jpeg Cleaner removes supplementary information to produce even
smaller files. (http://www.pppr.sk/rainbow/)
To Jpeg is for conversion to JPEG format at a click of the mouse
under MacOS (http://jsam.best.vwh.net/).
Interchange formats
1. Simple formats have a fixed size header, followed by data (PBM,
PGM, PPM)
The various pieces of information needed to process an image are
located at fixed offsets from the start of the file. For example , if the
magic number occupies four bytes , then bytes 5-8 and 9-12 might be
used to store image width and height, respectively.
2. Tagged formats use specific codes to tag various types of information
in the file (PNG, TIFF)
Tagged formats do not require that information at fixed locations in
an image file; instead they make use of special strings or codes to
identify particular items of data. Example :FITS , which uses ASCII
strings as tags; PNG , which uses four-byte sequences of uppercase
or lowercase characters ; and TIFF, which uses numeric codes.
PGM - portable graymap file format
Signature of the various PBM, PGM and PPM image format
Signature
Image type
Storage image
P1
Binary
ASCII
P2
Greyscale
ASCII
P3
RGB
ASCII
P4
Binary
Raw bytes
P5
Greyscale
Raw bytes
P6
RGB
Raw bytes
A ASCII PGM files- file's magic number is the two characters "P2".
In Raw PGM files - the "magic number" is "P5" instead of "P2".
ASCII PGM - DESCRIPTION
- A ASCII pgm
file's magic number is the two characters "P2".
- White space (blanks, TABs, CRs, LFs).
-A width,formatted as ASCII characters in decimal.
- White space.
- - A height, again in ASCII decimal.
- - Whitespace.
- - The maximum gray
-
value, again in
ASCII decimal.
- Whitespace.
-- Width * height gray values, each
in ASCII decimal, between
0 and the specified
maximum
value,
separated by whites pace,
starting at the
top-left corner
of the
graymap,
proceeding in normal English reading order. A value of
0
means black, and
the maximum value means white. In an ASCII file
color of each pixel is stored as a triplet of ASCII decimal values ( in the order red, green,
blue)
-- Characters from a "#" to
-
the next end-of-line are ignored
- No line should be longer
than 70
characters.
(comments).
Here is an example of a small graymap
in ASCII PGM format:
P2
# a simple PGM image
7 7 255
120
120
120
12
120
120
120
120
120
120
33
120
120
120
120
120
120
33
120
120
120
120
33
120
33
33
33
120
120
120
120
33
120
120
120
120
120
120
33
120
120
120
120
120
120
120
120
120
120
Raw PGM file
There is also a variant on
the format, available by setting
the RAWBITS option at compile time. This variant is different
in the following ways:
- The "magic number" is "P5" instead of "P2".
- The gray
decimal.
values are stored as plain bytes, instead of
ASCII
- No white space is allowed
in the grays section, and only a
single character of whitespace (typically a newline) is
allowed
after the maxval.
- The files are smaller and many times faster to read and
write.
- Note that this raw format can only
be used
for
maxvals less than or equal to 255. If you use the pgm library and
try to write a file with a larger
maxval, it will automatically
fall back on the slower but more general plain format.
The main advantage of the ASCII format is that pixel
values can be examined or modified very easily using a
standard text editor.
The advantage of the raw format is that it is much more
compact; in a Raw PGM file , pixel value are coded
using only a single character, but in an ASCII PGM file,
as many as four characters may be required for each
pixel value – with the result that ASCII files consume up
to four times as much space as raw files.
PNG – The transparent alternative
PNG (Portable Network Graphics), an extensible file format for the
lossless, portable, well-compressed storage of raster images. PNG
provides a patent-free replacement for GIF and can also replace many
common uses of TIFF. Indexed-color, grayscale, and truecolor images
are supported, plus an optional alpha channel. Sample depths range
from 1 to 16 bits.
More information on the PNG format
•The W3C website contains everything you could possibly want to know
about the PNG format.( http://www.w3.org/TR/REC-png.html)
•Group42 champions the cause of the PNG format.
(http://www.group42.com/png.htm)
GIF features retained in PNG include:
•Indexed-color images of up to 256 colors.
•Streamability: files can be read and written serially, thus allowing
the file format to be used as a communications protocol for on-the-fly
generation and display of images.
•Progressive display: a suitably prepared image file can be displayed
as it is received over a communications link, yielding a lowresolution image very quickly followed by gradual improvement of
detail.
•Transparency: portions of the image can be marked as transparent,
creating the effect of a non-rectangular image.
•Ancillary information: textual comments and other data can be
stored within the image file.
•Complete hardware and platform independence.
Effective, 100% lossless compression.
Important new features of PNG, not
available in GIF, include:
•Truecolor images of up to 48 bits per pixel.
•Grayscale images of up to 16 bits per pixel.
•Full alpha channel (general transparency masks).
•Image gamma information, which supports automatic
display of images with correct brightness/contrast
regardless of the machines used to originate and
display the image.
•Reliable, straightforward detection of file corruption.
•Faster initial presentation in progressive display
mode.
PNG is designed to be:
•Simple and portable: developers should be able to implement PNG
easily.
•Legally unencumbered: to the best knowledge of the PNG authors, no
algorithms under legal challenge are used. (Some considerable effort
has been spent to verify this.)
•Well compressed: both indexed-color and truecolor images are
compressed as effectively as in any other widely used lossless format,
and in most cases more effectively.
•Interchangeable: any standard-conforming PNG decoder must read all
conforming PNG files.
•Flexible: the format allows for future extensions and private add-ons,
without compromising interchangeability of basic PNG.
Robust: the design supports full file integrity checking as well as
simple, quick detection of common transmission errors.
PNG File Structure
A PNG file consists of an eight-byte signature, followed by a
series of chunks. Each chunk consists of : a 32 –bit integer
giving the number of bytes in the chunk’s data field; a fourbyte code to indicate chunk type, zero or more data bytes; and
32 –bit cyclic redundancy check ( CRC ) for the chunk, which
a PNG decoder can use to test whether the chunk data are
valid.
The first eight bytes of a PNG file always contain the
following (decimal) values:
137 80 78 71 13 10 26 10This signature indicates that the
remainder of the file contains a single PNG image,
consisting of a series of chunks beginning with an IHDR
chunk and ending with an IEND chunk
Chunk layout
Each chunk consists of four parts:
Length
A 4-byte unsigned integer giving the number of bytes in the chunk's
data field. The length counts only the data field, not itself, the chunk
type code, or the CRC. Zero is a valid length.
Chunk Type
A 4-byte chunk type code. For convenience in description and in
examining PNG files, type codes are restricted to consist of uppercase
and lowercase ASCII letters (A-Z and a-z, or 65-90 and 97-122
decimal). However, encoders and decoders must treat the codes as
fixed binary values, not character strings. For example, it would not
be correct to represent the type code IDAT by the EBCDIC
equivalents of those letters. Additional naming conventions for chunk
types are discussed in the next section.
Chunk layout
Chunk Data
The data bytes appropriate to the chunk type, if any. This field
can be of zero length.
CRC
A 4-byte CRC (Cyclic Redundancy Check) calculated on the
preceding bytes in the chunk, including the chunk type code and
chunk data fields, but not including the length field. The CRC is
always present, even for chunks containing no data.
Display of images
See Displaying images in
Java.ppt