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.