PNG
Bách khoa toàn thư mở Wikipedia
PNG (từ viết tắt trong tiếng Anh của Portable Network Graphics; chính thức đọc như "pinh") là một dạng hình ảnh sử dụng phương pháp nén dữ liệu mới - không làm mất đi dữ liệu gốc. PNG được tạo ra nhằm cải thiện và thay thế định dạng ảnh GIF với một định dạng hình ảnh không đòi hỏi phải có giấy phép sáng chế khi sử dụng. PNG được hỗ trợ bởi thư viện tham chiếu libpng, một thư viện nền tảng độc lập bao gồm các hàm của C để quản lý các hình ảnh PNG.
Những tập tin PNG thường có phần mở rộng là PNG
and png
và đã được gán kiểu chuẩn MIME là image/png
(được công nhận vào ngày 14 tháng 10 năm 1996).
Mục lục |
[sửa] Lịch sử và phát triển
Động cơ thúc đẩy cho việc tạo ra định dạng PNG bắt đầu vào khoảng đầu năm 1995, sau khi Unisys công bố họ sẽ áp dụng bằng sáng chế vào thuật toán nén dữ liệu LZW- được sử dụng trong định dạng GIF. Thuật toán được bảo vệ bởi bằng công nhận độc quyền sáng tạo ở trong nước Mỹ và tất cả các nước trên thế giới. Tuy nhiên, cũng đã có một số vấn đề với định dạng GIF khi cần có một số thay đổi trên hình ảnh, nhất giới hạn của nó là 256 màu trong thời điểm máy tính có khả năng hiển thị nhiều hơn 256 màu đang trở nên phổ biến. Mặc dù định dạng GIF có thể thể hiện các hình ảnh động, song PNG vẫn được quyết định là định dạng hình ảnh đơn (chỉ có một hình duy nhất). Một người "anh em" của nó là MNG đã được tạo ra để giải quyết vấn đề ảnh động. PNG lại tăng thêm sự phổ biến của nó vào tháng 8 năm 1999, sau khi hãng Unisys huỷ bỏ giấy phép của họ đối với các lập trình viên phần mềm miễn phí, và phi thương mại.
- Phiên bản 1.0 của đặc tả PNG được phát hành vào ngày 1 tháng 7 năm 1996, và sau đó xuất hiện vơi tư cách RFC 2083. Nó được tổ chức W3C khuyến nghị vào ngày 1 tháng 10 năm 1996.
- Phiên bản 1.1, với một số thay đổi nhỏ và thêm vào 3 thành phần mới, được phát hành vào ngày 31 tháng 12 năm 1998.
- Phiên bản 1.2, thêm vào một thành phần mở rộng, được phát hành vào ngày 11 tháng 8 năm 1999.
- PNG giờ đây là một chuẩn quốc tế (ISO/IEC 15948:2003), và cũng được công bố như một khuyến nghị của W3C vào ngày 10 tháng 11 năm 2003. Phiên bản hiện tại của PNG chỉ khác chút ít so với phiên bản 1.2 và không có thêm thành phần mới nào.
[sửa] Thông tin kỹ thuật
[sửa] Phần đầu của tập tin
Một tập tin PNG bao gồm 8-byte kí hiệu (89 50 4E 47 0D 0A 1A 0A được viết trong hệ thống có cơ số 16, chứa các chữ "PNG" và 2 dấu xuống dòng [1], ở giữa là sắp xếp theo số lượng của các thành phần, mỗi thành phần đều chứa thông tin về hình ảnh. Cấu trúc dựa trên các thành phần được thiết kế cho phép định dạng PNG có thể tương thích với các phiên bản cũ khi sử dụng.
[sửa] Các "thành phần" trong tập tin
PNG là cấu trúc như một chuỗi các thành phần, mỗi thành phần chứa kích thước, kiểu, dữ liệu, và mã sửa lỗi CRC ngay trong nó.
Chuỗi được gán tên bằng 4 chữ cái phân biệt chữ hoa chữ thường. Sự phân biệt này giúp bộ giải mã phát hiện bản chất của chuỗi khi nó không nhận dạng được.
Với chữ cái đầu, viết hoa thể hiện chuỗi này là thiết yếu, nếu không thì ít cần thiết hơn ancillary. Chuỗi thiết yếu chứa thông tin cần thiết để đọc được tệp và nếu bộ giải mã không nhận dạng được chuỗi thiết yếu, việc đọc tệp phải được hủy.
The case of the second letter indicates if the chunk is "public" (either in the specification or the registry of special purpose public chunks) or "private" (not standardised). Uppercase is public and lowercase is private. This ensures that public and private chunk names can never conflict with each other.
The third letter must be uppercase to conform to the PNG specification and is reserved for future expansion. Decoders should not complain about a lower case third letter as it may be used by some future extension of the PNG specification.
The case of the fourth letter indicates if a chunk is safe to copy by editors that don't recognise it. If lowercase the chunk may be safely copied regardless of the extent of modifications to the file. If uppercase it may only be copied if the modifications have not touched any critical chunks.
[sửa] Essential chunks
A decoder must be able to interpret these to read and render a PNG file.
- IHDR must be the first chunk, it contains the header
- PLTE contains the palette (list of colors)
- IDAT contains the image. The image may be split among multiple IDAT chunks. Doing so increases the filesize slightly but makes it possible to generate a PNG in a streaming manner.
- IEND marks the end of the image
[sửa] Metadata chunks
Other image attributes that can be stored in PNG files include gamma values, background color, and textual metadata information. PNG also supports color correction with the use of color management systems like sRGB.
- bKGD gives the default background color
- cHRM give the white balance
- gAMA specified gamma
- hIST can store the histogram, or total amount of each color in the image
- iCCP is an ICC color profile
- iTXt contains international (UTF-8) text either compressed or uncompressed.
- pHYs is the physical size of the image
- sBIT (significant bits) indicates the color-accuracy of the source data
- sPLT suggests a palette to use if the full range of colors is not available
- sRGB indicates that standard RGB colors are used
- tEXt can store text that can be represented in ISO 8859-1, with one name=value pair for each chunk
- tIME stores the time that the image was last modified
- tRNS contains transparency information. For indexed images it stores an alpha channel value for each palette entry. For truecolor and greyscale images it stores a single pixel value that is to be regarded as transparent.
- zTXt contains compressed text with the same limitations as tEXt
The lowercase first letter in these chunks indicates that they're not required for the PNG specification. The lowercase last letter in some chunks indicates that they're safe to copy, even if the application concerned doesn't understand them.
[sửa] Color depth
PNG images can either use palette-indexed color or be made up of one or more channels (numerical values directly representing quantities about the pixels). When there is more than one channel in an image all channels have the same number of bits allocated per pixel (known as the bitdepth of the channel). Although the PNG specification always talks about the bitdepth of channels, most software and users generally talk about the total number of bits per pixel (sometimes also referred to as bitdepth or color depth).
The number of channels will depend on if the image is greyscale or color and if it has an alpha channel. PNG allows the following combinations of channels:
bitdepth per channel | 1 | 2 | 4 | 8 | 16 |
indexed (color type 3) | 1 | 2 | 4 | 8 | |
greyscale (color type 0) | 1 | 2 | 4 | 8 | 16 |
greyscale & alpha (color type 4) |
16 | 32 | |||
truecolor (RGB - color type 2) |
24 | 48 | |||
truecolor & alpha (RGBA - color type 6) |
32 | 64 |
- greyscale
- greyscale and alpha (level of transparency for each pixel)
- red, green and blue (rgb/truecolor)
- red, green, blue and alpha
With indexed color images, the palette is always stored at a depth of 8 bits per channel. The palette must not have more entries than the image bitdepth allows for but it may have fewer (so if an image for example only uses 90 colors there is no need to have palette entries for all 256).
Indexed color pngs are allowed to have 1, 2, 4 or 8 bits per pixel by the standard; greyscale images with no alpha channel allow for 1, 2, 4, 8 or 16 bits per pixel. Everything else uses a bitdepth per channel of either 8 or 16. The combinations this allows are given in the table on the right. The green cells with numbers represent the supported formats, the numbers inside the total bits per pixel. The red cells represent unsupported combinations. The standard requires that decoders can read all supported color formats but many image editors can only produce a small subset of them.
[sửa] Độ trong suốt của ảnh
PNG offers a variety of transparency options. With truecolor and greyscale images either a single pixel value can be declared as transparent or an alpha channel can be added. For paletted images, alpha values can be added to palette entries. The number of such values stored may be less than the total number of palette entries, in which case the remaining entries are considered fully opaque.
The scanning of pixel values for binary transparency is supposed to be performed before any color reduction to avoid pixels becoming unintentionally transparent. This is most likely to pose an issue for systems that can decode 16 bits per channel images (as they must to be compliant with the specification) but only output at 8 bits per channel (the norm for all but the highest end systems).
[sửa] Kĩ thuật nén
PNG uses a non-patented lossless data compression method known as deflation. This method is combined with prediction, where for each image line, a filter method is chosen that predicts the color of each pixel based on the colors of previous pixels and subtracts the predicted color of the pixel from the actual color. An image line filtered in this way is often more compressible than the raw image line would be, especially if it is similar to the line above (since deflate has no understanding that an image is a 2D entity it just sees the image data as a stream of bytes).
[sửa] Interlacing
PNG offers an optional 2-dimensional, 7-pass interlacing scheme – the Adam7 algorithm. This is more sophisticated than GIF's 1-dimensional, 4-pass scheme, and allows a clearer low-resolution image to be visible earlier in the transfer.
[sửa] Ảnh động
PNG does not offer animation. MNG is an image format that supports animation and is based on the ideas and some of the chunks of PNG but is a complex system and does not offer fallback to single image display like GIF does. APNG is another image format based on PNG that supports animation and is simpler than MNG. APNG offers fallback to single image display for PNG decoders that do not support APNG. However, as of 2005 neither of these formats are widely supported.
[sửa] So sánh mặt kĩ thuật với GIF
- On most images, PNG can achieve greater compression than GIF (but see the section on filesize, below).
- PNG gives a much wider range of transparency options than GIF, including alpha-channel transparency.
- PNG gives a much wider range of color depths than GIF (truecolor compared to 256-color), allowing for greater color precision, smoother fades etc.
- GIF supports animation while PNG does not (but see the section on animation, above).
[sửa] Những chương trình sửa ảnh bitmap hỗ trợ PNG
Note that Macromedia Fireworks uses PNG as its native file format, but this contains a lot of metadata, such as information about layers, animation, text, and effects, so should not be distributed directly in this format. Fireworks can export as an optimised PNG (without the extra metadata), for use on web-pages etc. [2]
Other popular graphics programs which support the PNG format are Adobe Photoshop and The Gimp, a free and open-source image editing application available for Windows, Mac and Unix systems.
Image processing programs that have PNG compression problems mainly related to lack of full implementation of the PNG compressor library
- IrfanView 3.85
- Microsoft Picture It! Photo Premium 9.
- Macromedia Fireworks
[sửa] Sự hỗ trợ của các trình duyệt web cho PNG
Despite campaigns such as "burn all gifs" [3], tools such as gif2png [4], calls by the Free Software Foundation, and influential writers, PNG adoption on websites has been fairly slow.
GIF is still more widely used than PNG, partially due to misconceptions but also for several real reasons:
- On very old browsers such as Internet Explorer 3, PNG support may not be available
- GIF images support animation, which is often used in Internet advertising.
The full feature set of PNG is not often used on the web, for several reasons
- Internet Explorer 4 can crash on images containing metadata (e.g. Fireworks' native PNG file format)
- Versions of Internet Explorer up to 6 do not support native alpha-channel transparency
The fact that Internet Explorer does not support alpha-channel transparency has led some people to erroneously believe that Internet Explorer does not support transparency at all. However PNGs can be given a single transparent background color, which works on IE, and is equivalent to the transparency available with GIF images.
[sửa] Kích thước file
Generally, PNG files created by a decent encoder and without unwanted metadata should be smaller than the identical image encoded in GIF format. However PNG gives the image creator far more flexibility than GIF and care needs to be taken to avoid PNG files that are needlessly large.
As GIF is limited to 256 colors, many image editors will automatically reduce the color depth when saving an image in GIF format. Therefore when people save the same truecolor image as PNG and GIF they see that the GIF is smaller (they may also see that the PNG is higher quality depending on the original image and the quality of the color reduction). They do not realise it is possible to create a 256 color PNG that has identical quality to the GIF but has a smaller filesize than the GIF, and so they develop the misconception that PNG files are larger than "equivalent" GIF files.
Another mistake that people make is to compare PNG files that contain a large amount of metadata (e.g. unoptimized PNG files from Macromedia Fireworks) to files in other formats which do not contain metadata.
Some versions of Adobe Photoshop were also reported to have very poor PNG output support which further fuelled the idea that PNG is larger than GIF.
Various tools are available for optimizing PNG files. OptiPNG and pngcrush are both Open-source software optimizers for PNG files that can be run from a commandline in an MS-DOS window, or from a Unix or Linux commandline, and are very effective at reducing the size of PNG files for the web, especially those exported from Adobe Photoshop (versions prior to CS) and other graphics packages. Other tools such as PNGOut and AdvanceCOMP can also reduce the size of PNG files even more and thus, give the user the smallest file size possible for a given PNG image.
PNG was not intended to replace the other popular web image format JPEG. See the Comparison with JPEG section below for more details on how the two formats compare.
[sửa] So sánh với JPEG
JPEG will produce a smaller file than PNG for photographic (and photo-like) images since it uses a lossy encoding method specifically designed for photographic image data. Using PNG instead of a high-quality JPEG for such images would result in a large increase in filesize (often 5-10 times) with negligible gain in quality.
PNG is a better choice than JPEG for storing images that contain text, line art, or other images with sharp transitions that don't transform well into the frequency domain. Where an image contains both sharp transitions and photographic parts a choice must be made between the large but sharp PNG and JPEG artifacts around sharp transitions.
Finally, PNG is useful as a lossless format for images that are likely to undergo further editing and may need to be distributed in that lossless form, since it uses a better compression algorithm and is better supported than TIFF. JPEG is a poor choice for storing images that may need to be edited further as it suffers from generation loss issues.
[sửa] See also
- Digital image editing
- Computer Graphics, including:
- Comparison of layout engines (graphics)
- Related graphics file formats
- APNG
- JNG
- MNG
- Hình file formats
[sửa] External links
[sửa] libpng.org
- PNG Home Site
- libpng Home Page
- PNG-supporting browsers - overview of PNG status in different web browsers. First Windows IE support in 4.0b1; Netscape 4.04 [5].
- The Story of PNG by Greg Roelofs
- PNG: The Definitive Guide (Online Version) by Greg Roelofs
[sửa] W3C
[sửa] Others
- RFC 2083
- PNG transparency test
- Transparent PNG Generator
- The GD-library to generate dynamic PNG-files with PHP
- A guide to PNG optimization
- PNG as a future proof format
- PNG Adam7 interlacing
- List of PNG-related resources, libraries and papers