1. Lightning-Fast Bitmap Cropping

I'm coding like hell right now, and I had to come up with a way to crop (ie.
clip) a standard Euphoria bitmap.
I wrote a routine that I think is lightning-fast.
I has only being tested on a 640*22 bitmap, so maybe it's slow on larger
bitmaps (???).
If you don't know what cropping is, it's cutting out a rectangular area from
a bitmap, and saving that area.
You can use this to perform clipping aswell.
Here it is, try it out with Euphoria 2.0+, and tell me if it explodes your
keyboard;

global function crop_image(sequence bmp,sequence rect)
sequence ret
integer y
ret = repeat(repeat(0,rect[3]-rect[1]),rect[4]-rect[2])
y = 1

for i = rect[2] to rect[4]-1 do -- To the height of the cropping rect
    ret[y][1..rect[3]-rect[1]] = bmp[2][i][rect[1]..rect[3]-1]
    y = y + 1
end for
return ret
end function


Mike The Spike

PS. Use


Back to code-code-code!!
______________________________________________________
Get Your Private, Free Email at http://www.hotmail.com

new topic     » topic index » view message » categorize

2. Re: Lightning-Fast Bitmap Cropping

Elementary lesson in Euphoria optimization, almost entirely dedicated
to you, Mike the Spike...


yours:

global function crop_image(sequence bmp,sequence rect)
sequence ret
integer y
ret = repeat(repeat(0,rect[3]-rect[1]),rect[4]-rect[2])
y = 1

for i = rect[2] to rect[4]-1 do -- To the height of the cropping rect
    ret[y][1..rect[3]-rect[1]] = bmp[2][i][rect[1]..rect[3]-1]
    y = y + 1
end for
return ret
end function


mine:

global function crop(sequence bmp,integer x,integer y,integer
w,integer h)
    sequence s
    integer x2
    s = repeat(repeat(0,w),h)
    x2 = x+w-1
    for i = 1 to h do
        s[i] = bmp[y][x..x2]
        y += 1
    end for
    return s
end function

Much prettier, eh, pal?! Much faster too! I grabbed the first bitmap
I spotted on my drive, a 320x200x256 pic, ran it through 1000
iterations, removing a 20 pixel border from it, just for the heck of
it. Your "Lightning-Fast", routine took 6.63 seconds, my plain effort
just 3.49 seconds. I did not bother with any more tests, it's just not
worth it. I hope even you can appreciate the difference. jiri

Btw, your routine cheats: it does not show both the last column and
the last row of pixels of the cropped image.;)

new topic     » goto parent     » topic index » view message » categorize

3. Re: Lightning-Fast Bitmap Cropping

>Elementary lesson in Euphoria optimization, almost entirely dedicated
>to you, Mike the Spike...
>
>
>yours:
>
>global function crop_image(sequence bmp,sequence rect)
>sequence ret
>integer y
>ret = repeat(repeat(0,rect[3]-rect[1]),rect[4]-rect[2])
>y = 1
>
>for i = rect[2] to rect[4]-1 do -- To the height of the cropping rect
>     ret[y][1..rect[3]-rect[1]] = bmp[2][i][rect[1]..rect[3]-1]
>     y = y + 1
>end for
>return ret
>end function
>
>
>mine:
>
>global function crop(sequence bmp,integer x,integer y,integer
>w,integer h)
>     sequence s
>     integer x2
>     s = repeat(repeat(0,w),h)
>     x2 = x+w-1
>     for i = 1 to h do
>         s[i] = bmp[y][x..x2]
>         y += 1
>     end for
>     return s
>end function
>
>Much prettier, eh, pal?! Much faster too! I grabbed the first bitmap
>I spotted on my drive, a 320x200x256 pic, ran it through 1000
>iterations, removing a 20 pixel border from it, just for the heck of
>it. Your "Lightning-Fast", routine took 6.63 seconds, my plain effort
>just 3.49 seconds. I did not bother with any more tests, it's just not
>worth it. I hope even you can appreciate the difference. jiri
>
>Btw, your routine cheats: it does not show both the last column and
>the last row of pixels of the cropped image.;)

Thanks pal!
I knew there was a faster way, I was just too lazy to find out wich one it
was :p
Now I don't have to anymore, thanks again!


Mike The Spike
______________________________________________________
Get Your Private, Free Email at http://www.hotmail.com

