# Obracanie obrazka o 90 stopni za pomocą SDL.

0

Cześć,
mam za zadanie zaimplementować obracanie wczytanego obrazka o 90 stopni. Mam działający program, który rozmywa obraz i muszę go przerobić tak, aby obracał obrazek.
Rozmywanie (działa po naciśnięciu spacji):

``````#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <SDL/SDL.h>
#include <SDL/SDL_image.h>

void Filter(unsigned char * buf, int width, int height, int size, char bpp, unsigned short pitch)
{
printf("%d %d %d\n", width, height, (int)bpp);
unsigned char *tmp = (unsigned char*)malloc(pitch*height);
memcpy(tmp, buf, pitch*height);
int pix[9], sum, count;
int x, y, k, i;
for(y=0; y<height; ++y)
for(x=0; x<width; ++x)
{

pix[0] = y*pitch + x*bpp;
pix[1] = pix[0] - pitch;
pix[2] = pix[0] + pitch;
pix[3] = pix[1] - bpp;
pix[4] = pix[1] + bpp;
pix[5] = pix[0] - bpp;
pix[6] = pix[0] + bpp;
pix[7] = pix[2] - bpp;
pix[8] = pix[2] + bpp;

for(k=0; k<bpp; ++k)
{
sum = 0;
count = 0;
for(i=0; i<9; ++i)
{
if(pix[i]>=0 && pix[i]<pitch*height)
{
sum += (int) (tmp[ pix[i]+k ]);
++count;
}
}
sum /= count;
buf[ pix[0]+k ] = (unsigned char)sum;
}
}
}

SDL_Surface* Load_image(char *file_name)
{
/* Open the image file */
SDL_Surface* tmp = IMG_Load(file_name);
if ( tmp == NULL ) {
fprintf(stderr, "Couldn't load %s: %s\n",
file_name, SDL_GetError());
exit(0);
}
return tmp;
}

void Paint(SDL_Surface* image, SDL_Surface* screen)
{
SDL_BlitSurface(image, NULL, screen, NULL);
SDL_UpdateRect(screen, 0, 0, 0, 0);
};

int main(int argc, char *argv[])
{
Uint32 flags;
SDL_Surface *screen, *image;
int depth, done;
SDL_Event event;

/* Check command line usage */
if ( ! argv[1] ) {
fprintf(stderr, "Usage: %s <image_file>, (int) size\n", argv[0]);
return(1);
}

if ( ! argv[2] ) {
fprintf(stderr, "Usage: %s <image_file>, (int) size\n", argv[0]);
return(1);
}

/* Initialize the SDL library */
if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
return(255);
}

flags = SDL_SWSURFACE;
image = Load_image( argv[1] );
printf( "\n\nImage properts:\n" );
printf( "BitsPerPixel = %i \n", image->format->BitsPerPixel );
printf( "BytesPerPixel = %i \n", image->format->BytesPerPixel );
printf( "width %d ,height %d \n\n", image->w, image->h );

SDL_WM_SetCaption(argv[1], "showimage");

/* Create a display for the image */
depth = SDL_VideoModeOK(image->w, image->h, 32, flags);
/* Use the deepest native mode, except that we emulate 32bpp
for viewing non-indexed images on 8bpp screens */
if ( depth == 0 ) {
if ( image->format->BytesPerPixel > 1 ) {
depth = 32;
} else {
depth = 8;
}
} else
if ( (image->format->BytesPerPixel > 1) && (depth == 8) ) {
depth = 32;
}
if(depth == 8)
flags |= SDL_HWPALETTE;
screen = SDL_SetVideoMode(image->w, image->h, depth, flags);
if ( screen == NULL ) {
fprintf(stderr,"Couldn't set %dx%dx%d video mode: %s\n",
image->w, image->h, depth, SDL_GetError());
}

/* Set the palette, if one exists */
if ( image->format->palette ) {
SDL_SetColors(screen, image->format->palette->colors,
0, image->format->palette->ncolors);
}

printf("\$\$\$\$\$\$ %u \n", image->pitch);
/* Display the image */
Paint(image, screen);

