>From: Ian Wilkinson <
KILLspamwilkoKILLspam
SGTWILKO.F9.CO.UK>
>Reply-To: pic microcontroller discussion list <
RemoveMEPICLISTTakeThisOuT
MITVMA.MIT.EDU>
>To:
spamBeGonePICLISTspamBeGone
MITVMA.MIT.EDU
>Subject: Re: Image Compression Algorithm
>Date: Tue, 25 Apr 2000 00:54:24 +0100
>
>On Mon, 24 Apr 2000, Andrew Seddon wrote:
> >Hi, I was wondering if one of you PIC programming gurus could help me. I
> >realise it`s a pretty simple problem but I can`t seem to crack it. I have
> >written a few different algorithms that should work but all seem to
>produce
> >errors. Anyway here`s the problem. What I need to do is compress a B/W
> >image. I am planning to write it into memory as a sequence of 8 bit
>numbers
> >each representing a number of black and white pixels alternating. I.e
> >
> >10 250 0 250 15 17
> >
> >which would mean 10 white, 500 black, 15 white, 17 black.
> >
> >This method was suggested a while back and it seems like a good idea. I
>am
> >writing in C2C so some sort of C pseudo code would be really helpfull.
> >
> >BTW, my memory access is sequential so I can`t use any complicated
>algo`s. I
> >thought maybe some sort of counter and a variable that keeps track of
>black
> >and white.
> >
>
>That seems over complex.
>Have you thought instead how about 8 bit numbers, first bit of each
>represents
>the colour, then the rest is a 7bit representation of how many pixels are
>that
>colour.
>
>So black=0, white=1
>image has 3 black 10 white 1 black 250 white, 50 black would give this:
>00000011 {3 black}
>10001010 {10 white}
>00000001 {1 black}
>11111111 {127 white}
>11111011 {123 white, making this row total 250}
>00110010 {50 black}
>
>This is more efficient than your suggested method, as you are only marking
>changes and sections longer than 127 pixels, and not having a byte user
>even if
>the colours did not change.
>
>Assuming you are reading in the pixel colours on by one...
>pseudo code would be something like this...
>lastcol:=2
>count:=0
>do
> col:=getCol()
> if ((col==lastcol) and (count<127)) then
> count++
> else
> if (lastcol<2) then output (count or (128 and col))
> count:=1
> lastcol:=col
> endif
>loop until eof
>
>This should be easy to follow except maybe the
>if lastcol<2 then output (count or (128 and col))
>This line will output (what ever your output device is, memory, disk,
>serial
>port what ever, it does not matter), the count, now if the colour is white
>(col=1) then this part (128 and col) will equal 128 as a boolean and of 128
>and
>1 should always equal 128, if the colour is black (col=0) then 128 and 0 is
>0.
>So this part, count or (128 and col), says add 128 to count if col=1 as a
>boolean or of count and 128 will result in adding them.
>
>You could just add the numbers, but I like boolean ops...
>
>This should be easy to translate into code.
>
>Just like your code this could actually make the image file bigger under
>some
>instances, but I think it's more likely to produce better compression than
>yours. Also worth looking at is a GIF like compression that makes symbols
>from
>the image and then matches the symbols to the rest of the image. It does
>work,
>I have the spec here somewhere and built my own compressor from it.
>
>Ian.
>--
>Rule of Defactualization:
>Information deteriorates upward through bureaucracies.
>
>Uptime at 12:54am up 3 days, 4:05, 2 users,