General format of the image

Source: Internet
Author: User

Abstract: Describes the structure of various image files, especially the PPM file format and read-write PPM files. Because the recent use of PPM files for image processing, has not been touched before, so will be particularly concerned.

The general image file structure mainly contains the file header, file body and the end of the file three parts:

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

File body: Image data, color transform table

End of File: User name, comment, development date, working time
The above is a approximate image file structure description, the actual structure of the different format of the entries to be much finer, the structure is much more complex, the individual items occupy space and the order of the items are also very different. There is currently no very uniform image file format. However, most image processing software is compatible with several image file formats and can read many different formats of image files. In this way, different image formats can be converted to each other. Of course, there are special image format conversion software for the conversion between various image formats.

BMP format
BMP is a hardware device independent of the image file format, the use of very wide. It uses the bit map storage format, in addition to the image depth optional, does not adopt any other compression, therefore, the BMP file occupies a large space. BMP file Image depth selectable 1bit, 4 bit, 8 bit and 24bit. BMP files when storing data, the image is scanned in order from left to right, from bottom to top.
PCX format
is the image file format of the PC brush. The image depth of PCX can be selected as 1, 4, 8bit. Because this file format appears earlier, it does not support true color. PCX files are encoded with RLE stroke, and the compressed image data is stored in the file body. Therefore, when the captured image data is written in the PCX file format, it is to be RLE encoded, while reading a PCX file first to the RLE decoding, in order to further display and processing.
TIFF format
The TIFF (Tag image FileFormat) file is a more general image file format developed by Aldus and Microsoft for Scanners and desktop publishing systems. The TIFF format is flexible and variable, and it defines four different formats: Tiff-b for two-value images, tiff-g for black-and-white grayscale images, tiff-p for color images with color palettes, and tiff-r for RGB true color images. TIFF supports a variety of encoding methods, including RGB uncompressed, RLE compression, and JPEG compression.
GIF format
The GIF (Graphics Interchange Format) is an image file format developed by CompuServe Company in 1987 and expanded in 1989 on the 1987 version, with the expanded version number defined as GIF89A, The 1987 version was gif87a. GIF uses the LZW compression algorithm to store the image data, and uses the variable length compression algorithm. GIFs have an image depth from 1bit to 8bit, or GIF supports up to 256 colors of images. Another feature of the GIF format is that it can store multiple color images in a GIF file, which can form the simplest animation if multiple images stored in a file are read out and displayed on the screen.

SWF format
SWF (Shock Waveflash) is an animated file format generated by Macromedia Company Software Flash. This is a network vector graphics standard, high compression rate, but need flash software or plug-in to play

JPEG format
JPEG (Joint photographic expertsgroup) is an image expert group composed of CCITT (International Telegraph and Telephone Advisory Committee) and ISO (Organization for Standardization). The Expert Group developed the first international standard for compressing static digital images, with the standard name "digital compression and encoding of continuous-tone static images (Digitalcompression and Coding of Continuous-tone stillimage)", Referred to as the JPEG algorithm. This is a common standard with a wide range of applications, with the following objectives:

1. The algorithm developed in the image compression ratio is/or close to the current level of science, image fidelity in a wide range of compression evaluation is "very good", "excellent" to the original image "can not be distinguished."

2. The developed algorithm can be applied to any kind of digital image source, such as image size, color space, pixel aspect ratio, image content, complexity, color number and statistical characteristics.

3. The developed algorithm can be adjusted in terms of the complexity of the calculation, so that software execution or hardware execution can be selected based on performance and cost requirements.

4. The developed algorithm includes four encoding methods: Sequential coding, Progressive coding, lossless compression coding and layered coding.

JPEG uses a symmetric compression algorithm, which is the same time used to compress and decompress in the same system environment. Compressed image using JPEG compression coding algorithm, the compression ratio of about 1:5 to 1:50, or even higher.

PNG format
PNG (Portable Network Graphic format, portable Web image formats) is a lossless bitmap file storage format that was developed by the organization in the mid 1990s, officially announced October 1, 1996, is a light, no legal obstacles, A standard for good compression and specification, intended to be an attempt to replace GIFs and TIFF while adding features that are not available in their file formats. The PNG name is derived from the unofficial "PNG ' Snot GIF", which is a bitmap file (bitmapfile) storage format, read as "ping". PNG supports indexed color, grayscale, and True color, and provides an optional alpha channel. When used to store grayscale images, the depth of the grayscale image can be as much as 16 bits (bit), the color image can be stored in depth up to 48 bits (bit), and up to 16 bits of alpha channel data can be stored. PNG image format files (or data streams) consist of a 8-byte PNG file signature (PNG filesignature) domain and more than 3 data blocks (chunk) organized by a specific structure. PNG defines two types of data blocks, one of which is called the Critical Data Block (Criticalchunk), which is the standard data block, and the other is called the Auxiliary Data Block (ancillarychunks), which is an optional block of data. Critical data blocks define 4 standard blocks, each of which must contain them, and PNG read-write software must support them. Although the PNG file specification does not require the PNG codec to encode and decode an optional block of data, the specification advocates support for optional blocks of data.

Special attention to PPM

PPM Grayscale files:

The file header consists of 3 lines of text, which can be read by fgets

1) First action "P2", indicating file type

2) The width and height of the second behavior image

3) The maximum pixel value of the third Act 255
Next is the image data block. stored in row order. Each pixel occupies 4 bytes, the grayscale channel is a 4-byte ASCII integer, and the high byte is in front. The upper-left corner is the origin of the coordinates.


16-bit PPM files: (at least for reading PPM files generated by Dcraw)

The file header consists of 3 lines of text, which can be read by fgets

1) First action "P6", indicating file type

2) The width and height of the second behavior image

3) The maximum pixel value of the third act

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

ppm Color files:

The file header consists of 3 lines of text, which can be read by fgets

1) First action "P3", indicating file type

2) The width and height of the second behavior image

3) The maximum pixel value of the third Act 255

Next is the image data block. stored in row order. Each pixel occupies 12 bytes, followed by a red-green-blue channel, an integer representing 4-byte ASCII for each channel, and a high byte in front. The upper-left corner is the origin of the coordinates.

PPM file read and write

#ifndef Pnm_file_h
#define Pnm_file_h
#include <cstdlib>
#include <climits>
#include <cstring>
#include <fstream>
#include "image.h"
#include "misc.h"
#include <iostream.h>//for Debug,qiansen
#define BUF_SIZE 256

Class Pnm_error {};

Static image<rgb> *loadppm (const char *name) {
Char Buf[buf_size], doc[buf_size];


Std::ifstream file (name, Std::ios::in |std::ios::binary);
Pnm_read (file, buf);
if (strncmp (buf, "P5", 2)) {
Throw Pnm_error ();
cout<< "Pnmversion is P6,may was not supported." <<endl;
}
Pnm_read (file, buf);
Intwidth = Atoi (BUF);
Pnm_read (file, buf);
int height = atoi (buf);

Pnm_read (file, buf);
if (Atoi (BUF) >uchar_max)
Throw Pnm_error ();


image<rgb> *im = new image<rgb> (width, height);
File.read ((char*) imptr (im,0,0), Width * height *sizeof (RGB));
return im;
}

static void saveppm (Image<rgb>*im, Constchar *name) {
Intwidth = Im->width ();
int height= im->height ();
Std::ofstream file (name, std::ios::out | std::ios::binary);

file<< "p6\n" <<width<< "" <File.write ((char*) imptr (im,0,0), Width * height *sizeof (RGB));
}

Reference documents:

[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

Turn from: http://blog.163.com/[email protected]/blog/static/3567320220071122112155300/

from:http://blog.csdn.net/yangtrees/article/details/7731762

General format of the image

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.