done = 0;
int size =atoi( argv[2] );
printf("Actual size is: %d\n", size);
while ( ! done ) {
if ( SDL_PollEvent(&event) ) {
switch (event.type) {
case SDL_KEYUP:
switch (event.key.keysym.sym) {
case SDLK_ESCAPE:
case SDLK_TAB:
case SDLK_q:
done = 1;
break;
case SDLK_SPACE:
case SDLK_f:
SDL_LockSurface(image);

printf("Start filtering...  ");
Filter(image->pixels,image->w,image->h, size, image->format->BytesPerPixel, image->pitch);
printf("Done.\n");

SDL_UnlockSurface(image);

printf("Repainting after filtered...  ");
Paint(image, screen);
printf("Done.\n");

break;
case SDLK_r:
printf("Reloading image...  ");
image = Load_image( argv[1] );
Paint(image,screen);
printf("Done.\n");
break;
case SDLK_PAGEDOWN:
case SDLK_DOWN:
case SDLK_KP_MINUS:
size--;
if (size==0) size--;
printf("Actual size is: %d\n", size);
break;
case SDLK_PAGEUP:
case SDLK_UP:
case SDLK_KP_PLUS:
size++;
if (size==0) size++;
printf("Actual size is: %d\n", size);
break;
case SDLK_s:
printf("Saving surface at nowy.bmp ...");
SDL_SaveBMP(image, "nowy.bmp" );
printf("Done.\n");
default:
break;
}
break;
//              case  SDL_MOUSEBUTTONDOWN:
//              done = 1;
//              break;
case SDL_QUIT:
done = 1;
break;
default:
break;
}
} else {
SDL_Delay(10);
}
}
SDL_FreeSurface(image);
/* We're done! */
SDL_Quit();
return(0);
}
``````

Moja funkcja, która nie działa:

``````void Filter(unsigned char * buf, int width, int height, int size, char bpp, unsigned short pitch)
{
printf("%d %d %d\n", width, height, (int)bpp);
unsigned char *tmp = (unsigned char*)malloc(pitch*height);
memcpy(tmp, buf, pitch*height);
int i, j;

for(i=0; i<width; i++) {
for(j=0; j<height; j++) {
buf[i][j] = tmp[height-1-j][i];
}
}
}
``````

Sam algorytm obracania tablicy działa jak należy, bo go sprawdzałem na liczbach.

Mam problem z poprawnym odnoszeniem się do pojedynczych pixeli, tzn. nie do końca rozumiem jak to ma wyglądać.

Bardzo proszę o pomoc w poprawieniu kodu.

Z góry dziękuję za każdą odpowiedź i pozdrawiam.

0

Przepraszam za podbijanie, ale potrzebuję pilnie z tym pomocy, a wiem, że coś jest nie tak z wskaźnikami w mojej funkcji. Jest ktoś w stanie mi pomóc?

1

Rotację wykonujesz klawiszem Z. Argument, który podawałeś do funkcji Filter trzeba było rzutować na (unsigned char*). Rotację o 90 stopni zrobiłem w bardzo prosty sposób. Zamieniłem X z Y przy rysowaniu nowej bitmapy, szerokość z wysokością. Funkcje składowe (put_pixel, get_pixel) znalazłem gdzieś na necie. Testowałem na 32bitowym pliku PNG (z kanałem alpha). Powinieneś popracować jeszcze nad tym, żeby okienko i główny surface zmieniały rozmiar, dostosowując się do rysowanego obrazu.

``````#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <SDL/SDL.h>
#include <SDL/SDL_image.h>

void Filter(unsigned char * buf, int width, int height, int size, char bpp, unsigned short pitch)
{
printf("%d %d %d\n", width, height, (int)bpp);
unsigned char *tmp = (unsigned char*)malloc(pitch*height);
memcpy(tmp, buf, pitch*height);
int pix[9], sum, count;
int x, y, k, i;
for(y=0; y<height; ++y)
for(x=0; x<width; ++x)
{

pix[0] = y*pitch + x*bpp;
pix[1] = pix[0] - pitch;
pix[2] = pix[0] + pitch;
pix[3] = pix[1] - bpp;
pix[4] = pix[1] + bpp;
pix[5] = pix[0] - bpp;
pix[6] = pix[0] + bpp;
pix[7] = pix[2] - bpp;
pix[8] = pix[2] + bpp;

for(k=0; k<bpp; ++k)
{
sum = 0;
count = 0;
for(i=0; i<9; ++i)
{
if(pix[i]>=0 && pix[i]<pitch*height)
{
sum += (int) (tmp[ pix[i]+k ]);
++count;
}
}
sum /= count;
buf[ pix[0]+k ] = (unsigned char)sum;
}
}
}

SDL_Surface* Load_image(char *file_name)
{
/* Open the image file */
SDL_Surface* tmp = IMG_Load(file_name);
if ( tmp == NULL )
{
fprintf(stderr, "Couldn't load %s: %s\n",
file_name, SDL_GetError());
exit(0);
}
return tmp;
}

