General image format

Source: Internet
Author: User

Abstract: describes the structure of various image files, especially the PPM file format and the reading and writing of PPM files. Because we have recently used ppm files for image processing and have not been used before, we will pay special attention to them.

 

   Generally, the image file structure includes three parts: File Header, file body, and file end:

    File Header: Software ID, software version number, image resolution, image size, image depth, color type, encoding method, compression algorithm

   File body: image data, color conversion table

   End of file: User Name, comment, development date, working time
   The above is a rough description of the structure of the image file. The actual structure is much more detailed and the structure is much more complex according to the entries in different formats, the space occupied by each entry and the order of the entries are also quite different. Currently, there is no uniform image file format. However, most image processing software is compatible with several image file formats and can also read image files of different formats. In this way, different image formats can be converted to each other. Of course, there are also specialized image format conversion software for conversion between different image formats.

BMP format
BMP is an image file format unrelated to hardware devices and is widely used. It uses a bit ing storage format. Apart from image depth options, it does not use any compression. Therefore, BMP files occupy a large amount of space. The image depth of a BMP file can be 1 bit, 4 bit, 8 bit, or 24 bit. When a BMP file stores data, the image is scanned in the order of left to right and bottom to top.
PCX format
Is the image file format of the PC paint brush. The image depth of PCX can be 1, 4, or 8 bits. Because this file format appears earlier, it does not support true color. The PCX file uses RLE travel encoding, and the file body stores the compressed image data. Therefore, when writing the collected image data into the PCX file format, you need to encode it using Rle. When reading a PCX file, you must first perform RLE decoding on it, can be further displayed and processed.
Tiff format
The tag image fileformat file is a common image file format developed by Aldus and Microsoft for scanners and table Publishing Systems. Tiff format flexible and variable, it defines four different formats: TIFF-B for binary images; TIFF-G for black-and-white gray images; TIFF-P for color images with color palette; TIFF-R applies to RGB true color images. Tiff supports multiple encoding methods, including RGB, RLE, and JPEG.
GIF format
GIF (Graphics Interchange Format) is an image file format developed by CompuServe in 1987. In 1989, it was expanded based on the 1987 version. The expanded version number is defined as gif89a, the 1987 version is gif87a. GIF adopts LZW compression algorithm to store image data, and adopts a variable length compression algorithm. The GIF image depth ranges from 1 bit to 8 bit, that is, GIF supports up to 256 colors. Another feature of the GIF format is that it can store multiple color images in a GIF file. If you read multiple image data in a file one by one and display it on the screen, to form the simplest animation.

Swf format
SWF (shock waveflash) is an animation file format generated by Macromedia Flash. This is a network vector graphics standard with a high compression rate, but Flash software or plug-ins are required for playback.

JPEG format
JPEG (joint photographic expertsgroup) is an image Expert Group composed of CCITT (International Telegraph Telephone Advisory Committee) and ISO (International Organization for Standardization. The first international standard for compressing static digital images developed by the expert group, named "digital compression and encoding of Continuous Tone static images (digitalcompression and coding of continuous-tone stillimage )", JPEG algorithm for short. This is a widely applied general standard with the following objectives:

   1. the developed algorithms are/or close to the current scientific level in terms of image compression ratio, the image fidelity is evaluated as "good" and "excellent" to "no difference" from the original image in a wide compression range ".

   2. developed algorithms can be applied to any type of digital image source, for example, there are no restrictions on the image size, color space, pixel aspect ratio, image content, complexity, number of colors, and statistical characteristics.

   3. The developed algorithms can be adjusted in terms of computing complexity. Therefore, you can choose whether to execute the algorithms using software or hardware based on performance and cost requirements.

    4. The developed algorithms include four encoding methods: sequential encoding, progressive encoding, lossless compression encoding, and hierarchical encoding.

   JPEG adopts a symmetric compression algorithm, that is, it takes the same time for compression and decompression in the same system environment. The compression ratio of an image compressed using the JPEG compression algorithm is about to, or even higher.

PNG format
PNG (portable network graphic format, portable network image format) is a Lossless Bitmap File storage format developed by W3C In The Middle Of 1990s. It was officially announced on April 9, October 1, 1996, it is a lightweight, legal-free, well-performing, and well-regulated standard designed to replace GIF and Tiff while adding features not available in their file formats. The PNG name is derived from the unofficial "PNG 'snot GIF". It is a bitmapfile storage format and is read as "ping ". PNG supports indexing color, grayscale, and true color, and provides optional Alpha channels. When used to store grayscale images, the depth of a grayscale image can be up to 16 bits. When the color image is stored, the depth of the color image can be up to 48 bits ), it can also store up to 16 alpha channel data. PNG image format files (or data streams) are signed by an 8-byte PNG file (PNG
Filesignature) is composed of more than three chunks in a specific structure. PNG defines two types of data blocks. One is criticalchunk, which is a standard data block. The other is ancillarychunks ), this is an optional data block. Key data blocks define four standard data blocks. Each PNG file must contain them. PNG read/write software must also support these data blocks. Although the PNG file specification does not require the PNG codecs to encode and decode optional data blocks, the specification advocates the support of optional data blocks.

 

