Lecture 7 Basic Images Manipulation TK 3813 Contents
Lecture 7: Basic Images Manipulation TK 3813
Contents ¡ ¡ 12/17/2021 Storage Reading and writing images in Java Display Printing TK 3813 2
Storage ¡ Short-term: l l 12/17/2021 Memory-based which is fast, but is normally volation (no longer available when there is no power supply). E. g. RAM and dedicated memory of ‘framestore’ (specialised hardware for rapid access). TK 3813 3
Storage ¡ On-line storage: l l l 12/17/2021 Non-volatile. Allows relatively rapid access to image data. E. g. computer hard disk. TK 3813 4
Storage ¡ Archival storage: l l 12/17/2021 Used for long-term archiving of image data. Typically employs removable media, e. g. CDs. TK 3813 5
File Formats ¡ Image files are stored in various formats depending upon the requirements l l 12/17/2021 Images to be “pushed” to a web viewer may have limited palettes or interlaced pixels Images for medical archiving need accuracy Images for use on many platforms require a high degree of portability Images for use only in a specific application may be optimized for that application TK 3813 6
File Formats ¡ ¡ Detremine how the image data and additional information are stored. Typical format involves a header followed by the pixel data. Magic Number Header Pixel Data (in some format … perhaps compressed) • Magic Number – a unique identifier that identifies the file format • Header – miscellaneous information about the image. Image size, palette, compression method, etc… • Data – the pixel data in some format 12/17/2021 TK 3813 7
File Formats Typical format involves a header followed by the pixel data Magic Number 12/17/2021 Header Pixel Data (in some format … perhaps compressed) PNG Ox. D 3 PNG GIF 89 a JPEG Ox. FFD 8 FFE 0 JFIF PPM ASCI P 3 BMP BM TK 3813 Signature (magic number) 8
File Formats ¡ ¡ ¡ ¡ ¡ BMP – Windows Bit. Ma. P MACPAINT – Macintosh Paint format PPM – Portable Pixmap (PBM, PGM, PPM) GIF – Graphics Interchange Format PNG – Generic replacement for GIF JFIF – JPEG File Interchange Format FITS – Flexible Image Transport System ICO – ICOn resource file JPEG – Joint Photographic Experts Group PCX – none @ PC Paintbrush file format GIF and JPEG : the lingua franca for images on the World-Wide Web. 12/17/2021 TK 3813 9
File Formats ¡ 3 categories of file format: l l 12/17/2021 Device-specialised formats. ¡ Tailored for specific computer hardware. ¡ May be chosen for rapid display on a particular computer. ¡ Disadvantage: lack of portability. Software-specific formats. ¡ Tailored for particular software. ¡ E. g. Windows bitmap (BMP) formats commonly found on PC and the Mac. Paint format used on Apple Macintosh computer. TK 3813 10
File Formats l Interchange formats. ¡ Facilitate the exchange of image data between users, via removable storage media or computer networks. ¡ Usable for the widest possible range of hardware and software. ¡ Image compression is often a standard feature of interchange formats, since it reduces storage requirements and transmission times. ¡ Example: l l l 12/17/2021 GIF (Graphic Interchange Format) PNG (Portable Network Graphics) JFIF (JPEG File Interchange Format ) TIFF (Tagged Image File Format) PGM (Portable Grey Map) FITS (Flexible Image Transport System) TK 3813 11
GIF History In 1987, Compu. Serve designed the Graphics Interchange Format (GIF) specification for graphics files. The GIF specification incorporated the Lempel-Zev-Welch (LZW) compression technology on which Unisys Corporation was independently pursuing a patent filing. In early 1993, Unisys notified Compu. Serve of patent rights granted to LZW. At that time, Compu. Serve began negotiating with Unisys to secure a licensing agreement. This agreement was reached in mid-1994, and Compu. Serve then initiated a process to secure a similar license that would benefit its GIF developer community. Following the agreement reached between Compu. Serve and Unisys, Compu. Serve announced the Graphics Interchange Format (GIF) Developer Agreement, shortly after its completion, on December 29, 1994. This agreement is aimed at GIF developers who are developing programs and shareware primarily for use in conjunction with Compu. Serve. The service offers a license to these developers to use LZW technology in programs written to the GIF specification. The agreement offers software and shareware developers who use the LZW technology in their GIF programs protection under a software license that Compu. Serve is authorized to grant under the agreement with Unisys. Developers who choose to take advantage of this service would acquire the rights to use the LZW technology in certain software and shareware developed primarily for use in conjunction with Compu. Serve. Developers who choose to participate in this agreement within the implementation period will also benefit in that Unisys has agreed not to pursue royalty claims for past use of the LZW technology in GIF. The implementation period has been extended to January 31, 1995. Compu. Serve has presented this new agreement as a service to its GIF developer community. Cost to developers will be a $1. 00 one-time licensing fee and a royalty payment of 1. 5 percent or $0. 15, whichever is greater, per registered copy of a program containing the LZW technology. Compu. Serve will not profit from this service. Compu. Serve encourages developers to work with Unisys directly if the GIF Developer Agreement does not meet their needs. Unisys is continuing to make the LZW technology available to any interested parties under reasonable and non-discriminatory terms. Developers are not required to register with Compu. Serve. Registering with Compu. Serve is simply one option for addressing the Unisys LZW patent issue. Developers may want to consider consulting with legal counsel. 12/17/2021 TK 3813 12
PNG History PNG (pronounced ``ping'') is the Portable Network Graphics format, a format for storing bitmapped (raster) images on computers. Unofficially its acronym stands for ``PNG's Not GIF. '' PNG was designed to be the successor to the once-popular GIF format, which became decidedly less popular right around New Year's Day 1995 when Unisys and Compu. Serve suddenly announced that programs implementing GIF would require royalties, because of Unisys' patent on the LZW compression method used in GIF. Since GIF had been showing its age in a number of ways even prior to that, the announcement only catalyzed the development of a new and much-improved replacement format. PNG is the result. PNG Features: ¡multiple CRCs so that file integrity can be checked without viewing ¡ultra-clever magic signature that can detect the most common types of file corruption ¡better compression than GIF, typically 5% to 25% (but often 40% or 50% better on tiny images) ¡non-patented (you betcha!), completely lossless compression ¡majorly gnarly two-dimensional interlacing scheme ¡ 1 -, 2 -, 4 - and 8 -bit palette support (like GIF) ¡ 1 -, 2 -, 4 -, 8 - and 16 -bit grayscale support ¡ 8 - and 16 -bit-per-sample (that is, 24 - and 48 -bit) truecolor support ¡full alpha transparency in 8 - and 16 -bit modes, not just simple on-off transparency like GIF ¡``palette-alpha'' mode, effectively transforming normal RGB palette into RGBA ¡gamma correction for cross-platform ``brightness'' control ¡color correction for cross-platform, precision color ¡both compressed and uncompressed text chunks for copyright and other info ¡full Year 2000 (Y 2 K) support, and then some (good for at least 63 millenia! yowza!) ¡free and complete reference implementation with full source code 12/17/2021 TK 3813 13
PBM, PGM, PPM 12/17/2021 TK 3813 14
PBM, PGM, PPM These formats are simple and easy to encode/decode. The ASCII variants are ideal for image transmission. 12/17/2021 Magic Number Image Type Storage P 1 binary ASCII P 2 grayscale ASCII P 3 RGB ASCII P 4 binary native (hex) P 5 grayscale native (hex) P 6 RGB native (hex) TK 3813 15
PGM (Portable Grey Map) Format ¡ ¡ 12/17/2021 The Portable Greymap format is a common greyscale file format on Unix systems. The file consists of an ASCII header followed by binary data values held as unsigned binary bytes. The header may optionally contain comment lines interspersed anywhere between the "magic number" line and the line containing the maximum grey value. Comments lines are preceeded by the hash '#' character, which must appear as the first character. The maximum length of a comment line (including the hash) is 70 characters. TK 3813 16
PGM ASCII Format 7 pixels magic number comment Width, height max_value pixel values 12/17/2021 P 2 # small 7 7 255 120 120 33 120 120 120 little image 120 120 33 120 120 120 33 33 33 120 120 120 120 TK 3813 17
PBM (Portable Bit. Map) File Format ¡ 12/17/2021 PBM stores single bit pixel image as a series of ascii "0" or "1"'s. Traditionally "0" refers to white while "1" refers to black. The header is identical to PPM and PGM format except there is no third header line (the maximum pixel value doesn't have any meaning. The magic identifier for PBM is "P 1". TK 3813 18
PGM(Portable Gray. Map) File Format ¡ 12/17/2021 This format is identical to the above except it stores greyscale information, that is, one value per pixel instead of 3 (r, g, b). The only difference in the header section is the magic identifiers which are "P 2" and "P 5", these correspond to the ascii and binary form of the data respectively. TK 3813 19
PPM (Portable Pix. Map) Format ¡ 12/17/2021 A PPM file consists of two parts, a header and the image data. The first "line" is a magic PPM identifier, it can be "P 3" or "P 6" (not including the double quotes!). The next line consists of the width and height of the image as ascii numbers. The last part of the header gives the maximum value of the colour components for the pixels, this allows the format to describe more than single byte (0. . 255) colour values. In addition to the above required lines, a comment can be placed anywhere with a "#" character, the comment extends to the end of the line. TK 3813 20
BMP (Bit. Ma. P) File Format ¡ ¡ ¡ 12/17/2021 BMP is a standard file format for computers running the Windows operating system. The format was developed by Microsoft for storing bitmap files in a device-independent bitmap (DIB) format that will allow Windows to display the bitmap on any type of display device. The term "device independent" means that the bitmap specifies pixel color in a form independent of the method used by a display to represent color. TK 3813 21
BMP (Bit. Ma. P) File Format ¡ ¡ 12/17/2021 User l M$ Windows 3. 0 and higher Signature l First two bytes, must be BM, in ASCII or Hex (42 4 D) Motivation l To store raster (bitmap) image data in a format that is independent of the color specification scheme used on any single hardware device It may use compression (though I never came across a compressed. bmp-file) and is (by itself) not capable of storing animation. TK 3813 22
BMP (Bit. Ma. P) File Format ¡ . bmp file contains of the following data structures: l BITMAPFILEHEADER ¡ l l contains a color table. BYTE ¡ 12/17/2021 information about the bitmap such as size, colors, . . . Bitmap Info RGBQUAD ¡ l some information about the bitmap file (about the file, not about the bitmap itself). BITMAPINFOHEADER ¡ Bitmap File Header Bitmap Data the image data, which format is specified by the bmih structure. TK 3813 23
Exact BMP file Structure ¡ ¡ 12/17/2021 The following tables give exact information about the data structures and also contain the settings for a bitmap with the following dimensions: size 100 x 100, 256 colors, no compression. The start-value is the position of the byte in the file at which the explained data element of the structure starts, the size-value contains the number of bytes used by this data element, the name-value is the name assigned to this data element by the Microsoft API documentation. Stdvalue stands for standard value. There actually is no such a thing as a standard value but this is the value Paint assigns to the data element if using the bitmap dimensions specified above (100 x 256). The meaning-column gives a short explanation of the purpose of this data element. TK 3813 24
Bitmap File Header 12/17/2021 TK 3813 25
Bitmap Information Valid choices are 1, 4, 8, 24 12/17/2021 TK 3813 26
Bitmap Information Note that bi. Bit. Count actually specifies the color resolution of the bitmap. The possible values are: 1 (black/white); 4 (16 colors); 8 (256 colors); 24 (16. 7 million colors). Valid choices are 1, 4, 8, 24 12/17/2021 TK 3813 27
Bitmap Data It depens on the BITMAPINFOHEADER structure how the pixel data is to be interpreted. The rows of a BMP image are stored upside down. That means that the uppest row which appears on the screen actually is the lowest row stored in the bitmap, a short example: 12/17/2021 TK 3813 28
BMP Example: 12/17/2021 TK 3813 29
Image Compression ¡ Aim: to reduce the amount of data needed to represent an image – can be stored or transmitted more efficiently. l l ¡ Example: 225 x 300 image: l l l 12/17/2021 Compression – lossy and lossless- to reduce redundant information. Colour palettes to reduce the amount of data at each pixel. PPM (Portable Pixmap): raw data requires 198 KB. GIF : 40 KB. JPEG : range from 41 to 6 KB (depending on quality). TK 3813 30
Image Compression ¡ Compression factors: l l l 12/17/2021 Transmission and storing of large image. Reduce of baud rate. Reduce of transmission time. TK 3813 31
Image Compression ¡ Data vs information: l l Data are the means by which information is conveyed. Various type of data can be used to convey the same information. 32 bits “five” 101, 632 bits 8 bits “ 5” 1 0 1 3 bits Four different representation of the same information. 12/17/2021 TK 3813 32
Redundancy ¡ ¡ ¡ More data are used than is strictly necessary. Data compression is achieved when one or more redundancies are reduced or eliminated. 3 basic type of redundancy: l 12/17/2021 Coding redundancy: ¡ The representation chosen for the pixel values of an image is not the most efficient that is possible. ¡ E. g. 8 -bit coding scheme has the capacity to represent 256 distinct level of intensity in an image, but to represent 16 different grey levels, for example, we actually need 4 -bit only. TK 3813 33
Redundancy l l 12/17/2021 Interpixel redundancy: ¡ The values of a pixel and its immediate neighbours are often strongly correlated, i. e. the value at a pixel can be predicted from values of its neighbours. Psychovisual redundancy: ¡ Not all the information present in an image is of equal importance to the human visual system. ¡ This information is said to be psychovisually redundant, which can be eliminated without significantly imparing the quality of image perception. ¡ E. g. there are upper limits on the number of quantisation levels and spatial frequency that can be detected by eyes. TK 3813 34
Redundancy l 12/17/2021 For video data, we also have interframe redundancy: ¡ Temporal equivalent of interpixel redundancy. ¡ Video frame rates are sufficiently high that the change in a pixel’s value from frame to frame is typically very small over the majority of the frame. TK 3813 35
Image Compression ¡ Three types: l 12/17/2021 Lossless compression. ¡ To remove redundancies when data are stored or transmitted and then replace it when the image is reconstructed. ¡ Moderate compression. ¡ E. g. Delta compression (recode an image in terms of the difference in grey level between each pixel and the previous pixel in the row) TK 3813 36
Image Compression l Lossy compression. ¡ Rely on the fact that human visual system is insensitive to the loss of certain kind of information. ¡ Discard some of the information present in the image. ¡ These can be used when raw image data can be easily reproduced or the information loss can be tolerated. ¡ E. g: JPEG l l 12/17/2021 Images are encoded in 8 x 8 blocks. A discrete cosine transform is applied. The results are quantised to remove unused frequencies. The result is encoded using a variable length encoding scheme. TK 3813 37
Image Compression l Error-Free compression. ¡ In some application (medical, bussiness, satellite imagery, etc. ), lossy compression is usually prohibited for legal reason or undesirable loss. ¡ Error-free compression techniques generally are composed of two relatively independent operations: l l ¡ 12/17/2021 Devising an alternative representation of the image in which its interpixel redundancies are reduced. Coding the representation to eliminate coding redundancies. E. g. Variable length coding. TK 3813 38
Java and Image Processing ¡ Java and Images l l 12/17/2021 Java provides and Image class – java. awt. Image for the purpose of storing images. Contains methods for obtaining information about the Image object TK 3813 39
Java and Image Processing 12/17/2021 TK 3813 40
Java and Image Processing 12/17/2021 TK 3813 41
Java and Image Processing A P P L E T 12/17/2021 TK 3813 42
Java and Image Processing ¡ Image object compose of three interface : l l l 12/17/2021 Image observer Image producer Image consumer TK 3813 43
Java and Image Processing 12/17/2021 TK 3813 44
Java and Image Processing 12/17/2021 TK 3813 45
Java and Image Processing ¡ Producers and Consumers l l l 12/17/2021 Availability of image data Image handling in Java 1. 0/1. 1 Push model of image processing TK 3813 46
Java and Image Processing ¡ Why producer and consumer: l l ¡ Image l l 12/17/2021 Flexibility for programmers Loading and displying images doesn’t make the grades. Represents an image (instance of image) Obtaining information about image e. g. width and height. TK 3813 47
Java and Image Processing ¡ Image Producer (implement the Image. Producer interface) l l 12/17/2021 Can generate pixel values or acquire pixels from the source (get. Image()). Forwarding the pixel data to one or more consumers. TK 3813 48
Java and Image Processing ¡ public interface Image. Consumer (implement the Image. Consumer interface) l l l 12/17/2021 The interface for objects expressing interest in image data through the Image. Producer interfaces. When a consumer is added to an image producer, the producer delivers all of the data about the image using the method calls defined in this interface. Register their interset in an image with the producer and, as image data become available, the producer calls methods of those consumer to transfer the data to them. Work behind the scenes to facilitate the display of images on AWT components such as Canvas. TK 3813 49
Java and Image Processing 12/17/2021 TK 3813 50
Java and Image Processing ¡ Two ways to create an Image object l l 12/17/2021 Off-screen image Image. Producer TK 3813 51
Java and Image Processing ¡ 12/17/2021 Creating custom image producer TK 3813 52
Java and Image Processing 12/17/2021 TK 3813 53
Java and Image Processing 12/17/2021 TK 3813 54
Java and Image Processing 12/17/2021 TK 3813 55
Java and Image Processing 12/17/2021 TK 3813 56
Java and Image Processing 12/17/2021 TK 3813 57
Java and Image Processing 12/17/2021 TK 3813 58
M E M O R Y I M A G E S O U R C E 12/17/2021 Java and Image Processing Gray. Scale Image Color Image (default) TK 3813 59
Java and Image Processing ¡ 12/17/2021 Memory. Image. Source TK 3813 60
Java and Image Processing ¡ Memory. Image. Source and create. Image java. awt. Component 12/17/2021 1 -Dimensional array TK 3813 61
Reading and Writing Images in Java ¡ Previous lecture, we use get. Image() or create. Image() methods of Applet and Toolkit classes to load a GIF and JPEG image into an image object. The limitation: l l l 12/17/2021 It is read-only; there is no support for writing images in these formats. It cannot be extended easily to support other formats. It loads images asynchronously, so special measures may need to be taken to delay processing until loading has completed. TK 3813 62
Reading and Writing Images in Java ¡ With Java 2, explicit handling of JPEG image file is supported via the classes of the com. sun. image. codec. jpegpackage. l l l 12/17/2021 Can load image data synchronously into Buffered. Image objects. Can write image data to a JPEG file. However, since the JPEG uses lossy compression, every cycle of reading and writing an image degrades it a little more. TK 3813 63
Display ¡ Hardware: l Although computer’s colour monitor has 3 independent electron guns for red, green and blue components of a video signal, this does not guarantee that we will be able to display 24 -bit RGB images due to: ¡ PC graphics cards have a fixed on-board video memory which can be configured for various different combinations of spatial and colour resolution. l l 12/17/2021 Trade-off between these parameters. Increase spatial resolution (can display larger image), reducing colour resolution (image may not be rendered accurately). TK 3813 64
Display ¡ 12/17/2021 Software: displaying images in Java. TK 3813 65
Thank you 12/17/2021 TK 3813 66
- Slides: 66