void Paint(SDL_Surface* image, SDL_Surface* screen)
{
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
SDL_BlitSurface(image, NULL, screen, NULL);
SDL_UpdateRect(screen, 0, 0, 0, 0);
};

Uint32 get_pixel32( SDL_Surface *surface, int x, int y )
{
//Convert the pixels to 32 bit
Uint32 *pixels = (Uint32 *)surface->pixels;
//Get the requested pixel
return pixels[ ( y * surface->w ) + x ];
}

void put_pixel32( SDL_Surface *surface, int x, int y, Uint32 pixel )
{
//Convert the pixels to 32 bit
Uint32 *pixels = (Uint32 *)surface->pixels;
//Set the pixel
pixels[ ( y * surface->w ) + x ] = pixel;
}

SDL_Surface* rotate(SDL_Surface* source) // obracanie o 90 stopni
{
SDL_Surface *target;

//tworzymy nowa powierzchnie o wymiarach obroconych o 90 stopni

if( source->flags & SDL_SRCCOLORKEY )
{
target = SDL_CreateRGBSurface( SDL_SWSURFACE, source->h, source->w, source->format->BitsPerPixel, source->format->Rmask, source->format->Gmask, source->format->Bmask, 0 );
}
//Otherwise
else
{
target = SDL_CreateRGBSurface( SDL_SWSURFACE, source->h, source->w, source->format->BitsPerPixel, source->format->Rmask, source->format->Gmask, source->format->Bmask, source->format->Amask );
}

//obracamy
for (int x=0; x<source->w; x++)
{
for (int y=0; y<source->h; y++)
{
put_pixel32(target,y,x,get_pixel32(source,x,source->h-y-1));
}
}

//zwracamy nowy obraz
return target;
}

int main(int argc, char *argv[])
{
Uint32 flags;
SDL_Surface *screen, *image;
int depth, done;
SDL_Event event;

/* Check command line usage */
if ( ! argv[1] )
{
fprintf(stderr, "Usage: %s <image_file>, (int) size\n", argv[0]);
return(1);
}

if ( ! argv[2] )
{
fprintf(stderr, "Usage: %s <image_file>, (int) size\n", argv[0]);
return(1);
}

/* Initialize the SDL library */
if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
{
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
return(255);
}

flags = SDL_SWSURFACE;
image = Load_image( argv[1] );
printf( "\n\nImage properts:\n" );
printf( "BitsPerPixel = %i \n", image->format->BitsPerPixel );
printf( "BytesPerPixel = %i \n", image->format->BytesPerPixel );
printf( "width %d ,height %d \n\n", image->w, image->h );

SDL_WM_SetCaption(argv[1], "showimage");

/* Create a display for the image */
depth = SDL_VideoModeOK(image->w, image->h, 32, flags);
/* Use the deepest native mode, except that we emulate 32bpp
for viewing non-indexed images on 8bpp screens */
if ( depth == 0 )
{
if ( image->format->BytesPerPixel > 1 )
{
depth = 32;
}
else
{
depth = 8;
}
}
else if ( (image->format->BytesPerPixel > 1) && (depth == 8) )
{
depth = 32;
}
if(depth == 8)
flags |= SDL_HWPALETTE;
screen = SDL_SetVideoMode(image->w, image->h, depth, flags);
if ( screen == NULL )
{
fprintf(stderr,"Couldn't set %dx%dx%d video mode: %s\n",
image->w, image->h, depth, SDL_GetError());
}

/* Set the palette, if one exists */
if ( image->format->palette )
{
SDL_SetColors(screen, image->format->palette->colors,
0, image->format->palette->ncolors);
}

printf("\$\$\$\$\$\$ %u \n", image->pitch);
/* Display the image */
Paint(image, screen);

done = 0;
int size =atoi( argv[2] );
printf("Actual size is: %d\n", size);
while ( ! done )
{
if ( SDL_PollEvent(&event) )
{
switch (event.type)
{
case SDL_KEYUP:
switch (event.key.keysym.sym)
{
case SDLK_z:
{
SDL_Surface *rotatedImg=rotate(image);

Paint(rotatedImg, screen);

SDL_FreeSurface(rotatedImg);
}
break;
case SDLK_ESCAPE:
case SDLK_TAB:
case SDLK_q:
done = 1;
break;
case SDLK_SPACE:
case SDLK_f:
SDL_LockSurface(image);

printf("Start filtering...  ");
Filter((unsigned char*)image->pixels,image->w,image->h, size, image->format->BytesPerPixel, image->pitch);
printf("Done.\n");

SDL_UnlockSurface(image);

printf("Repainting after filtered...  ");
Paint(image, screen);
printf("Done.\n");

break;
case SDLK_r:
printf("Reloading image...  ");
image = Load_image( argv[1] );
Paint(image,screen);
printf("Done.\n");
break;
case SDLK_PAGEDOWN:
case SDLK_DOWN:
case SDLK_KP_MINUS:
size--;
if (size==0) size--;
printf("Actual size is: %d\n", size);
break;
case SDLK_PAGEUP:
case SDLK_UP:
case SDLK_KP_PLUS:
size++;
if (size==0) size++;
printf("Actual size is: %d\n", size);
break;
case SDLK_s:
printf("Saving surface at nowy.bmp ...");
SDL_SaveBMP(image, "nowy.bmp" );
printf("Done.\n");
default:
break;
}
break;
//              case  SDL_MOUSEBUTTONDOWN:
//              done = 1;
//              break;
case SDL_QUIT:
done = 1;
break;
default:
break;
}
}
else
{
SDL_Delay(10);
}
}
SDL_FreeSurface(image);
/* We're done! */
SDL_Quit();
return(0);
}
``````
0

Mam problem z tym kodem na swoim komputerze, bo jak testowałem go na jednym to działało dobrze, a na innym już wyrzuca błąd "Segmentation fault". Co może być przyczyną takiego stanu ?

0

masz gdzies wyciek pamieci.

Debugger do reki i patrzaj, zapewne uzywasz wskaznika ktory nie jest zainicializowany.

1

To jest wina wczytywanego obrazka. Funkcje do pikseli, które dodałem są tylko do 32-bitowych grafik. Dla mniejszych wychodzą poza zakres tablicy pixels.

Oto nowy kod, mam nadzieję, że już bezproblemowy ;) Dodałem bardziej rozbudowane funkcje do pikseli (stąd: http://sdl.beuc.net/sdl.wiki/Pixel_Access ).

``````#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <SDL/SDL.h>
#include <SDL/SDL_image.h>

void Filter(unsigned char * buf, int width, int height, int size, char bpp, unsigned short pitch)
{
printf("%d %d %d\n", width, height, (int)bpp);
unsigned char *tmp = (unsigned char*)malloc(pitch*height);
memcpy(tmp, buf, pitch*height);
int pix[9], sum, count;
int x, y, k, i;
for(y=0; y<height; ++y)
for(x=0; x<width; ++x)
{

pix[0] = y*pitch + x*bpp;
pix[1] = pix[0] - pitch;
pix[2] = pix[0] + pitch;
pix[3] = pix[1] - bpp;
pix[4] = pix[1] + bpp;
pix[5] = pix[0] - bpp;
pix[6] = pix[0] + bpp;
pix[7] = pix[2] - bpp;
pix[8] = pix[2] + bpp;

for(k=0; k<bpp; ++k)
{
sum = 0;
count = 0;
for(i=0; i<9; ++i)
{
if(pix[i]>=0 && pix[i]<pitch*height)
{
sum += (int) (tmp[ pix[i]+k ]);
++count;
}
}
sum /= count;
buf[ pix[0]+k ] = (unsigned char)sum;
}
}
}

SDL_Surface* Load_image(char *file_name)
{
/* Open the image file */
SDL_Surface* tmp = IMG_Load(file_name);
if ( tmp == NULL )
{
fprintf(stderr, "Couldn't load %s: %s\n",
file_name, SDL_GetError());
exit(0);
}
return tmp;
}

void Paint(SDL_Surface* image, SDL_Surface* screen)
{
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
SDL_BlitSurface(image, NULL, screen, NULL);
SDL_UpdateRect(screen, 0, 0, 0, 0);
};

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;
break;

case 2:
return *(Uint16 *)p;
break;

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;
break;

case 4:
return *(Uint32 *)p;
break;

default:
return 0;       /* shouldn't happen, but avoids warnings */
}
}

void putpixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
{
int bpp = surface->format->BytesPerPixel;
/* Here p is the address to the pixel we want to set */
Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;

switch(bpp) {
case 1:
*p = pixel;
break;

case 2:
*(Uint16 *)p = pixel;
break;

case 3:
if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
p[0] = (pixel >> 16) & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = pixel & 0xff;
} else {
p[0] = pixel & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = (pixel >> 16) & 0xff;
}
break;

case 4:
*(Uint32 *)p = pixel;
break;
}
}

