pixel level modifications on an SDL_Surface

Started by
13 comments, last by bennn 14 years, 5 months ago
hi guys, i'm a newbie using sdl in C++. I did my codes to try to get rgb color components from an 8-bit grayscale image using what was taught here : <html>http://sdl.beuc.net/sdl.wiki/SDL_PixelFormat</html> However, I keep getting this error: incompatible types in assignment at this line: color = fmt->palette->colors[index]; My partial code as shown below: void colorInterpolation(SDL_Surface *s) { SDL_PixelFormat *fmt; Uint32 temp, pixel; Uint8 red, green, blue, alpha, index; SDL_Color *color; fmt=s->format; /* Check the bitdepth of the surface */ if(fmt->BitsPerPixel!=8){ fprintf(stderr, "Not an 8-bit surface.\n"); return; } SDL_LockSurface(s); .............. ............ /* Get the topleft pixel */ index = *(Uint8 *)s->pixels; color = fmt->palette->colors[index]; //ERROR : incompatible types in assignment WHY?????? ............. ........... ....... Your advice is highly appreciated!!! thanks! :D
Advertisement
Because fmt->palette->colors[index] has type SDL_Color, not SDL_Color *.

Did you check the documentation?
hi mattd,

could u pls help pin point to me how to change this code to correct this error?

I did exactly like the tutorial below extracted from http://sdl.beuc.net/sdl.wiki/SDL_PixelFormat

1 SDL_Surface *surface;
2 SDL_PixelFormat *fmt;
3 SDL_Color *color;
4 Uint8 index;
5
6 .
7 .
8
9 /* Create surface */
10 .
11 .
12 fmt=surface->format;
13
14 /* Check the bitdepth of the surface */
15 if(fmt->BitsPerPixel!=8){
16 fprintf(stderr, "Not an 8-bit surface.\n");
17 return(-1);
18 }
19
20 /* Lock the surface */
21 SDL_LockSurface(surface);
22
23 /* Get the topleft pixel */
24 index=*(Uint8 *)surface->pixels;
25 color=fmt->palette->colors[index];
26
27 /* Unlock the surface */
28 SDL_UnlockSurface(surface);
29 printf("Pixel Color-> Red: %d, Green: %d, Blue: %d. Index: %d\n",
30 color->r, color->g, color->b, index);
Here's a diff.

You DO know C++, right? :)
Here's an example of what you, or rather what that sample code is doing:

int array[1];array[0] = 0;int* pNum = array[0]; //Error is here..similar to the sample code..


There's multiple ways to fix this, but if you are having trouble
understanding this concept, i suggest you read up on what pointers, references, etc. are in C/C++.
Read Chapter 6 here.

Nothing wrong with not understanding..it's all in the learning process.
And, also, don't always assume other people's sample code to be right,
and blindly copy and paste.
Hi mattd, thanks for the help seriously!! I have never learnt C++, this is probably the first time do something like that and i'm trying to get things right by myself... Now I got this runtime error "Segmentation fault", what could be wrong??
Quote:Original post by bennn
what could be wrong??

The fact that you're learning a graphics library before you learn the language its interface uses? :) (OK, it's C, but hey)

Post your entire code. Use [source] ... [/source] tags to keep it tidy.
thanks matt, PM-ed you :)
I got your PM, but can't you post your code in this thread? That way anyone can help by checking it, or learn from any mistakes pointed out.
thanks guys, any help is greatly appreciated, i'm really stucked right now, the functions in the codes aren't fully implemented as i'm still figuring out those errors on hand...

This topic is closed to new replies.

Advertisement