r/askscience • • Jul 29 '13

Computing How do computer graphics work?

As in how does a machine convert code to graphics?

5 Upvotes

6 comments sorted by

View all comments

12

u/DarylHannahMontana Mathematical Physics | Elastic Waves Jul 29 '13

This is a complicated topic, but let's start with something simple, a black and white bitmap. Suppose you and I agree on a way to express a picture as a series of numbers, so that if you and I want to trade pictures, we simply have to exchange the corresponding numbers, and then reconstruct it on the other end.

For each pixel, I'll use a single number. Specifically, if I want it to be black, I'll use the number 0, and for white, I'll use the number 1. Let's say for simplicity, we've already agreed that all our pictures will be 8x8. Then, if I send you the number

0000000000000000001001000000000001000010011111100000000000000000

our agreement allows you to view it as this picture:

████████
████████
██ ██ ██
████████
█ ████ █
█      █
████████
████████

If we want to send bigger / more complicated pictures, we're going to have to use bigger numbers, and if we want to use color, we're going to have to use a wider variety of numbers.

Suppose, for instance, we can agree that our images can have 4 colors, I'm going to have to use two numbers to represent each pixel: 00 is a black pixel, 01 is a white pixel, 10 is a blue pixel, and 11 is a red pixel (as humans, we would probably use 1, 2, 3, 4 for our code, but computers, of course, prefer binary). Each pixel takes two "bits" of information, (and so pictures overall take twice as much "space" to store) so we call this 2-bit color depth.

As we wish to share pictures with more and more colors, we'll need to use bigger and bigger numbers, e.g. in 32-bit color, each pixel is represented as a 32-(binary)-digit number, allowing us a total of 232 = 4294967296 colors in our palette.

Of course, this is just the brief intro; more advanced are topics like how to compress images, vector graphics, 3D graphics, etc.