1

I implemented the staff's solution to compress a bmp by a factor say 0.5 and compared the compressed version with the original bmp using xxd command. I expected it copy just half of rgb triples from the original but its not so. Although the number of rgb triples in compressed file are half in length and breadth of the original but i can't understand how values of those rgb triples in compressed file are calculated from orginal.

for eg. original file

123456

123456

with resize factor of 0.5 result expected is

123

but its not so.WHY?

1 Answer 1

4

The spec says "think about what it means to resize with a value between 0 and 1."

Your example:

123456
123456

to end up as

123

is really a crop rather than resize.

The staff version does this, using smiley.bmp as an example, where W = white (255,255,255) and R = Red (255,0,0):

 W W R R R R W W 
 W R W W W W W R
 R W R W W R W R
 R W W W W W W R
 R W R W W R W R
 R W W R R W W R
 W R W W W W W R
 W W R R R R W W

 W R R W
 R R W W
 R R W W
 W W W R

So it

  • reads line 1, writes bytes 1, 3, 5, 7
  • skips line 2
  • reads line 3, writes bytes 1, 3, 5, 7
  • skips line 4 etc.

That way, by skipping every other pixel, it is reducing the height by 2 and the width by 2 and what started out as an 8x8 bmp ends up a 4x4 bmp.

That's the simplest way to "compress" a bitmap, but it gives you a pretty horrible result. There are other methods, like bilinear interpolation, where you would average the pixels that you are replacing.

In the smiley.bmp example:

 W W   R R   R R   W W 
 W R   W W   W W   W R

 R W   R W   W R   W R
 R W   W W   W W   W R

 R W   R W   W R   W R
 R W   W R   R W   W R

 W R   W W   W W   W R
 W W   R R   R R   W W

Then, you would average each of those groups of 4 to get a new pixel value, and then write it out.

So let's pick the first one, using decimal rather than hex:

( [255,255,255] + [255,255,255] + [255,255,255] + [255,255,0] ) / 4
= [255,255,191]  Light pink

If you follow that all the way through, you end up with a smiley face that, rather than being white and red, is lightpink and dark pink. But the 4x4 bmp will still resemble a smiley face.

Perhaps it is best illustrated with an image. This is a (screenshot) of a 256x256 bitmap and 2 "resized by 0.25" versions, the top one using the averaging method I described above and the bottom one using the Hacker version:

256 bitmap example resized 2 ways

Just to illustrate, I then resized each of them back up to 256x256 using my pset resize program so the difference is clearer to see.

64x64s resized back up to original size

(the image used is a standard test image as described in this Wikipedia page)

Hope this helps! -Brenda.

1
  • That's true. I was honestly expecting that you would 'average' the pixels around the lines, though, not just ignore some. I'm a bit surprised, as I thought that this would be too small a challenge. Still, I haven't done Hacker Ed, so what do I know? @curiouskiwi
    – ChiCubed
    Jul 21, 2014 at 4:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .