Copying part of a pixmap
BlitzMax Forums/BlitzMax Programming/Copying part of a pixmap
| ||
SuperStrict Function PixmapCopyRegion:TPixmap(source:TPixmap, x:Int, y:Int, w:Int, h:Int) Local dest:TPixmap = CreatePixmap(w, h, PF_RGBA8888) source = ConvertPixmap(source, PF_RGBA8888) Local source_p:Byte Ptr = PixmapPixelPtr(source, x, y) Local dest_p:Byte Ptr = PixmapPixelPtr(dest) For Local i:Int = 0 To h - 1 For Local j:Int = 0 To w - 1 dest_p[(i * PixmapPitch(dest)) + (j * 4)] = source_p[(i * PixmapPitch(source)) + (j * 4)] Next Next Return dest End Function Local s:TPixmap = LoadPixmap("smile.jpg") If s Then Local d:TPixmap = PixmapCopyRegion(s, 0, 0, 64, 64) SavePixmapPNG(d, "smile.png") End If End Can anyone see anything obviously wrong with that function? It looks correct to me but the results are off. This is the image I'm using as the source: And this is the result I get from the above code: Also, if I run the above code but pass the width and height parameters as 128 (to copy the full image), the result is just a blank image. Someone point out my obvious and stupid mistake, because I can't see it. (Edited for image tags that work) |
| ||
And I've just had PixmapWindow pointed out to me, so this is now largely a theoretical exercise. |
| ||
Well I just figured it out, I'm multiplying the wrong variable by 4. The lines: For Local j:Int = 0 To w - 1 dest_p[(i * PixmapPitch(dest)) + (j * 4)] = source_p[(i * PixmapPitch(source)) + (j * 4)] Should read: For Local j:Int = 0 To (w * 4) - 1 dest_p[(i * PixmapPitch(dest)) + (j)] = source_p[(i * PixmapPitch(source)) + (j)] |
| ||
I'm multiplying the wrong variable by 4. I make similar mistakes all the time. My brain just won't accept that pixel co-ordinates (x,y) refer to column x and row y. It must be that I always think of the words row,column in that order. I also think of x,y in the same order so they naturally match up. |