Pay special attention to ppm

PPM grayscale file:

The file header consists of three lines of text and can be read by fgets.

1) The first behavior "p2" indicates the file type

2) width and height of the second behavior Image

3) The maximum pixel value of the third behavior is 255.
Next is the image data block. Store data in row order. Each pixel occupies 4 bytes, and the gray channel is an integer in the 4-byte ASCII code. The high byte is in the front. The upper left corner is the coordinate origin.

16-bit ppm file: (at least applicable to reading ppm files generated by dcraw)

The file header consists of three lines of text and can be read by fgets.

1) The first behavior "P6" indicates the file type

2) width and height of the second behavior Image

3) maximum pixel value of the third Behavior

Next is the image data block. Store data in row order. Each pixel occupies 6 bytes, followed by a red, green, and blue channel. Each channel is a 2-byte integer with a high byte in front.

 

Ppm color files:

The file header consists of three lines of text and can be read by fgets.

1) The first behavior "P3" indicates the file type

2) width and height of the second behavior Image

3) The maximum pixel value of the third behavior is 255.

Next is the image data block. Store data in row order. Each pixel occupies 12 bytes, followed by a red, green, and blue channel. Each channel is an integer expressed in 4-byte ASCII code, with the highest byte in front. The upper left corner is the coordinate origin.

 

Read and Write ppm files

 # IfndefPnm_file_h
 # DefinePnm_file_h    
 # Include<Cstdlib>
 # Include<Climits>
 # Include<Cstring>
 # Include<Fstream>
 # Include"Image. H"
 # Include"Misc. H"
 # Include<Iostream. h> //Debug, qiansen     
 # DefineBuf_size256  
   
 ClassPnm_error{};

 StaticImage <RGB>* Loadppm (constChar* Name){
 CharBuf [buf_size],Doc [buf_size];
  
  
 STD: ifstreamFile (name,STD: IOS: In| STD: IOS: Binary );
 Pnm_read (file,Buf );
 If(Strncmp (BUF,"P5 ",2 )){
  // ThrowPnm_error ();
  Cout <"pnmversion IsP6, MayBe not supported. "<Endl;
 }
 Pnm_read (file,Buf );
 Intwidth=Atoi (BUF );
 Pnm_read (file, Buf );
 IntHeight =Atoi (BUF );
 
 Pnm_read (file,Buf );
 If (atoi (BUF)> uchar_max)
  ThrowPnm_error ();
 
  
 Image <RGB>* Im=NewImage <RGB> (width,Height );
 File. Read (char *) imptr (IM, 0, 0 ),Width*Height* Sizeof (RGB ));  
     ReturnIm;
 }
 
 StaticVoidSaveppm (image <RGB> * im,Constchar * name){
 Intwidth= Im-> width ();
 Int Height =Im-> height ();
 STD: ofstreamFile (name,STD: IOS: Out|STD: IOS: Binary );
 
 File <"P6 \ n" <width <""<Height <" \ n "<uchar_max <" \ n ";
 File. Write (char *) imptr (IM, 0, 0), width*Height* Sizeof (RGB ));
 } 

References:

[1] http://topic.csdn.net/t/20050912/10/4263160.html
[2] http://www.kylinx.net/node/55
[3] http://blog.csdn.net/begtostudy/archive/2006/10/13/1332750.aspx

From: http://blog.163.com/jianweicheng@126/blog/static/3567320220071122112155300/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.