Saturday, 20 March 2010

Colours, files and compression

I was commenting the other day at Elemental, my dear about Just Jill's photographs and I wondered what file formats Jill used when editing her fine images.

I humbly realised that many people won't know about file formats, compression, or even colour so I thought I'd do a serious post for a change.

I am no photography expert by any means, but I've done enough image processing to know it's a potential minefield.

I worked for a time using extremely low level fluorescence microscopy with a camera cooled by liquid nitrogen to minimise thermal effects. I was shut in the dark, in a tiny black painted room with a double door 'airlock' to keep out all stray light, and I sat there for hours listening to the radio, taking maybe 15 min exposures when all I could see was a faint, red glow from the laser illuminating the sample. Later I'd try to pick out the faintest of faint detail in the images. It became a skill, but limited to that type of image. However I had to know what the information in the image meant, in computer terms, so I could manipulate it without losing anything or, even worse, introducing an artefact that was not there in the original data.

This brings me to colour depth and file formats. Not all file formats are the same, some are 'lossy' meaning they really do lose data when you save your image. When you open the file again it 'recreates' the original as best as it can, but it is not the same. JPEG is the prime example and a good analogy is with mp3 audio files. Mp3 tricks the ear by removing things the brain won't miss using a psychoacoustic model. JPEG tricks the eye with strange, Fourier transform-like mathematics, which is very efficient in saving space but rarely unfolds back to an exact copy. I'll show examples of this later and, because you have a computer, I bet you've seen these artefacts before.

For these examples I'm using an old, free version of Paint Shop Pro, because it's basic, simple, it works and I like it, and I'm using 24 bit colour. Now, what does that mean? Think of the three primary colours red, green and blue (RGB), all bright and saturated. Now mix them together remembering this is coloured light, NOT paint. You can make yellow, cyan, magenta and white. You can make black too, just turn the lights off.

24 bit colour assigns 8 bits to each primary colour, and in binary 28 = 256. This gives 256 brightness levels to each colour with zero being off/none/black and 255 being saturated. Think R,G,B (a triplet of numbers separated by commas) then bright red = 255,0,0. Similarly green = 0,255,0 and blue = 0,0,255. Black is 0,0,0.

Red + green gives yellow = 255,255,0 and I can make any darker yellow by mixing lower, but equal amounts, of red and green, this is 200, 200,0.

How to get a paler yellow? Well, 255,255,255 = white so you have to add some blue. The background colour on this page is 255,255,238.

When red=green=blue you get a grey. This = 200,200,200. Note there can only be 256 greys in a greyscale image including black and white.

How many colours in total?
256 x 256 x 256 = 28 x 28 x 28 = 224 = 16,777,216

And that is what we get unless you spend a lot of money on a dedicated Macintosh or Silicon Graphics workstation, then you can get to 30 bit colour which gives 210 (1024) shades to each primary colour for a total of more than a billion colours. I doubt the human eye can discriminate so finely but scientific data is different. If you see it advertised as 32 bit colour it's bullshit, it will be 24 or 30 bit with layers and transparency.

So how do we save over 16 million colours in a file? Think of a tiny 100 by 100 pixel image; that's 10,000 pixels, every one with 8 bits of data each for red, green and blue. 8 bits = 1 byte so it takes 3 bytes per pixel to store the colour data, or 30 kilobytes for the file. Then there will be some identification tag so the system knows WTF the file is, and basic stuff like height and width. I just saved a 100x100 px pure white image in different formats and Windows reports the file sizes to be:

Windows bitmap (*.bmp) = 29.3 KB
About what I predicted if you strictly save all 24 bits of data.

JPEG (*.jpg) = 823 bytes
Quite a saving! Plus it displays perfectly, not a blemish. This is not always the case with JPEGs though as I will show.

PNG (*.png) = 289 bytes
The compression of PNG can be amazing. In this case it will just record the fact that "everything's white" and that's all you need. PNG will however faithfully recreate the correct 24 bit RGB colour data for every pixel. The payback is that the file sizes soon exceed JPEG for complicated images.

TIFF (*.tif=1.31 KB) and TARGA (*.tga=418 bytes) both have good compression and again will keep full 24 bit colour.

