Create surfaces with custom data
I'm trying to resize an SDL surface after loading it with a bitmap. I do this by creating new pixel data by a resize function and then calling SDL_CreateRGBSurfaceFrom() with that pixel data.
It works fine for resizing only the height of the image. If the width is resized(irrespective of height), the resulting image shows some weird color pattern mildly resembling the original image.
I suspect it has something to do with the pitch, but I didn't find anything wrong there.
I would appreciate any help, I'm really at my wit's end after spending two days on this.
I wouldn't say no to a lecture on pitch either, I've found very little on the net about that.
This is the code, its for 16-bit data only.
Thanks, (for reading till here at least)
It works fine for resizing only the height of the image. If the width is resized(irrespective of height), the resulting image shows some weird color pattern mildly resembling the original image.
I suspect it has something to do with the pitch, but I didn't find anything wrong there.
I would appreciate any help, I'm really at my wit's end after spending two days on this.
I wouldn't say no to a lecture on pitch either, I've found very little on the net about that.
This is the code, its for 16-bit data only.
Thanks, (for reading till here at least)
Code:
/* ... */
#define BPP_BMAP 16 /* bits per pixel */
typedef unsigned short word; /* 16 bit integer */
#define align4(X) (((X) + 3) & ~3) /* return alignment at next multiple of 4 */
#define pixcopy(DEST, DOFF, SRC, SOFF) (*(word*)((DEST) + (DOFF)) = *(word*)((SRC) + (SOFF)))
/* resizes a bitmap by width or height or both */
void* resizebmap(void **base, int w, int h, float scalew, float scaleh)
{
int x, y, neww, newh;
void *newbase;
neww = (int) (w * scalew);
newh = (int) (h * scaleh);
neww = align4(neww);
/* allocate memory for bitmap data for resized image */
if ((newbase = malloc(neww * newh * BPP_BMAP)) == NULL)
return NULL;
/* resize by expanding each pixel */
for (y = 0; y < newh; y++){
for (x = 0; x < neww; x++){
pixcopy(newbase, y * neww + x, *base, ((int)(y/scaleh))*w + ((int)(x/scalew)));
}
}
return newbase;
}
main()
{
float xc, yc;
int p;
SDL_Surface *screen, *bmp, *bmp1;
void *pix;
if ((bmp1 = SDL_LoadBMP("test.bmp")) == NULL){
printf("Couldn't load bitmap\n");
exit(1);
}
if ((bmp = SDL_ConvertSurface(bmp1, screen->format, 0)) == NULL){
printf("Couldn't load bitmap\n");
exit(1);
}
xc = 1, yc = 1;
pix = resizebmap(&bmp->pixels, bmp->pitch, bmp->h, xc, yc);
p = bmp->pitch * xc;
p = align4(p);
bmp1 = SDL_CreateRGBSurfaceFrom (pix, bmp->w * xc, bmp->h * yc, bmp->format->BitsPerPixel, p, bmp->format->Rmask, bmp->format->Gmask, bmp->format->Bmask, bmp->format->Amask);
SDL_BlitSurface(bmp1, NULL, screen, NULL);
/* ... */
}
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| When to create custom OpenGL view instead of subclass NSOpenGL view | Coyote | 37 | 16,927 |
Oct 20, 2009 08:16 PM Last Post: Coyote |
|
| Smooth model surfaces | imikedaman | 3 | 2,469 |
Jul 12, 2006 10:34 PM Last Post: imikedaman |
|
| Non-square, non-POT, hardware-accelerated surfaces... | NekoYasha | 13 | 5,282 |
Oct 25, 2005 03:06 PM Last Post: NekoYasha |
|
| Sharing textures in SDL's OpenGL surfaces | PowerMacX | 4 | 3,835 |
Oct 11, 2004 01:34 PM Last Post: OneSadCookie |
|

