int dcolusion() { Uint32 pixel; Uint8 r,g,b; for (int x=0;x<14;x++) { for (int y=0;y<16;y++) { pixel = get_pixel32(screen,pacman->x+x,pacman->y+y+2); SDL_GetRGB(pixel,screen->format,&r,&g,&b); if (r == 0 && g == 0 && b==0xff) return 1; if (r == 0 && g == 0 && b == 0) { pixel = get_pixel32(screen,pacman->x+x,pacman->y+y+3); SDL_GetRGB(pixel,screen->format,&r,&g,&b); } } } return 0; }
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; }
SDL_Surface *flip_surface(SDL_Surface *surface, int flags) { SDL_Surface *flipped = NULL; // Alpha? Colour-keyed? Uint32 amask = (surface->flags & SDL_SRCCOLORKEY) ? 0 : surface->format->Amask; flipped = SDL_CreateRGBSurface(SDL_SWSURFACE, surface->w, surface->h, surface->format->BitsPerPixel, surface->format->Rmask, surface->format->Gmask, surface->format->Bmask, amask); printf("w=%d, h=%d\n", flipped->w, flipped->h); // Lock surface if need be if(SDL_MUSTLOCK(surface)) SDL_LockSurface(surface); for(size_t x = 0, rx = flipped->w-1; x < (size_t)flipped->w; x++, rx--) for(size_t y = 0, ry = flipped->h-1; y < (size_t)flipped->h; y++, ry--) { size_t dest_x = (flags & FLIP_HORIZONTAL) ? rx : x, dest_y = (flags & FLIP_VERTICAL) ? ry : y; set_pixel32(flipped, dest_x, dest_y, get_pixel32(surface, x, y)); } // Unlock surface if need be if(SDL_MUSTLOCK(surface)) SDL_UnlockSurface(surface); // Copy colour-key if(surface->flags & SDL_SRCCOLORKEY) SDL_SetColorKey(flipped, SDL_RLEACCEL|SDL_SRCCOLORKEY, surface->format->colorkey); // finished :) return flipped; }
bool CollisionAtPixel (int X, int Y, SDL_Surface* surface, int face) { Uint8 r,g,b; Uint32 xxx; int DisplacementX = 0; int DisplacementY = 0; switch (face) { case 1:DisplacementY = -1;break; case 2:DisplacementX = -1;break; case 3:DisplacementY = 1;break; case 4:DisplacementX = 1;break; } SDL_LockSurface(surface); //lock the surface xxx = get_pixel32(surface,X+DisplacementX,Y+DisplacementY); // get the pixel colour on the surface at the x,y cor-ords SDL_UnlockSurface(surface); // unlock the surface SDL_GetRGB ( xxx , surface->format, &r, &g, &b); // convert the colour to RGB values if(r != 0 && g != 0 && b != 0) { return true; } return false; }
int load_font(struct font* font, const char* filepath, int ncols, int nrows) { uint32_t* pixels = NULL; int pitch = 0; uint32_t bg_color; int cell_w, cell_h, top, base_a, curr_char; int rows; int i; if (init_image_from_file(&font->image, filepath) == -1) return -1; if ((font->image.width / ncols) * (font->image.height / nrows) > MAX_FONT_CHARS) return -1; if (lock_image(&font->image, (void**)&pixels, &pitch) == -1) return -1; bg_color = pixels[0]; cell_w = font->image.width / ncols; cell_h = font->image.height / nrows; top = cell_h; base_a = cell_h; curr_char = 0; for (rows = 0; rows < nrows; ++rows) { int cols; for (cols = 0; cols < ncols; ++cols) { int p_col, p_row; font->chars_rects[curr_char].x = cell_w * cols; font->chars_rects[curr_char].y = cell_h * rows; font->chars_rects[curr_char].w = cell_w; font->chars_rects[curr_char].h = cell_h; for (p_col = 0; p_col < cell_w; ++p_col) { for (p_row = 0; p_row < cell_h; ++p_row) { int px = (cell_w * cols) + p_col; int py = (cell_h * rows) + p_row; if (get_pixel32(pixels, pitch, px, py) != bg_color) { font->chars_rects[curr_char].x = px; p_col = cell_w; p_row = cell_h; } } } for (p_col = cell_w - 1; p_col >= 0; --p_col) { for (p_row = 0; p_row < cell_h; ++p_row) { int px = (cell_w * cols) + p_col; int py = (cell_h * rows) + p_row; if (get_pixel32(pixels, pitch, px, py) != bg_color) { font->chars_rects[curr_char].w = (px - font->chars_rects[curr_char].x) + 1; p_col = -1; p_row = cell_h; } } } for (p_row = 0; p_row < cell_h; ++p_row) { for (p_col = 0; p_col < cell_w; ++p_col) { int px = (cell_w * cols) + p_col; int py = (cell_h * rows) + p_row; if (get_pixel32(pixels, pitch, px, py) != bg_color) { if (p_row < top) { top = p_row; } /* Break the loops */ p_col = cell_w; p_row = cell_h; } } } if (curr_char == 'A') { for (p_row = cell_h - 1; p_row >= 0; --p_row) { for (p_col = 0; p_col < cell_w; ++p_col) { int px = (cell_w * cols) + p_col; int py = (cell_h * rows) + p_row; if (get_pixel32(pixels, pitch, px, py) != bg_color) { base_a = p_row; p_col = cell_w; p_row = -1; } } } } ++curr_char; } } font->space_width = cell_w / 2; font->line_height = base_a - top + 2; font->char_spacing = 0; font->line_spacing = 0; for (i = 0; i < ncols * nrows; ++i) { font->chars_rects[i].y += top; font->chars_rects[i].h -= top; } unlock_image(&font->image); return 0; }
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; }
void BitmapFont::build_font( SDL_Surface *surface ) { //Si la surface est NULL if( surface == NULL ) { return; } //Recupere le bitmap bitmap = surface; //Mise en place de la couleur du background (fond) Uint32 bgColor = SDL_MapRGB( bitmap->format, 0, 0xFF, 0xFF ); //Dimension des cellules int cellW = bitmap->w / 16; int cellH = bitmap->h / 16; //Le caractere courant int currentChar = 0; //On parcours les lignes des cellules for( int rows = 0; rows < 16; rows++ ) { //On parcours les colonnes des cellules for( int cols = 0; cols < 16; cols++ ) { //caractere courant chars[ currentChar ].x = cellW * cols; chars[ currentChar ].y = cellH * rows; //Mise en place des dimensions du caractere chars[ currentChar ].w = cellW; chars[ currentChar ].h = cellH; //On parcours les colonnes des pixels for( int pCol = 0; pCol < cellW; pCol++ ) { //On parcours les lignes des pixels for( int pRow = 0; pRow < cellH; pRow++ ) { //Recupere les coordonnees du pixel int pX = ( cellW * cols ) + pCol; int pY = ( cellH * rows ) + pRow; //Si un pixel non "colorkey" est trouve if( get_pixel32( pX, pY, bitmap ) != bgColor ) { //Coordonnee x chars[ currentChar ].x = pX; //On arrete la boucle pCol = cellW; pRow = cellH; } } } //On parcours les colonnes des pixels for( int pCol_w = cellW - 1; pCol_w >= 0; pCol_w-- ) { //On parcours les lignes des pixels for( int pRow_w = 0; pRow_w < cellH; pRow_w++ ) { //Recupere les coordonnees du pixel int pX = ( cellW * cols ) + pCol_w; int pY = ( cellH * rows ) + pRow_w; //Si un pixel non "colorkey" est trouve if( get_pixel32( pX, pY, bitmap ) != bgColor ) { //longueur du caractere courant chars[ currentChar ].w = ( pX - chars[ currentChar ].x ) + 1; //On arrete la boucle pCol_w = -1; pRow_w = cellH; } } } //caractere suivant currentChar++; } } }
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; }