GIF (*.gif) only has 256 colours in a defined pallet and you can't mix them. It's not a fair comparison (so I won't), but GIF is good for greyscale because there are only 256 shades of grey you can display (8 bits). Having said that, be careful with converting to GIF from 24 bits as there are many ways to interpret the darkness or shade of a colour. GIF is good for drawings, cartoons, charts etc which have a strictly limited number of colours. GIF also supports simple, frame to frame, animations and was the internet favourite before the full colour Flash.

To continue I'll concentrate on PNG and JEPG because both are 24 bit and fully internet supported. I'll show the differences by doing three edits of an image and I have chosen the colours deliberately because I know it will f**k up JPEG. The same will be happening in any image you repeatedly edit, you just may not be able to visually discriminate it.

I started a new image and made a smooth colour gradient from red (255,0,0) to blue (0,0,255) and then drew a green (0,255,0) line. Then I saved it as a PNG (left). The same colour data was still on the screen so I saved a copy as JPEG (right), just using the default settings. Then I closed my original image and opened the two new ones. Here they are!

Look carefully at the JPEG image on the right. There are clearly colour distortions. If you don't believe me, save them both and blow them up yourself (or, if you use Firefox, then right click on an image, hold down the Ctrl key and left click 'View Image', on the new tab hold down Ctrl again and spin the mousewheel to zoom).

My three edits will only add three words of white text to each image. Nothing fancy, and with no antialias so the edges of the text should be blocky and sharp. The PNG I will save as PNG, the JPEG I'll save as JPEG. Each time I add text I will save the file, close it and reopen it for the next edit.

First edit

If you can't see the distortions of the JPEG by now get a better monitor or spectacles.

Second edit

Third edit

And all I did was open-edit-save-close, open-edit-save-close... Look at the white text in the final JPEG, its not all white! But it's roughly acceptable to the eye for a blog post. This happens subtly to all colour boundaries in JPEG so it's not good for photography; you need to preserve exactly what you have on the screen when you save and JPEG will not do that. If this were an mp3 you would be moaning about scratchy, poppy distorted sound, but our eyes are more forgiving. Beware!

I know most digital cameras spit out JPEGs, and this is to conserve memory, but you may have an option for some kind of 'raw' format which, though it will absorb all the camera's memory in just a few shots, will give a bitmap (*.bmp) in some way. Save and edit that and then, finally, make your masterpiece into a cheap and nasty JPEG for the punters.

If you have to start with a JPEG then save a copy as PNG and work with that. Do the edits, and the last thing is to save a final copy as a JPEG again. Look at the file sizes though, the tradeoff between quality and file size may sometimes make PNG the better file to post.

I hope some of this makes sense. I've been up all night, the birds are singing outside, it's been raining and one of my cats just came in soaking wet. She jumped on my lap and gave a shake of drips everywhere. Thank you Titch but the mackerel for breakfast is mine. Goodnight/day to all :)


jill said...

Oh my! Thank you so much for doing this Andy. I see what you are saying, now whether or not I can apply this when editing my photos is a totally other story. I'll play around with it and let you know.

Clarification on something...I was an art major so I know the three primary colors are red, yellow and blue, but your example replaced yellow with green. Please explain. Thanks!

Andy Holroyd said...

I thought primary colors would come up, and it’s quite hard to get your head round. We're both right, and an explanation is worth a full post to itself. I’ll try to knock something together in the next day or two.

Simple answer: Paint/dye/ink doesn’t shine, or emit light, it selectively absorbs or reflects the light that’s shining on it.

lisleman said...

thanks for that very informative post. I thought jpeg and png were pretty much the same but now I know differently. I have saved all my pictures in jpeg but now I'll pay more attention to editting and using png.

Just checking but do the distortions look like bubbles?

Andy Holroyd said...

The 'bubbles' are just part of it lisleman. Note also the black outline around the green and the colours bleeding into the white text.

beth said... all seems too complicated for me !....and I was starting to believe that I really knew what I was doing.....

Andy Holroyd said...

Hi Beth - don't worry about it. JPEGs are fine for complicated images like photographs. The detail almost always masks the distortions and if it looks pleasing to the eye, it's good. Just be careful if you do lots and lots of re-edits.