new topic     » goto parent     » topic index » view message » categorize

4. Re: Lightning-Fast Bitmap Cropping

>Elementary lesson in Euphoria optimization, almost entirely dedicated
>to you, Mike the Spike...
>
>
>yours:
>
>global function crop_image(sequence bmp,sequence rect)
>sequence ret
>integer y
>ret = repeat(repeat(0,rect[3]-rect[1]),rect[4]-rect[2])
>y = 1
>
>for i = rect[2] to rect[4]-1 do -- To the height of the cropping rect
>     ret[y][1..rect[3]-rect[1]] = bmp[2][i][rect[1]..rect[3]-1]
>     y = y + 1
>end for
>return ret
>end function
>
>
>mine:
>
>global function crop(sequence bmp,integer x,integer y,integer
>w,integer h)
>     sequence s
>     integer x2
>     s = repeat(repeat(0,w),h)
>     x2 = x+w-1
>     for i = 1 to h do
>         s[i] = bmp[y][x..x2]
>         y += 1
>     end for
>     return s
>end function
>
>Much prettier, eh, pal?! Much faster too! I grabbed the first bitmap
>I spotted on my drive, a 320x200x256 pic, ran it through 1000
>iterations, removing a 20 pixel border from it, just for the heck of
>it. Your "Lightning-Fast", routine took 6.63 seconds, my plain effort
>just 3.49 seconds. I did not bother with any more tests, it's just not
>worth it. I hope even you can appreciate the difference. jiri
>
>Btw, your routine cheats: it does not show both the last column and
>the last row of pixels of the cropped image.;)

Jiri, I put your routine in my code, and only changed the parameters from
integers to a sequence (like my routine), now it seems that yours  is slow
after all.
Here is your routine, with the parameters set like my routine, try it out:

global function cropBitmap(sequence bmp,sequence rect)
    sequence s
    integer x2
    s = repeat(repeat(0,rect[3]),rect[4])
    x2 = rect[1]+rect[3]-1
    for i = 1 to rect[4] do
        s[i] = bmp[rect[2]][rect[1]..rect[3]]
        rect[2] = rect[2] + 1
    end for
    return s
end function


Mike The Spike
PS. Sequences are slower then integers...
______________________________________________________
Get Your Private, Free Email at http://www.hotmail.com

new topic     » goto parent     » topic index » view message » categorize

5. Re: Lightning-Fast Bitmap Cropping

Shit Jiri, your routine slows down a bit with the new parameters, but it's
still faster then mine...
So don't get me wrong or anything :)
Still, things realy fly in my code now! :)



Mike The Spike
PS. I'm using your "scale.e", it's quite fast aswell!
______________________________________________________
Get Your Private, Free Email at http://www.hotmail.com

new topic     » goto parent     » topic index » view message » categorize

6. Re: Lightning-Fast Bitmap Cropping

>Jiri, I put your routine in my code, and only changed the parameters
from
>integers to a sequence (like my routine), now it seems that yours  is
slow
>after all.

Call it "Elementary Lesson in Euphoria Optimization", Chapter 2. ;)
jiri

new topic     » goto parent     » topic index » view message » categorize

7. Re: Lightning-Fast Bitmap Cropping

>Shit Jiri, your routine slows down a bit with the new parameters, but
it's
>still faster then mine...
:
>Still, things realy fly in my code now! :)
:
>PS. I'm using your "scale.e", it's quite fast aswell!

Finally we are making some progress! That's Chapter 3. (Never mind, I
am
probably a lousy teacher.) jiri

new topic     » goto parent     » topic index » view message » categorize

8. Re: Lightning-Fast Bitmap Cropping

> ---------------------- Information from the mail
header -----------------------
> Sender:       Euphoria Programming for MS-DOS
<EUPHORIA at LISTSERV.MUOHIO.EDU>
> Poster:       jiri babor <jbabor at PARADISE.NET.NZ>
> Subject:      Re: Lightning-Fast Bitmap Cropping
> --------------------------------------------------------------------------
-----
>
    <SNIP>
>
> Finally we are making some progress! That's Chapter 3. (Never mind, I
> am
> probably a lousy teacher.) jiri
>

    Not much in each chapter.  Do you have test after each chapter? :)
just a friendly poke.

        Lucius L. Hilley III

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu