extern int glimageLoadAndBind (const char * fname, GLuint *tex) { SDL_Surface * texSurface = NULL; SDL_RWops *rwop = NULL; char ShouldBeFlipped = 0; int flags= IMG_INIT_JPG | IMG_INIT_PNG; int initted= IMG_Init(flags); int sign = (SDL_BYTEORDER == SDL_BIG_ENDIAN) ? 1 : -1; printf("%s\n", fname); if((initted & flags) != flags) { printf("IMG_Init: Failed to init required jpg and png support!\n"); printf("IMG_Init: %s\n", IMG_GetError()); return 0; } if((texSurface = IMG_Load(fname)) == NULL ) { fprintf(stderr, "Impossible d'ouvrir le fichier : %s\n", IMG_GetError()); return 0; } rwop=SDL_RWFromFile(fname, "rb"); //Si l'image est en JPEG ou PNG, il faut inverser if (IMG_isJPG(rwop) || IMG_isBMP(rwop) || IMG_isPNG(rwop)) ShouldBeFlipped = 1; if (ShouldBeFlipped) //invert_surface_vertical(texSurface); FlipVertically(texSurface); IMG_Quit(); glGenTextures(1, tex); glBindTexture(GL_TEXTURE_2D, *tex); if (texSurface->format->BytesPerPixel == 3){ if (sign * texSurface->format->Rshift > sign * texSurface->format->Bshift) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texSurface->w, texSurface->h, 0, GL_RGB, GL_UNSIGNED_BYTE, texSurface->pixels); else glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texSurface->w, texSurface->h, 0, GL_BGR, GL_UNSIGNED_BYTE, texSurface->pixels); } if (texSurface->format->BytesPerPixel == 4){ if (sign * texSurface->format->Rshift > sign * texSurface->format->Bshift) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texSurface->w, texSurface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, texSurface->pixels); else glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texSurface->w, texSurface->h, 0, GL_BGRA, GL_UNSIGNED_BYTE, texSurface->pixels); } SDL_FreeSurface(texSurface); return 1; }
static void LoadArchivePics( PicManager *pm, const char *archive, const char *dirname) { char *buf = NULL; char path[CDOGS_PATH_MAX]; sprintf(path, "%s/%s", archive, dirname); tinydir_dir dir; if (tinydir_open(&dir, path) != 0) { LOG(LM_MAP, LL_DEBUG, "no pic dir(%s): %s", path, strerror(errno)); goto bail; } while (dir.has_next) { tinydir_file file; if (tinydir_readfile(&dir, &file) != 0) { LOG(LM_MAP, LL_WARN, "cannot read file: %s", strerror(errno)); break; } if (!file.is_reg) goto nextFile; long len; buf = ReadFileIntoBuf(file.path, "rb", &len); if (buf == NULL) goto nextFile; SDL_RWops *rwops = SDL_RWFromMem(buf, len); bool isPng = IMG_isPNG(rwops); if (isPng) { SDL_Surface *data = IMG_Load_RW(rwops, 0); if (data != NULL) { char nameBuf[CDOGS_FILENAME_MAX]; PathGetBasenameWithoutExtension(nameBuf, file.path); PicManagerAdd(&pm->customPics, &pm->customSprites, nameBuf, data); } } rwops->close(rwops); nextFile: CFREE(buf); buf = NULL; if (tinydir_next(&dir) != 0) { printf( "Could not go to next file in dir %s: %s\n", path, strerror(errno)); goto bail; } } bail: CFREE(buf); tinydir_close(&dir); }
LuxSprite * Lux_OGL_PNGtoSprite( uint8_t * data, uint32_t size ) { LuxSprite * sprite = new LuxSprite; SDL_RWops * src = SDL_RWFromMem(data, size); if ( IMG_isPNG(src) ) { SDL_Surface * surface = IMG_Load_RW(src, 0); if ( surface ) { SDL_SetAlpha(surface, 0, 255); Texture * temp_texure = new Texture; glGenTextures(1, &temp_texure->texnum); glBindTexture(GL_TEXTURE_2D, temp_texure->texnum); if ( Lux_OGL_QueryExtension("GL_ARB_texture_non_power_of_two") ) { temp_texure->tw = surface->w; temp_texure->th = surface->h; } else { temp_texure->tw = Lux_OGL_PowerOfTwo( surface->w ); temp_texure->th = Lux_OGL_PowerOfTwo( surface->h ); } temp_texure->w = surface->w; temp_texure->h = surface->h; temp_texure->pot = ( temp_texure->tw == temp_texure->w && temp_texure->th == temp_texure->h) ? true : false; SDL_Surface * temp_sheet = SDL_CreateRGBSurface( oglGraphics_flags, temp_texure->tw, temp_texure->th, 32, RMASK, GMASK, BMASK, AMASK ); SDL_BlitSurface( surface, NULL, temp_sheet, NULL ); SDL_SetAlpha(temp_sheet, 0, 0); glTexImage2D(GL_TEXTURE_2D, 0, 4, temp_sheet->w, temp_sheet->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, temp_sheet->pixels); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST); sprite->data = (void*) temp_texure; SDL_FreeSurface(temp_sheet); SDL_FreeSurface(surface); //Lux_OGL_ScreenSuface( (Texture*)sprite->data ); return sprite; } else { lux::core->SystemMessage(SYSTEM_MESSAGE_ERROR) << "Lux_OGL_PNGtoSprite failed" << std::endl; } } delete sprite; return NULL; }
SDL_Surface * Lux_OGL_LoadSpriteImage(std::string file) { uint8_t * data = NULL; uint32_t size; SDL_Surface * temp_surface = NULL; if ( lux::game ) { size = lux::game->GetFile(file, &data, false); if ( size ) { SDL_RWops * src = SDL_RWFromMem(data, size); if ( IMG_isPNG(src) ) { temp_surface = IMG_Load_RW(src, 1); SDL_SetAlpha(temp_surface, 0, 255); } else lux::core->SystemMessage(SYSTEM_MESSAGE_LOG) << "not a png image" << std::endl; } } return temp_surface; }
/* Check for valid PNG format file. */ int image_sdl_check_png(const char *png_path) { int res; SDL_RWops *rwop; assert(png_path); assert(strlen(png_path) > 0); if(reg_file_exists(png_path) != 1) return -1; rwop = SDL_RWFromFile(png_path, "rb"); if(rwop == NULL) { sg_log_err("SDL_RWFromFile(\"%s\") failure, %s.", png_path, SDL_GetError()); return -1; } res = IMG_isPNG(rwop) ? 1 : 0; SDL_FreeRW(rwop); return res; }
int main(int argc, char *argv[]) { Uint32 flags; SDL_Surface *screen, *image; int i, depth, done; SDL_Event event; #if 0 SDL_RWops* rw_ops; #endif /* Check command line usage */ if ( ! argv[1] ) { fprintf(stderr, "Usage: %s <image_file>\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; for ( i=1; argv[i]; ++i ) { if ( strcmp(argv[i], "-fullscreen") == 0 ) { SDL_ShowCursor(0); flags |= SDL_FULLSCREEN; continue; } #if 0 rw_ops = SDL_RWFromFile(argv[1], "r"); fprintf(stderr, "BMP:\t%d\n", IMG_isBMP(rw_ops)); fprintf(stderr, "GIF:\t%d\n", IMG_isGIF(rw_ops)); fprintf(stderr, "JPG:\t%d\n", IMG_isJPG(rw_ops)); fprintf(stderr, "PNG:\t%d\n", IMG_isPNG(rw_ops)); fprintf(stderr, "TIF:\t%d\n", IMG_isTIF(rw_ops)); /* fprintf(stderr, "TGA:\t%d\n", IMG_isTGA(rw_ops)); */ fprintf(stderr, "PCX:\t%d\n", IMG_isPCX(rw_ops)); #endif /* Open the image file */ #ifdef XPM_INCLUDED image = IMG_ReadXPMFromArray(picture_xpm); #else image = IMG_Load(argv[i]); #endif if ( image == NULL ) { fprintf(stderr, "Couldn't load %s: %s\n", argv[i], SDL_GetError()); continue; } SDL_WM_SetCaption(argv[i], "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()); continue; } /* Set the palette, if one exists */ if ( image->format->palette ) { SDL_SetColors(screen, image->format->palette->colors, 0, image->format->palette->ncolors); } /* Draw a background pattern if the surface has transparency */ if(image->flags & (SDL_SRCALPHA | SDL_SRCCOLORKEY)) draw_background(screen); /* Display the image */ SDL_BlitSurface(image, NULL, screen, NULL); SDL_UpdateRect(screen, 0, 0, 0, 0); done = 0; while ( ! done ) { if ( SDL_PollEvent(&event) ) { switch (event.type) { case SDL_KEYUP: switch (event.key.keysym.sym) { case SDLK_LEFT: if ( i > 1 ) { i -= 2; done = 1; } break; case SDLK_RIGHT: if ( argv[i+1] ) { done = 1; } break; case SDLK_ESCAPE: case SDLK_q: argv[i+1] = NULL; /* Drop through to done */ case SDLK_SPACE: case SDLK_TAB: done = 1; break; default: break; } break; case SDL_MOUSEBUTTONDOWN: done = 1; break; case SDL_QUIT: argv[i+1] = NULL; done = 1; break; default: break; } } else { SDL_Delay(10); } } SDL_FreeSurface(image); } /* We're done! */ SDL_Quit(); return(0); }
bool Image::isPNG(SDL_RWops *src) { return IMG_isPNG(src); }
static void PicManagerLoadDirImpl( PicManager *pm, const char *path, const char *prefix) { tinydir_dir dir; if (tinydir_open(&dir, path) == -1) { perror("Cannot open image dir"); goto bail; } for (; dir.has_next; tinydir_next(&dir)) { tinydir_file file; if (tinydir_readfile(&dir, &file) == -1) { perror("Cannot read image file"); goto bail; } if (file.is_reg) { SDL_RWops *rwops = SDL_RWFromFile(file.path, "rb"); const bool isPng = IMG_isPNG(rwops); if (isPng) { SDL_Surface *data = IMG_Load_RW(rwops, 0); if (!data) { perror("Cannot load image"); fprintf(stderr, "IMG_Load: %s\n", IMG_GetError()); } else { char buf[CDOGS_PATH_MAX]; if (prefix) { char buf1[CDOGS_PATH_MAX]; sprintf(buf1, "%s/%s", prefix, file.name); PathGetWithoutExtension(buf, buf1); } else { PathGetBasenameWithoutExtension(buf, file.name); } PicManagerAdd(&pm->pics, &pm->sprites, buf, data); } } rwops->close(rwops); } else if (file.is_dir && file.name[0] != '.') { if (prefix) { char buf[CDOGS_PATH_MAX]; sprintf(buf, "%s/%s", prefix, file.name); PicManagerLoadDirImpl(pm, file.path, buf); } else { PicManagerLoadDirImpl(pm, file.path, file.name); } } } bail: tinydir_close(&dir); }