Answer the question
In order to leave comments, you need to log in
How are pictures encoded?
When downloading a file from the network (in this case, a picture), we are dealing with an array of bytes. How to determine what extension this picture is (png, jpg, bmp, etc.)? Is there any service information? How to convert from one format to another?
PS Yes, I know, you can find a specification of any format and subtract it there, but I would like to understand the basics first, and then poke around in the specifications
Answer the question
In order to leave comments, you need to log in
There is such a thing - a signature. A set of bytes that only occur in this file type.
PNG also has a signature. www.libpng.org/pub/png/spec/1.2/PNG-Structure.html
The first eight bytes of a PNG file always contain the following (decimal) values:
137 80 78 71 13 10 26 10
In the general case, of course, in the data transfer protocol there must be a way to indicate how the transmitted data is to be interpreted. For example, HTTP uses headers in the response to indicate the content type (Content-Type) and encoding at the HTTP level, such as compression, if present. Upon receiving data, the client (for example, a browser) must decode the content to its original form (for example, decompress if the server compressed the resource being sent) and then work with it based on the specified Content-Type. It is impossible to 100% determine in advance that a picture of one format or another, but, as Pavel Volintsev correctly noted, most formats have a signature, according to which, with a probability of 99.9 ..%, we can say that a PNG or JPEG has arrived. In any case, a correctly implemented graphic format reader (decoder) should be prepared for the fact that the picture can be damaged and should give an error if it is impossible to parse the picture according to the intended format.
The raster image formats themselves (we are not considering a vector now) just the same and differ primarily in: a) features of the representation of a pixel map (matrix of pixels): how color is encoded, index or component, how many different color values a pixel can have, whether there is compression what compression algorithms are used - lossless (PNG) or lossy (JPEG); b) service information: this can be both basic necessary information, for example, image dimensions and the size of one pixel in bytes (read - how many bytes are allocated for the color of one pixel), and additional, for example EXIF, where you can specify the author, and the shooting mode, and much more; c) add. features that make each format special: support for animation (GIF, MNG), support for lossy compression (JPEG), support for layers in pictures (Photoshop PSD), support for "multi-page" images (TIF) and so on.
Converting actually comes down to:
1) determining the original image format;
2) reading the source format in order to translate it into a convenient expanded representation in memory, convenient for the programmer in terms of further operations (compress, read service information, etc.)
3) writing the resulting representation in memory to a new format, which again means writing the necessary signatures and service information in accordance with the format specification, plus image conversion by compression algorithms, if necessary (for example, jpeg efficiency for photographic images is achieved using a rather complex and cunning encoding algorithm , which takes into account the perception of color by a person, while PNG compresses almost like a regular archiver, which does not interpret the compressed data in any way).
I hope it is clear where to dig further.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question