Index: [thread] [date] [subject] [author]
  From: Evan Martin <txs@concentric.net>
  To  : ggi-develop@eskimo.com
  Date: Wed, 12 May 1999 01:24:50 -0700

Re: Semi-Snag in PyGGI...

Jim Meier wrote:
> Ah, I see. Makes sense once I pull my head out of the toilet.
> 
> Would it be a reasonable course of action to do something like the following:
> int block_size=(number_of_pixels*actual_pixel_size_in_bytes);
> ggi_pixel p=ggiMapColor(vis, &color);
> ggi_pixel *pixel_buffer=(ggi_pixel*)malloc(block_size);
> ggi_pixel *p=pixel_buffer
> for(i=0;i<number_of_pixels;i++){
>     *pixel_buffer=p;
>     p+=actual_pixel_size_in_bytes;
> }
> 
> I'm thinking there's something wrong with my pointer arithmetic there.. but that's
> a small detail to iron out.

It took me a while to figure this one out, too.
The only problem is, that since p is a pointer to ggi_pixel, the line
p+=actual_pixel_size_in_byes; will advance the pointer
actual_pixel_size_in_bytes*sizeof(p) bytes forward in memory, not the
desired effect.
Casting to a uint8 (a byte, really) will solve this.

Read my post from a while back titled "Re: GGI usage question", it has
an explanation and code snippet which does something similar.
I'll paste the code, too.  (Note that this was written to work with
someone's wacky pallete implementation, so it has the extra complexity
of the two loops and the "graphics" struct.)

const ggi_pixelformat *pf = ggiGetPixelFormat(graphics->vis);
int pixelbytesize = pf->size/8;

uint8 *pixelbuf = (uint8*)malloc(graphics->x*graphics->y*pixelbytesize);
uint8 *p = pixelbuf;
for (i = 0; i < graphics->x; i++) {
    for (j = 0; j < graphics->y; j++);
        *(ggi_pixel*)p = graphics->palette[graphics->memory[i][j]];
        p += pixelbytesize;
    }
}
ggiPutBox(graphics->vis, 0, 0, graphics->x, graphics->y, pixelbuf);
free(pixelbuf);
ggiFlush(graphics->vis);

-- 
Evan Martin - txs@concentric.net - http://e.x0r.ml.org

Index: [thread] [date] [subject] [author]