T
Tachchen nochmal !
Also ich habe ja den Code kopiert und in mein Programm reinkopiert. Die Methode scheint auch ganz gut zu funktionieren, allerdings nur mit kleineren Bildern. Ich zunächst ein 12x12 PNG eingelesen. Keine Fehler. Jetzt probiere ich mich mit einem PNG von 1680x1050 und erhalte immer in der Zeile 1373 einen Speicherzugriffsfehler. Ich würde mich freuen wenn mal jemand kurz meinen Code hier ausprobieren könnte. Ich hab jedenfalls keine Ahnung wo hier ein Speicherzugriffsfehler stattfindet ??
#include "SDL.h"
#include "SDL_image.h"
#include <stdlib.h>
#include <stdio.h>
void testGetpixel( SDL_Surface* image);
Uint32 getpixel(SDL_Surface *surface, int x, int y);
int main() {
if( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr,
"Couldn't initialize SDL: %s\n", SDL_GetError());
exit(1);
}
SDL_RWops *rwop;
rwop=SDL_RWFromFile("Map1.png", "rb");
SDL_Surface* map = IMG_LoadPNG_RW( rwop );
testGetpixel( map );
}
void testGetpixel( SDL_Surface* image) {
for( int i = 1 ; i <= image->w; ++i ) {
for( int j = 1; j <= image->h; ++j ) {
if( j % 15 == 0)
printf("%d x %d\n", i ,j);
else
printf("%d x %d; ", i ,j);
Uint32 pixel= getpixel( image, i , j);
}
}
}
Uint32 getpixel(SDL_Surface *surface, int x, int y) {
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to retrieve */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
switch(bpp) {
case 1:
return *p;
case 2:
return *(Uint16 *)p;
case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
return p[0] << 16 | p[1] << 8 | p[2];
else
return p[0] | p[1] << 8 | p[2] << 16;
case 4:
return *(Uint32 *)p;
default:
return 0; /* shouldn't happen, but avoids warnings */
}
}
Kompiliert habe ich das Ganze mit
gcc -c `sdl-config --cflags` -std=c99 test.c
gcc -o test test.o `sdl-config --libs` -lSDL_image -std=c99