0

I decided I'd attempt an image compression (From pixel RGBs) idea I had for a bit of extra credit in class. I've finished, but I find the levels of compression I get varies HUGELY from image to image. With this image, I'm getting a file size 1.25x the size of the coresponding PNG. With this image however, I'm getting a file size 22.5x the size of the PNG.

My compression works by first assigning each color in the image with an int(starting from 0), then using that int rather than the actual color in the file. The file is formatted as:

0*qj8c1*50i2p2*pg93*9zlds4*2rk5*4ok4r6*8mv1w7*2r25l8*3m89o9*9yp7c10*111*2clz112*g1j13*2w34z14*auq15*3zhg616*mmhc17*5lgsi18*25lw919*7ip84+0!0!0!0!0!0!0!0!0!0!0!0!0!1!1!1!@2!2!2!2!2!2!2!2!2!2!2!2!2!3!3!3!@4!4!4!4!4!4!4!4!4!4!4!4!4!5!5!5!@6!6!6!6!6!6!6!6!6!6!6!6!6!0!0!0!@3!3!3!3!3!3!3!3!3!3!3!3!3!2!2!2!@1!1!1!1!1!1!1!1!1!1!1!1!1!4!4!4!@7!7!7!7!7!7!7!7!7!7!7!7!7!6!6!6!@5!5!5!5!5!5!5!5!5!5!5!5!5!8!8!8!@9!9!9!9!9!9!9!9!9!9!9!9!9!10!10!10!@8!8!8!8!8!8!8!8!8!8!8!8!8!11!11!11!@12!12!12!12!12!12!12!12!12!12!12!12!12!7!7!7!@13!13!13!13!13!13!13!13!13!13!13!13!13!14!14!14!@15!15!15!15!15!15!15!15!15!15!15!15!15!16!16!16!@17!17!17!17!17!17!17!17!17!17!17!17!17!13!13!13!@18!18!18!18!18!18!18!18!18!18!18!18!18!15!15!15!@10!10!10!10!10!10!10!10!10!10!10!10!10!19!19!19!@

Where the first bit (With the *s and base36 numbers) is the dictionary defining the colors, and the second part seperated by !s is the actual image.

Why am I seeing the level of compression vary so hugely image from image? Is there a flaw in my compression algorithm?

Edit: The fact that the actual level of compression is poor compared to JPEG or PNG isn't an issue, I wasn't expecting to rival any major formats.

Thanks

ajb
  • 31,309
  • 3
  • 58
  • 84
Nathan
  • 1,287
  • 6
  • 15
  • 32
  • Are there consistent differences in the types of images that get the different results? At first sight, I would expect camera images to use a lot of colors, with subtle shading, but some painted images to use relatively few colors in big blocks. – Patricia Shanahan Jun 03 '14 at 20:16
  • I just tested a simple black and white image and I got over 8x the size, so it sounds like you're correct. Why does my algorithm work better with detailed images? I thought having all the colors being represented by 1 character(1 byte) would be better than getting up to 2 or 3 bytes per pixel... – Nathan Jun 03 '14 at 20:20
  • Actually, I was guessing the opposite - more compression with fewer colors. However, any consistent behavior gives you an opening to investigate what is happening. – Patricia Shanahan Jun 03 '14 at 20:21
  • One possibility is that e.g. png is doing particularly well on the simpler images. You might try comparing to full bitmap size. – Patricia Shanahan Jun 03 '14 at 20:23
  • Look at (the colour statistics of) the image itself. If most pixels have the same colour, PNG does pretty well. Compression in general does to some degree _correlation/patterns_ (neighbours of a red pixel are likely to be red) and _statistics_ and _compact data representations_ (most frequent colour index using less bits). – Joop Eggen Jun 03 '14 at 20:24
  • Just tried a BMP and I'm getting smaller files, which is definetly good! And @JoopEggen ahh I see, I havn't taken patterns or statistics or counting consecutive pixels into account, it sounds like it could be implemented easily though. Instead of `0!0!0!0!0!0!0!0!0!` we could have `9*0!` – Nathan Jun 03 '14 at 20:28

0 Answers0