SDL_Surface *load_image_mem(void *mem, int size) { SDL_Surface* optimizedImage = NULL; SDL_Surface* tvimage; SDL_Surface* timage; SDL_RWops *rw_out; rw_out = SDL_RWFromMem(mem, size); tvimage = IMG_Load_RW(rw_out, 0); timage = SDL_DisplayFormat( tvimage ); optimizedImage = SDL_CreateRGBSurface( SDL_SWSURFACE, timage->w, timage->h, timage->format->BitsPerPixel, timage->format->Rmask, timage->format->Gmask, timage->format->Bmask,timage->format->Amask ); //Copy color key Uint32 colorkey = SDL_MapRGB( timage->format, 0xde, 0x97, 0x47 ); SDL_SetColorKey( optimizedImage, SDL_RLEACCEL | SDL_SRCCOLORKEY, colorkey ); if( SDL_MUSTLOCK( timage ) ) { //Lock the surface SDL_LockSurface( timage ); } for (int x=0;x < timage->w;x++) { for (int y=0;y < timage->h;y++) { //x,y Uint32 pixel = get_pixel32( timage, x, y ); put_pixel32( optimizedImage, x,y, pixel ); } } if( SDL_MUSTLOCK( timage ) ) { SDL_UnlockSurface( timage ); } SDL_FreeSurface( tvimage ); SDL_FreeSurface( timage ); return optimizedImage; }
int SDLdrawLattice(double lattice[N][N], SDL_Surface *paintme, SDL_Surface *screen) { double scaling = 255./2.; for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { for (int k = 0; k < HEIGHT/N; ++k) { for (int l = 0; l < WIDTH/N; ++l) { Uint32 pix = SDL_MapRGB( screen->format, 0, 1.+cos(lattice[i][j])*scaling, 0 ); put_pixel32( paintme, i*HEIGHT/N+k, j*WIDTH/N+l, pix ); } } } //std::cout << std::endl; } apply_surface( 0, 0, paintme, screen ); SDL_Flip( screen ); //SDL_Delay( 10 ); }
SDL_Surface* PerlinNoise::Render_Clouds(const int x, const int y, const int z, const int w, const int h, const double zoom, const double p) { const SDL_PixelFormat& fmt = *(screen->format); SDL_Surface* ret = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, fmt.BitsPerPixel, fmt.Rmask,fmt.Gmask,fmt.Bmask,fmt.Amask); for(int yi = 0; yi<h; yi++) { for(int xi = 0; xi<w; xi++) { int color= (int)(perlinNoise(x+xi, y+yi, z, zoom, p)*255); if(color>255) color=255; if(color<0) color=0; Uint32 pixel = SDL_MapRGB(ret->format, color, color, color); put_pixel32(ret,xi,yi,pixel); } } return ret; }
SDL_Surface *flip_surface( SDL_Surface *surface, int flags ) { //Pointer to the soon to be flipped surface SDL_Surface *flipped = NULL; //If the image is color keyed if( surface->flags & SDL_SRCCOLORKEY ) { flipped = SDL_CreateRGBSurface( SDL_SWSURFACE, surface->w, surface->h, surface->format->BitsPerPixel, surface->format->Rmask, surface->format->Gmask, surface->format->Bmask, 0 ); } //Otherwise else { flipped = SDL_CreateRGBSurface( SDL_SWSURFACE, surface->w, surface->h, surface->format->BitsPerPixel, surface->format->Rmask, surface->format->Gmask, surface->format->Bmask, surface->format->Amask ); } //If the surface must be locked if( SDL_MUSTLOCK( surface ) ) { //Lock the surface SDL_LockSurface( surface ); } //Go through columns for( int x = 0, rx = flipped->w - 1; x < flipped->w; x++, rx-- ) { //Go through rows for( int y = 0, ry = flipped->h - 1; y < flipped->h; y++, ry-- ) { //Get pixel Uint32 pixel = get_pixel32( surface, x, y ); //Copy pixel if( ( flags & FLIP_VERTICAL ) && ( flags & FLIP_HORIZONTAL ) ) { put_pixel32( flipped, rx, ry, pixel ); } else if( flags & FLIP_HORIZONTAL ) { put_pixel32( flipped, rx, y, pixel ); } else if( flags & FLIP_VERTICAL ) { put_pixel32( flipped, x, ry, pixel ); } } } //Unlock surface if( SDL_MUSTLOCK( surface ) ) { SDL_UnlockSurface( surface ); } //Copy color key if( surface->flags & SDL_SRCCOLORKEY ) { SDL_SetColorKey( flipped, SDL_RLEACCEL | SDL_SRCCOLORKEY, surface->format->colorkey ); } //Return flipped surface return flipped; }
int main( int argc, char* args[] ) { img_t in, gauss, out; hough_t HT = {0}; lines_t lv = {0}; roi_t ROI = {0,0, 640, 480}; HT.l = &lv; in.w = out.w = gauss.w = IMG_W; in.h = out.h = gauss.h = IMG_H; in.r = out.r = gauss.r = &ROI; #if STATIC_ALLOC unsigned char in_img_data[IMG_W * IMG_H] = {0}; unsigned char gauss_img_data[IMG_W * IMG_H] = {0}; unsigned char out_img_data[IMG_W * IMG_H] = {0}; unsigned int hough_accumulator[(int)(IMG_W*1.4142)*180]; in.d = in_img_data; gauss.d = gauss_img_data; out.d = out_img_data; HT.accu = hough_accumulator; #else in.d = malloc(in.w * in.h * sizeof(unsigned char)); gauss.d = malloc(gauss.w * gauss.h * sizeof(unsigned char)); out.d = malloc(out.w * out.h * sizeof(unsigned char)); HT.accu = malloc((int)(IMG_W*1.4142)*180 * sizeof(unsigned int)); #endif #if SANITY_CHECKS if(in.d && gauss.d && out.d){ #if DEBUG printf("memory OK\n"); #endif } #endif #if SDL_USED SDL_Surface* screen = NULL; SDL_Init(SDL_INIT_EVERYTHING); #endif #if LOAD_BMP_SDL SDL_Surface* input_img = NULL; screen = SDL_SetVideoMode(IMG_W, IMG_H, 32, SDL_HWSURFACE); input_img = SDL_LoadBMP("input.bmp"); SDL_BlitSurface(input_img, NULL, screen, NULL); { unsigned int i, j; Uint32 p; for(i=0; i<IMG_H; i++){ for(j=0; j<IMG_W; j++){ in.d[i*in.w + j] = (unsigned char)get_pixel32(screen, j, i); } } } #else #endif init_hough(&HT); #if PRINT_TIME clock_t start = clock(); #endif unsigned int i; for(i=0; i<ITERATIONS; i++){ gaussian_noise_reduce(&in, &gauss); /* Pre-edge detection: some blurring */ canny_edge_detect(&gauss, &out); /* Actual edge detection */ init_hough(&HT); hough_transform(&out, &HT); /* Transform */ hough_getlines(HT.max/3, &HT); /* Analyze the accumulator, fill the detected lines */ } #if PRINT_TIME printf("Hough transform - time elapsed: %f\n", ((double)clock() - start) / CLOCKS_PER_SEC); #endif #if DEBUG printf("Accumulator info:\n \timage \tw: %d, h: %d\n", HT.img_w, HT.img_h); printf("\taccum \tw: %d, h: %d, max: %d\n", HT.w, HT.h, HT.max); #endif #if DRAW_ACCUM /* Draw the accumulator */ screen = SDL_SetVideoMode(HT.w, HT.h, 32, SDL_HWSURFACE); /* Set the window to accumulator's size */ { unsigned int i, j; Uint32 p; for(i=0; i<HT.h; i++){ /* Loop through every pixel */ for(j=0; j<HT.w; j++){ p = 0xff*(1-(float)HT.accu[i*HT.w + j]/(float)HT.max); /* Maximize contrast */ put_pixel32(screen, j, i, (0xff000000 | (p << 16) | (p << 8) | p)); /* Draw each pixel */ } } } SDL_Flip(screen); /* Flush to window */ #if WAIT_KEY wait(); #endif #endif #if DRAW_OUTPUT screen = SDL_SetVideoMode(IMG_W, IMG_H, 32, SDL_HWSURFACE); { unsigned int i, j; Uint32 p; for(i=0; i<IMG_H; i++){ for(j=0; j<IMG_W; j++){ p = out.d[i*out.w + j]; put_pixel32(screen, j, i, (0xff000000 | (p << 16) | (p << 8) | p)); } } for(i=0; i<HT.l->count; i++){ /* Draw all the detected lines */ lineRGBA(screen, HT.l->l[i].x1, HT.l->l[i].y1, HT.l->l[i].x2, HT.l->l[i].y2, 0xff, 0, 0, 0xff); } } SDL_Flip(screen); #if WAIT_KEY wait(); #endif #endif #if LOAD_BMP_SDL SDL_FreeSurface(input_img); #endif #if SDL_USED SDL_FreeSurface(screen); SDL_Quit(); #endif #if !STATIC_ALLOC free(in.d); free(gauss.d); free(out.d); free(HT.accu); #endif return 0; }