Color coding. Coding of graphic information_10 class_Informatics lesson §16. Image encoding

What difficulties did you have? How can they be overcome?

2. Plot an 8 pixel wide black and white drawing encoded with the hexadecimal sequence 2466FF6624 16 .

3. Plot a black and white drawing 5 pixels wide, encoded with the hexadecimal sequence 3A53F88 16 .

4. A 10×15 cm drawing is encoded with a resolution of 300 ppi. Estimate the number of pixels in this drawing. (Answer: about 2 megapixels)

5. Construct a hex code for colors that have RGB codes (100,200,200), (30,50,200), (60,180, 20), (220, 150, 30). (Answer: #64C8C8, #1E32C8, #3CB414, #DC961E)

6. How would you name a color specified on a web page as a code: #CCCCCC, #FFCCCC, #CCCCFF, #000066, #FF66FF, #CCFFFF, #992299, #999900, #99FF99? Find the decimal values ​​of the components of the RGB code. (Answer: (204,204,204), (255,204,204), (204,204,255), (0,0,102), (255.255,102), (104,255,255), (153,34,153), (153,153.0), (153,255,153))

7. What color depth? How are color depth and file size related?

8. What is the color depth if the picture uses 65536 colors? 256 colors? 16 colors? (Answer: 16 bits; 8 bits; 4 bits)

9. For yellow, find the red, green, and blue components in 12-bit encoding. (Answer: R=G=15, B=0)

10. How much space does a palette take up in a file that uses 64 colors? 128 colors?

11. How many bytes will a 40×50 pixel image code take in true color mode? when encoding with a palette of 256 colors? when encoding with a palette of 16 colors? in black and white (two colors)? (Answer: 6000, 2000, 1000, 250)

12. How many bytes will an 80x100 pixel image code take in 12 bits per pixel encoding? (Answer: 12000)

13. To store a 32×32 pixel bitmap, 512 bytes of memory were allocated. What is the maximum possible number of colors in an image's palette? (Answer: 16)

14. To store a bitmap image with a size of 128 x 128 pixels, 4 kilobytes of memory were allocated. What is the maximum possible number of colors in an image's palette? (Answer: 4)

15. In the process of converting a raster graphic file, the number of colors decreased from 1024 to 32. How many times has the information volume of the file decreased? (Answer: 2 times)

16. In the process of converting a raster graphic file, the number of colors decreased from 512 to 8. How many times did the information volume of the file decrease? (Answer: 3 times)

17. Monitor screen resolution - 1024 x 768 pixels, color depth - 16 bits. What is the required amount of video memory for this graphics mode? (Answer: 1.5 MB)

18. After converting a bitmap 256-color graphic file to black and white format (2 colors), its size has decreased by 70 bytes. What was the size of the original file? (Answer: 80 bytes)

19. How much memory is needed to store a 64-color, 32-by-128-dot bitmap graphics image? (Answer: 3 KB)

20. What is the width (in pixels) of a rectangular 64-color unpacked bitmap that occupies 1.5 MB on disk if its height is half its width? (Answer: 2048)

21. What is the width (in pixels) of a rectangular 16-color unpacked bitmap that occupies 1 MB on disk if its height is twice its width? (Answer: 1024)

What if the drawing is in color? In this case, one bit is no longer enough to encode the color of a pixel. For example, in the image of the Russian flag shown in the figure, there are 4 colors: black, blue, red and white. It takes 2 bits to encode one of the four options, so the code for each color (and the code for each pixel) will consist of two bits. Let 00 be black, 01 red, 10 blue, and 11 white. Then we get the following table:

The only problem is that when displaying on the screen, you need to somehow determine which color corresponds to one or another code. That is, color information must be expressed as a number (or a set of numbers).

Man perceives light as a set of electromagnetic waves. A certain wavelength corresponds to a certain color. For example, waves with a length of 500 × 565 m are green. So-called "white" light is actually a mixture of wavelengths that cover the entire visible range.

In the RBG model, the brightness of each component

(or, as they say, each channel) most often

encoded as an integer from 0 to 255. In this case,

color code is a triple of numbers (R, G, B), brightness

individual channels. Color (0,0,0) is black

color, and (255,255,255) is white. If all the components of Fig.5

have equal brightness, resulting in shades of gray, from black to white. Web page color coding also uses the RGB model, but the channel luminances are written in hexadecimal (0016 to FF16) and the color code is preceded by #. For example, red is coded as #FF0000 and blue as #0000FF.

Here are some color codes:

Table 1

In total there are 256 brightness options for each of the three colors. This makes it possible to encode 256 3 = 16,777,216 shades, which is more than enough for a human. Since 256 = 2 8 , each of the three components takes up 8 bits or 1 byte in memory, and all information about some color is 24 bits (or 3 bytes). This value is called color depth.

Color depth is the number of bits used to encode the color of a pixel. coding graphic information

24-bit color coding is often referred to as true color mode (English TrueColor - true color). To calculate the size of the picture in bytes with this encoding, you need to determine the total number of pixels (multiply the width and height) and multiply the result by 3, since the color of each pixel is encoded in three bytes. For example, a 20x30 pixel image encoded in truecolor mode would take up 20x30x3 = 1800 bytes. Of course, this does not take into account compression, which is used in all modern image file formats. Besides, in real files there is a header that contains service information (for example, the dimensions of the picture). In addition to the true color mode, 16-bit coding is also used (English HighColor - “high” color), when 5 bits are assigned to the red and blue components, and 6 bits to the green, to which the human eye is more sensitive. In HighColor mode, 216 = 65536 different colors can be encoded. In mobile phones, 12-bit color coding (4 bits per channel, 4096 colors). Generally, the fewer colors used, the more the color image will be distorted. Thus, when encoding color, there is also an inevitable loss of information that is "added" to the loss caused by sampling. However, as the number of colors used increases, the file size grows at the same time. For example, in true color mode, the file will be twice as large as with 12-bit encoding. Very often (for example, in diagrams, diagrams and drawings), the number of colors in the image is small (no more than 256). In this case, palette coding is used.

The color palette is a table in which each color specified as components in the RGB model is mapped to a numerical code.

Palette encoding is done like this:

  • select the number of colors N (as a rule, no more than 256);
  • from the true color palette (16,777,216 colors) choose any N colors and for each
  • from them we find the components in the RGB model;
  • Each of the colors is assigned a number (code) from 0 to N-1;
  • · compose a palette, first writing down the RGB components of the color with code 0, then the color components with code 1, etc.
  • · the color of each pixel is encoded not as RGB values, but as a color number in the palette.

For example, when encoding the image of the Russian flag (see above), 4 colors were chosen:

  • black: RGB code (0,0,0); binary code 002;
  • red: RGB code (255,0,0); binary code 012
  • blue: RGB code (0,0,255); binary code 102;
  • white: RGB code (255,255,255); binary code 112.

Therefore, the palette, which is usually written to a special service area at the beginning of the file, is four three-byte blocks:

The code of each pixel takes only two bits. To roughly estimate the volume of a picture with a palette that includes N colors (without compression), you need:

  • determine the size of the palette, 3×N bytes or 24×N bits;
  • · determine the color depth (number of bits per pixel), that is, find the smallest natural number k, such that 2 k ? N;
  • Calculate the total number of pixels M by multiplying the dimensions of the picture;
  • · to determine the information volume of the main part Mchk bits.

The table shows data on some coding options with a palette:

table 2

Palettes with more than 256 colors are not used in practice. RGB encoding best describes the color that is emitted by some device, such as a monitor or laptop screen. When we look at an image printed on paper, the situation is completely different. We see not the direct rays of the source entering the eye, but reflected from the surface. "White light" from some source (the sun, a light bulb), containing waves in the entire visible range, falls on the paper on which the paint is applied. The paint absorbs some of the rays (their energy is spent on heating), and the rest enter the eye, this is the color that we see.


For example, if paint absorbs red rays, only blue and green remain - we see blue. In this sense, red and blue colors complement each other, just like the green-violet and blue-yellow pairs.

Indeed, if you “subtract” green from a white color (its RGB code is #FFFFFF), then you get the color #FF00FF (purple, magenta), and if you “subtract” blue, you get the color #FFFF00 (yellow).

On three additional colors - blue,

purple and yellow - a color scheme is being built

CMY model (eng. Cyan - blue, Magenta -

purple, Yellow - yellow), which is applied

for printing. Fig.8

The values ​​C=M=Y=0 indicate that no ink is applied to white paper, so all rays are reflected, this is white. If you add blue, the red rays are absorbed, leaving only blue and green. If you apply more yellow paint on top, which absorbs blue rays, only green remains.

When applying blue, violet and yellow paints, theoretically, black should be obtained, all rays are absorbed. However, in practice, everything is not so simple. The colors are not perfect, so instead of black, it turns out dirty brown. In addition, when printing black areas, you have to “pour” a triple portion of ink into one place. It should also be borne in mind that usually black text is often printed on printers, and color inks are much more expensive than black ones.

To solve this problem, black is added to the ink set, this is the so-called key color (eng. Keycolor), so the resulting model is designated CMYK. The image that most printers print is made up of dots of these four colors that are very close together in a pattern. This creates the illusion that there are different colors in the drawing. In addition to the RGB and CMY (CMYK) color models, there are others. The most interesting of them is the HSB model (English Hue - tone, shade; Saturation - saturation, Brightness - brightness), which is closest to the natural perception of a person. Hue is, for example, blue, green, yellow. Saturation is the purity of a hue, reducing saturation to zero results in gray. Brightness determines how light or dark a color is. Any color turns black when brightness is reduced to zero.

Strictly speaking, a color encoded in RGB, CMYK and HSV models depends on the device on which this color will be displayed. To encode "absolute" color, the Lab model is used (English Lighntess - lightness, a and b - parameters that determine the tone and saturation of the color), which is an international standard. This model is used, for example, to convert colors from RGB to CMYK and vice versa.

Typically, images intended for printing are prepared on a computer (in RGB mode), and then converted to the CMYK color model. In this case, the task is to get the same color when printing as on the monitor. And this is where problems arise. The fact is that when displaying pixels on the screen, the monitor receives some numbers (RGB codes), on the basis of which it is necessary to “paint” the pixels in one color or another. An important conclusion follows from this.

The color we see on the monitor depends on the characteristics and settings of the monitor.

This means that, for example, red color (R=255, G=B=0) will be different on different monitors. Surely you have seen this effect in a store that sells TVs and monitors - the same picture looks different on each of them. What to do?

First, the monitor is calibrated - adjusting the brightness, contrast, white, black and gray colors. Secondly, professionals working with color images use the color profiles of monitors, scanners, printers and other devices. Profiles store information about which actual colors correspond to different RGB codes or CMYK codes. To create a profile, special devices are used - calibrators (colorimeters), which “measure” color using three sensors that receive rays in the red, green and blue ranges. Modern graphic file formats (for example, the Adobe Photoshop .PSD format), along with pixel codes, also contain the monitor profile on which the picture was created. In order for the result of printing on the printer to be as similar as possible to the image on the monitor, it is necessary (using the monitor profile) to determine the “absolute” color (for example, in the Lab model) that the user saw, and then (using the printer profile) to find the CMYK code , which will print the closest color.

The problem is that not all colors of the RGB model can be printed. First of all, this applies to bright and saturated colors. For example, a bright red color (R=255, G=B=0) cannot be printed, the color closest to it in the CMYK model (C=0, M=Y=255, K=0) when converted back to RGB can give values in the region R=237, G=28, B=26. Therefore, when bright colors are converted to CMYK (and when bright patterns are printed), they become dimmer. This must be taken into account by professional designers.

So, with raster encoding, the picture is divided into pixels (discretized). For each pixel, a single color is defined, which is most often encoded using an RGB code. In practice, these operations are performed by a scanner (an image input device) and a digital camera.

Raster encoding has the following advantages:

  • universal method (any image can be encoded);
  • · the only method for encoding and processing blurry images without;
  • clear boundaries, such as photographs.

Raster encoding has disadvantages:

  • When sampling, there is always a loss of information;
  • When resizing an image, the color and shape of objects in the picture are distorted, since when increasing the size, it is necessary to somehow restore the missing pixels, and when decreasing, replace several pixels with one;
  • The file size does not depend on the complexity of the image, but is determined only by the resolution and color depth;
  • As a rule, raster drawings have a large volume.

There are many different formats for raster drawings. The most common are the following:

BMP (eng. bitmap - bitmap, files with the extension .bmp) - a standard format in the operating Windows system; supports paletted and truecolor encoding;

JPEG (eng. JointPhotographicExpertsGroup - a joint group of expert photographers, files with the extension .jpg or .jpeg) - a format designed specifically for encoding photographs; only supports true color mode; to reduce the file size, strong compression is used, in which the image is slightly distorted, so it is not recommended to use it for drawings with clear boundaries;

GIF (eng. GraphicsInterchangeFormat - a format for exchanging images, files with the extension .gif) - a format that supports only coding with a palette (from 2 to 256 colors); unlike previous formats, parts of the picture can be transparent, that is, the background will “shine” through them on the web page; in the modern version of the GIF format, you can store animated images; lossless compression is used, that is, the image is not distorted during compression;

PNG (PortableNetworkGraphics - portable network images, files with the .png extension) is a format that supports both true color mode and paletted coding; parts of the image can be transparent and even translucent (32-bit RGBA encoding, where the fourth byte specifies transparency); the image is compressed without distortion; animation is not supported.

The properties of the considered formats are summarized in the table:

Table 3

You already know that all kinds of information are stored in the computer memory in the form of binary codes, that is, chains of zeros and ones. Having received such a chain, it is absolutely impossible to say that it is text, picture, sound or video. For example, the code 11001000 2 may represent the number 200, the letter "I", one of the color components of a pixel in true color mode, a palette color number for a pattern with a palette of 256 colors, a pixel color 8 for a black and white pattern, and the like. How does a computer understand binary data? First of all, you need to focus on the file name extension. For example, most often files with the .txt extension contain text, and files with the .bmp, .gif, .jpg, .png extensions contain pictures.

However, the file extension can be changed as you like. For example, you can do so text file will have the extension .bmp, and the picture in JPEG format- extension.txt. Therefore, at the beginning of all files of special formats (except for plain text, .txt), a header is written, by which you can "recognize" the type of file and its characteristics. For example, BMP files start with the characters "BM" and GIF files start with the characters "GIF". In addition, the title indicates the size of the picture and its characteristics, such as the number of colors in the palette, compression method, etc.

Using this information, the program "decrypts" the main part of the file and displays it on the screen.


Color palette
Visible radiation

24-bit color(which is a subset true color English "true color") in computer graphics - a method of representing and storing an image that allows you to display a large number of colors, halftones and shades. A color is represented using 256 levels for each of the three components of the RGB model: red(R), green(G), and blue(B), resulting in 16,777,216 (28+8+8) different colors.

32-bit TrueColor can store an alpha channel, which sets the degree of pixel transparency for displaying translucent images, such as translucent windows, fade-out menus, and shadows. Some video adapters are capable of processing the alpha channel in hardware.

Super-Truecolor

There are also systems (such as SGI) that allocate more than 8 bits per channel for color representation, such ways of representing image information are also commonly called TrueColor (for example, a 48-bit TrueColor scanner).

In cameras with a resolution of more than 8 bits per channel (usually 12, sometimes up to 22), the "full color" image is stored in the form of raw data (RAW).

Notes


Wikimedia Foundation. 2010 .

See what "TrueColor" is in other dictionaries:

    Truecolor- See also True Colors (disambiguation). Truecolor is a method of representing and storing graphical image information (especially in computer processing) in an RGB color space such that a very large number of colors, shades, and hues can be… … Wikipedia

    Color depth bit image 8 bit gray scale 8 bit color 15/16 bit: Highcolor 24 bit: Truecolor 30/36/48 bit: Deep Color

    Truecolor- Le terme Truecolor désigne les méthodes de représentation et de stockage d information d image dans un espace colorimétrique RVB telles qu un très grand nombre de couleurs, de nuances et des teintes peuvent être affichées dans une image, comme… … Wikipédia en Français

    true color- True Color (engl. für Echtfarben) ist ein Begriff aus der Computertechnik (Grafikkarten) und bezeichnet eine Farbtiefe von 24 Bit (3×8 Bit, entspricht 224 ≈ 16.78 Millionen Farben). Bilder dieser Farbtiefe erwecken beim menschlichen Betrachter… … Deutsch Wikipedia

    true color- ● en /trou ko lor/adj. GRAPH Se dit d un dispositif de restitution qui permet d afficher plus de couleurs que ne peut en distinguer l œil humain, qui ne voit donc plus la différence entre les vraies couleurs et les couleurs d une photo. Aussi… … Dictionnaire d "informatique francophone

    color depth- 1 bit monochrome 8 bit grayscale 8 bit color 15/16 bit color (High color) 24 bit color (True color) 30/36/48 bit color (Deep color) Related Indexed color Palette RGB color model Web safe color This box … Wikipedia

    Portable Network Graphics- PNG A PNG image with an 8 bit transparency channel (top). The same image is overlaid onto a checkered background (botto … Wikipedia

    Bildspeicher

    Bildwiederholspeicher- Der Bildspeicher bzw. Framebuffer (engl. frame - Einzelbild, buffer - Puffer) ist Teil des Video RAM von Computern und entspricht einer digitalen Kopie des Monitorbildes. Das heißt, jedem Bildschirmpixel kann genau ein bestimmter Bereich des… … Deutsch Wikipedia

    frame buffer- Der Bildspeicher bzw. Framebuffer (engl. frame - Einzelbild, buffer - Puffer) ist Teil des Video RAM von Computern und entspricht einer digitalen Kopie des Monitorbildes. Das heißt, jedem Bildschirmpixel kann genau ein bestimmter Bereich des… … Deutsch Wikipedia

Books

  • Russian language. We hand over the Unified State Exam 2014 (CDpc), Dunaeva L. A., Rudenko-Morgun O. I., Shchegoleva A. E., Kedrova G. E., Streltsova T. E.. Complex "1 C: Tutor. Russian language. We hand over USE 2014" was developed to support the unified state exam conducted by the Ministry of Education and Science of the Russian Federation and…

What difficulties did you have? How can they be overcome?

2. Plot an 8 pixel wide black and white drawing encoded with the hexadecimal sequence 2466FF6624 16 .

3. Plot a black and white drawing 5 pixels wide, encoded with the hexadecimal sequence 3A53F88 16 .

4. A 10×15 cm drawing is encoded with a resolution of 300 ppi. Estimate the number of pixels in this drawing. (Answer: about 2 megapixels)

5. Construct a hex code for colors that have RGB codes (100,200,200), (30,50,200), (60,180, 20), (220, 150, 30). (Answer: #64C8C8, #1E32C8, #3CB414, #DC961E)

6. How would you name a color specified on a web page as a code: #CCCCCC, #FFCCCC, #CCCCFF, #000066, #FF66FF, #CCFFFF, #992299, #999900, #99FF99? Find the decimal values ​​of the components of the RGB code. (Answer: (204,204,204), (255,204,204), (204,204,255), (0,0,102), (255.255,102), (104,255,255), (153,34,153), (153,153.0), (153,255,153))

7. What is color depth? How are color depth and file size related?

8. What is the color depth if the picture uses 65536 colors? 256 colors? 16 colors? (Answer: 16 bits; 8 bits; 4 bits)

9. For yellow, find the red, green, and blue components in 12-bit encoding. (Answer: R=G=15, B=0)

10. How much space does a palette take up in a file that uses 64 colors? 128 colors?

11. How many bytes will a 40×50 pixel image code take in true color mode? when encoding with a palette of 256 colors? when encoding with a palette of 16 colors? in black and white (two colors)? (Answer: 6000, 2000, 1000, 250)

12. How many bytes will an 80x100 pixel image code take in 12 bits per pixel encoding? (Answer: 12000)

13. To store a 32×32 pixel bitmap, 512 bytes of memory were allocated. What is the maximum possible number of colors in an image's palette? (Answer: 16)

14. To store a bitmap image with a size of 128 x 128 pixels, 4 kilobytes of memory were allocated. What is the maximum possible number of colors in an image's palette? (Answer: 4)

15. In the process of converting a raster graphic file, the number of colors decreased from 1024 to 32. How many times has the information volume of the file decreased? (Answer: 2 times)

16. In the process of converting a raster graphic file, the number of colors decreased from 512 to 8. How many times did the information volume of the file decrease? (Answer: 3 times)

17. Monitor screen resolution - 1024 x 768 pixels, color depth - 16 bits. What is the required amount of video memory for this graphics mode? (Answer: 1.5 MB)

18. After converting a bitmap 256-color graphic file to black and white format (2 colors), its size has decreased by 70 bytes. What was the size of the original file? (Answer: 80 bytes)

19. How much memory is needed to store a 64-color, 32-by-128-dot bitmap graphics image? (Answer: 3 KB)

20. What is the width (in pixels) of a rectangular 64-color unpacked bitmap that occupies 1.5 MB on disk if its height is half its width? (Answer: 2048)

21. What is the width (in pixels) of a rectangular 16-color unpacked bitmap that occupies 1 MB on disk if its height is twice its width? (Answer: 1024)

Rasters, pixels, sampling, resolution

Like all kinds of information, images in a computer are encoded as binary sequences. Two fundamentally different coding methods are used, each of which has its own advantages and disadvantages.

Both a line and an area are made up of an infinite number of points. We need to encode the color of each of these points. If there are infinitely many of them, we immediately conclude that this requires an infinite amount of memory. Therefore, the image cannot be encoded in a “dot-by-point” way. However, this idea can still be used.

Let's start with a black and white drawing. Imagine that a grid is superimposed on the image of a rhombus, which divides it into squares. Such a grid is called a raster. Now for each square we define a color (black or white). For those squares in which a part turned out to be painted in black, and a part in white, we will choose a color depending on which part (black or white) is larger.

Picture 1.

We have a so-called raster drawing, consisting of pixel squares.

Definition 1

Pixel(eng. pixel = picture element, picture element) is the smallest picture element for which you can set your own color. Having divided the “ordinary” drawing into squares, we performed its discretization - we divided a single object into separate elements. Indeed, we had a single and indivisible drawing - the image of a rhombus. As a result, we got a discrete object - a set of pixels.

The binary code for a black and white picture obtained as a result of sampling can be constructed as follows:

  • we replace white pixels with zeros, and black pixels with ones;
  • write out the rows of the resulting table one by one.

Example 1

Let's show this with a simple example:

Figure 2.

The width of this picture is $8$ pixels, so each line of the table consists of $8$ bits. In order not to write a very long chain of zeros and ones, it is convenient to use the hexadecimal number system, encoding $4$ neighboring bits (tetrad) with one hexadecimal digit.

Figure 3

For example, for the first line we get the code $1A_(16)$:

and for the whole drawing: $1A2642FF425A5A7E_(16)$.

Remark 1

It is very important to understand what we have gained and what we have lost as a result of discretization. Most importantly, we were able to encode the drawing in binary. However, the picture was distorted - instead of a rhombus, we got a set of squares. The reason for the distortion is that in some squares, parts of the original image were filled with different colors, and in the encoded image, each pixel necessarily has one color. Thus, some of the original information was lost during encoding. This will manifest itself, for example, when the picture is enlarged - the squares increase, and the picture is even more distorted. To reduce information loss, you need to reduce the pixel size, that is, increase the resolution.

Definition 2

Permission is the number of pixels per inch of image size.

Resolution is usually measured in pixels per inch (the English notation $ppi$ = pixels per inch is used). For example, a resolution of $254$ $ppi$ means that there are $254$ pixels per inch ($25.4$ mm), so each pixel "contains" a $0.1×0.1$ mm square of the original image. The higher the resolution, the more accurately the picture is encoded (less information is lost), but at the same time the file size also grows.

Color coding

What if the drawing is in color? In this case, one bit is no longer enough to encode the color of a pixel. For example, in the image of the Russian flag $4$ shown in the figure, the colors are black, blue, red and white. To encode one of the four options, $2$ bits are needed, so the code for each color (and the code for each pixel) will consist of two bits. Let $00$ be black, $01$ be red, $10$ be blue, and $11$ be white. Then we get the following table:

Figure 4

The only problem is that when displaying on the screen, you need to somehow determine which color corresponds to one or another code. That is, color information must be expressed as a number (or a set of numbers).

Man perceives light as a set of electromagnetic waves. A certain wavelength corresponds to a certain color. For example, wavelengths of $500-565$ nm are green. So-called "white" light is actually a mixture of wavelengths that cover the entire visible range.

According to the modern concept of color vision (Jung-Helmholtz theory), the human eye contains three types of sensitive elements. Each of them perceives the entire stream of light, but the first are most sensitive in the red region, the second in the green region, and the third in the blue region. Color is the result of excitation of all three types of receptors. Therefore, it is believed that any color (that is, the sensations of a person who perceives waves of a certain wavelength) can be imitated using only three light beams (red, green and blue) of different brightness. Therefore, any color is approximately decomposed into three components - red, green and blue. By changing the strength of these components, you can make any color. This color model is called RGB after the initial letters of the English words red (red), green (green) and blue (blue).

In the RBG model, the brightness of each component (or, as they say, each channel) is most often encoded by an integer from $0$ to $255$. In this case, the color code is a triple of numbers (R, G, B), the brightness of individual channels. Color ($0,0,0$) is black and ($255,255,255$) is white. If all components are of equal brightness, shades of gray are obtained, from black to white.

Figure 5

To make a light red (pink) color, you need to increase the brightness of the green and blue channels equally in red ($255.0.0$), for example, the color ($255, $150, $150) is pink. Uniform decrease in the brightness of all channels makes a dark color, for example, the color with the code ($100.0.0$) is dark red.

In total, there are $256$ brightness options for each of the three colors. This allows $256^3= 16,777,216$ shades to be encoded, which is more than enough for a human. Since $256 = 2^8$, each of the three components occupies $8$ bits or $1$ bytes in memory, and all information about some color is $24$ bits (or $3$ bytes). This value is called color depth.

Definition 3

Color depth is the number of bits used to encode the color of a pixel.

$24$-bit color coding is often referred to as True Color mode. To calculate the size of the picture in bytes with this encoding, you need to determine the total number of pixels (multiply the width and height) and multiply the result by $3$, since the color of each pixel is encoded in three bytes. For example, a picture of $20x30$ pixels encoded in truecolor mode would take $20x30x3 = 1800$ bytes.

In addition to the true color mode, $16$-bit coding (High Color) is also used, when $5$ bits are assigned to the red and blue components, and $6$ bits to the green component, to which the human eye is more sensitive. In High Color mode, $2^(16) = 65,536$ different colors can be encoded. AT mobile phones$12$-bit color coding ($4$ bits per channel, $4096$ colors).

Palette coding

Generally, the fewer colors used, the more the color image will be distorted. Thus, when encoding color, there is also an inevitable loss of information that is "added" to the loss caused by sampling. Very often (for example, in schemes, diagrams and drawings) the number of colors in the image is small (no more than $256$). In this case, palette coding is used.

Definition 4

Color palette is a table in which each color specified as components in the RGB model is assigned a numerical code.

Palette encoding is done like this:

  • select the number of colors $N$ (usually not more than $256$);
  • choose any $N$ colors from the true color palette ($16 777 216$ colors) and find components in the RGB model for each of them;
  • each color is assigned a number (code) from $0$ to $N–1$;
  • we compose the palette, first writing the RGB components of the color with the $0$ code, then the color components with the $1$ code, and so on.

The color of each pixel is encoded not as RGB values, but as a color number in the palette. For example, when encoding the image of the Russian flag (see above), $4$ colors were chosen:

  • black: RGB code ($0.0.0$); binary code $002$;
  • red: RGB code ($255.0.0$); binary code $012$;
  • blue: RGB code ($0.0.255); binary $102$;
  • white: RGB code ($255,255,255); binary code $112$.

Therefore, the palette, which is usually written to a special service area at the beginning of the file (it is called the file header), is four three-byte blocks:

Figure 6

The code of each pixel takes only two bits.

Palettes with more than $256$ of colors are not used in practice.

Advantages and disadvantages of raster coding

Raster encoding has dignity:

  • universal method (any image can be encoded);
  • the only method for encoding and processing blurry images that do not have clear boundaries, such as photographs.

And limitations:

  • when discretizing, there is always a loss of information;
  • when resizing the image, the color and shape of the objects in the picture are distorted, since when increasing the size, it is necessary to somehow restore the missing pixels, and when decreasing, replace several pixels with one;
  • the file size does not depend on the complexity of the image, but is determined only by the resolution and color depth.

As a rule, raster drawings have a large volume.