SDL_Surface* rotate(SDL_Surface* source) // obracanie o 90 stopni
{
SDL_Surface *target;

//tworzymy nowa powierzchnie o wymiarach obroconych o 90 stopni

if( source->flags & SDL_SRCCOLORKEY )
{
target = SDL_CreateRGBSurface( SDL_SWSURFACE, source->h, source->w, source->format->BitsPerPixel, source->format->Rmask, source->format->Gmask, source->format->Bmask, 0 );
}
//Otherwise
else
{
target = SDL_CreateRGBSurface( SDL_SWSURFACE, source->h, source->w, source->format->BitsPerPixel, source->format->Rmask, source->format->Gmask, source->format->Bmask, source->format->Amask );
}

//obracamy
for (int x=0; x<source->w; x++)
{
for (int y=0; y<source->h; y++)
{
putpixel(target,y,x,getpixel(source,x,source->h-y-1));
}
}

//zwracamy nowy obraz
return target;
}

int main(int argc, char *argv[])
{
Uint32 flags;
SDL_Surface *screen, *image;
int depth, done;
SDL_Event event;

/* Check command line usage */
if ( ! argv[1] )
{
fprintf(stderr, "Usage: %s <image_file>, (int) size\n", argv[0]);
return(1);
}

if ( ! argv[2] )
{
fprintf(stderr, "Usage: %s <image_file>, (int) size\n", argv[0]);
return(1);
}

/* Initialize the SDL library */
if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
{
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
return(255);
}

flags = SDL_SWSURFACE;
image = Load_image( argv[1] );
printf( "\n\nImage properts:\n" );
printf( "BitsPerPixel = %i \n", image->format->BitsPerPixel );
printf( "BytesPerPixel = %i \n", image->format->BytesPerPixel );
printf( "width %d ,height %d \n\n", image->w, image->h );

SDL_WM_SetCaption(argv[1], "showimage");

/* Create a display for the image */
depth = SDL_VideoModeOK(image->w, image->h, 32, flags);
/* Use the deepest native mode, except that we emulate 32bpp
for viewing non-indexed images on 8bpp screens */
if ( depth == 0 )
{
if ( image->format->BytesPerPixel > 1 )
{
depth = 32;
}
else
{
depth = 8;
}
}
else if ( (image->format->BytesPerPixel > 1) && (depth == 8) )
{
depth = 32;
}
if(depth == 8)
flags |= SDL_HWPALETTE;
screen = SDL_SetVideoMode(image->w, image->h, depth, flags);
if ( screen == NULL )
{
fprintf(stderr,"Couldn't set %dx%dx%d video mode: %s\n",
image->w, image->h, depth, SDL_GetError());
}

/* Set the palette, if one exists */
if ( image->format->palette )
{
SDL_SetColors(screen, image->format->palette->colors,
0, image->format->palette->ncolors);
}

printf("\$\$\$\$\$\$ %u \n", image->pitch);
/* Display the image */
Paint(image, screen);

done = 0;
int size =atoi( argv[2] );
printf("Actual size is: %d\n", size);
while ( ! done )
{
if ( SDL_PollEvent(&event) )
{
switch (event.type)
{
case SDL_KEYUP:
switch (event.key.keysym.sym)
{
case SDLK_z:
{
SDL_Surface *rotatedImg=rotate(image);

Paint(rotatedImg, screen);

SDL_FreeSurface(rotatedImg);
}
break;
case SDLK_ESCAPE:
case SDLK_TAB:
case SDLK_q:
done = 1;
break;
case SDLK_SPACE:
case SDLK_f:
SDL_LockSurface(image);

printf("Start filtering...  ");
Filter((unsigned char*)image->pixels,image->w,image->h, size, image->format->BytesPerPixel, image->pitch);
printf("Done.\n");

SDL_UnlockSurface(image);

printf("Repainting after filtered...  ");
Paint(image, screen);
printf("Done.\n");

break;
case SDLK_r:
printf("Reloading image...  ");
image = Load_image( argv[1] );
Paint(image,screen);
printf("Done.\n");
break;
case SDLK_PAGEDOWN:
case SDLK_DOWN:
case SDLK_KP_MINUS:
size--;
if (size==0) size--;
printf("Actual size is: %d\n", size);
break;
case SDLK_PAGEUP:
case SDLK_UP:
case SDLK_KP_PLUS:
size++;
if (size==0) size++;
printf("Actual size is: %d\n", size);
break;
case SDLK_s:
printf("Saving surface at nowy.bmp ...");
SDL_SaveBMP(image, "nowy.bmp" );
printf("Done.\n");
default:
break;
}
break;
//              case  SDL_MOUSEBUTTONDOWN:
//              done = 1;
//              break;
case SDL_QUIT:
done = 1;
break;
default:
break;
}
}
else
{
SDL_Delay(10);
}
}
SDL_FreeSurface(image);
/* We're done! */
SDL_Quit();
return(0);
}
``````