surface surface::load(std::streambuf* buf,string type) { surface surface; SDL_RWops* wop = build_rwops_stream(buf); struct noname { SDL_RWops* woop; ~noname() { if(woop!=NULL) SDL_RWclose(woop); } } destroyer; destroyer.woop = wop; if(type=="auto") surface.build(IMG_Load_RW(wop,false)); else surface.build(IMG_LoadTyped_RW(wop,false,(char*)type.c_str())); return surface; /*int beg = strea.tellg(); strea.seekg(0,ios::end); int size = ((int)strea.tellg())-beg; strea.seekg(beg,ios::beg); vector<char> vec(size); strea.read(&vec[0],size); SDL_RWops* wop = SDL_RWFromConstMem(&vec[0],size); if(wop == NULL) throw exception_sdl(); if(type=="auto") surface.build(IMG_Load_RW(wop,true)); else surface.build(IMG_LoadTyped_RW(wop,true,(char*)type.c_str())); return surface;*/ }
SDL_Surface * BitmapHandler::loadBitmapFromDir(std::string path, std::string fname, bool setKey) { if(!fname.size()) { logGlobal->warnStream() << "Call to loadBitmap with void fname!"; return NULL; } if (!CResourceHandler::get()->existsResource(ResourceID(path + fname, EResType::IMAGE))) { return nullptr; } SDL_Surface * ret=NULL; auto readFile = CResourceHandler::get()->loadData( ResourceID(path + fname, EResType::IMAGE)); if (isPCX(readFile.first.get())) { //H3-style PCX ret = loadH3PCX(readFile.first.get(), readFile.second); if (ret) { if(ret->format->BytesPerPixel == 1 && setKey) { const SDL_Color &c = ret->format->palette->colors[0]; SDL_SetColorKey(ret,SDL_SRCCOLORKEY,SDL_MapRGB(ret->format, c.r, c.g, c.b)); } } else logGlobal->errorStream()<<"Failed to open "<<fname<<" as H3 PCX!"; } else { //loading via SDL_Image CFileInfo info(CResourceHandler::get()->getResourceName(ResourceID(path + fname, EResType::IMAGE))); ret = IMG_LoadTyped_RW( //create SDL_RW with our data (will be deleted by SDL) SDL_RWFromConstMem((void*)readFile.first.get(), readFile.second), 1, // mark it for auto-deleting &info.getExtension()[0] + 1); //pass extension without dot (+1 character) if (ret) { if (ret->format->palette) { //set correct value for alpha\unused channel for (int i=0; i< ret->format->palette->ncolors; i++) ret->format->palette->colors[i].unused = 255; } } else { logGlobal->errorStream()<<"Failed to open "<<fname<<" via SDL_Image"; } } SDL_SetColorKey(ret, SDL_SRCCOLORKEY, SDL_MapRGB(ret->format, 0, 255, 255)); return ret; }
SDL_Surface *loadImageFromResource(const char *RES_ID) { unique_ptr<buffer> data = loadResourceBuffer(RES_ID); if (data) { SDL_Surface *surf = IMG_LoadTyped_RW(data->rw, 0, "PNG"); if (!surf) { fprintf(stderr, "Could not load image %s: %s\n", RES_ID, IMG_GetError()); } return surf; } return nullptr; }
bool Image::Load(SDL_RWops *src, int freesrc, char *type) { SDL_Surface *temp = IMG_LoadTyped_RW(src, freesrc, type); if(temp == NULL) return false; else m_Surface = temp; return true; }
Bitmap::Bitmap(const char *filename) { SDL_RWops ops; const char *extension; shState->fileSystem().openRead(ops, filename, FileSystem::Image, false, &extension); SDL_Surface *imgSurf = IMG_LoadTyped_RW(&ops, 1, extension); if (!imgSurf) throw Exception(Exception::SDLError, "Error loading image '%s': %s", filename, SDL_GetError()); p->ensureFormat(imgSurf, SDL_PIXELFORMAT_ABGR8888); if (imgSurf->w > glState.caps.maxTexSize || imgSurf->h > glState.caps.maxTexSize) { /* Mega surface */ p = new BitmapPrivate; p->megaSurface = imgSurf; } else { /* Regular surface */ TEXFBO tex; try { tex = shState->texPool().request(imgSurf->w, imgSurf->h); } catch (const Exception &e) { SDL_FreeSurface(imgSurf); throw e; } p = new BitmapPrivate; p->gl = tex; TEX::bind(p->gl.tex); TEX::uploadImage(p->gl.width, p->gl.height, imgSurf->pixels, GL_RGBA); SDL_FreeSurface(imgSurf); } p->addTaintedArea(rect()); }
// Called by Rocket when a texture is required by the library. bool RocketSDL2Renderer::LoadTexture(Rocket::Core::TextureHandle& texture_handle, Rocket::Core::Vector2i& texture_dimensions, const Rocket::Core::String& source) { Rocket::Core::FileInterface* file_interface = Rocket::Core::GetFileInterface(); Rocket::Core::FileHandle file_handle = file_interface->Open(source); if (!file_handle) return false; file_interface->Seek(file_handle, 0, SEEK_END); size_t buffer_size = file_interface->Tell(file_handle); file_interface->Seek(file_handle, 0, SEEK_SET); char* buffer = new char[buffer_size]; file_interface->Read(buffer, buffer_size, file_handle); file_interface->Close(file_handle); size_t i; for(i = source.Length() - 1; i > 0; i--) { if(source[i] == '.') break; } Rocket::Core::String extension = source.Substring(i+1, source.Length()-i); SDL_Surface* surface = IMG_LoadTyped_RW(SDL_RWFromMem(buffer, buffer_size), 1, extension.CString()); if (surface) { SDL_Texture *texture = SDL_CreateTextureFromSurface(mRenderer, surface); if (texture) { texture_handle = (Rocket::Core::TextureHandle) texture; texture_dimensions = Rocket::Core::Vector2i(surface->w, surface->h); SDL_FreeSurface(surface); } else { return false; } return true; } return false; }
// Apple provides both stream and file loading functions in ImageIO. // Potentially, Apple can optimize for either case. SDL_Surface* IMG_Load(const char *file) { SDL_Surface* sdl_surface = NULL; sdl_surface = LoadImageFromFile(file); if(NULL == sdl_surface) { // Either the file doesn't exist or ImageIO doesn't understand the format. // For the latter case, fallback to the native SDL_image handlers. SDL_RWops *src = SDL_RWFromFile(file, "rb"); char *ext = strrchr(file, '.'); if(ext) { ext++; } if(!src) { /* The error message has been set in SDL_RWFromFile */ return NULL; } sdl_surface = IMG_LoadTyped_RW(src, 1, ext); } return sdl_surface; }
void broov_gui_init_audio_image(int audio_image_idx) { broov_gui_clean_audio_image(); g_audio_image_idx = audio_image_idx; switch (audio_image_idx) { case BROOV_LOADING_IMAGE: default: rw4 = SDL_RWFromMem(bg_loading, bg_loading_len); break; } if (rw4) { char image[] = "JPG"; SDL_Surface *backgroundImage = IMG_LoadTyped_RW(rw4, 1, image); if (backgroundImage) { backgroundTexture = SDL_CreateTextureFromSurface(0, backgroundImage); } } }
bool Image::load_memory( const char* buffer, int buffer_size, const std::string& ext, uint32 pixel_format ) { SDL_SURFACE::SharedPtr surface; if( buffer == nullptr ) { NOM_LOG_ERR( NOM, "Cannot load image source from memory: buffer == nullptr" ); return false; } if( buffer_size == 0 ) { NOM_LOG_WARN( NOM, "Cannot load image source from memory: buffer_size == 0" ); return false; } surface.reset( IMG_LoadTyped_RW( SDL_RWFromMem( (char*) buffer, buffer_size), 1, ext.c_str() ), priv::FreeSurface ); if( surface != nullptr ) { this->image_.reset( SDL_ConvertSurfaceFormat( surface.get(), pixel_format, 0), priv::FreeSurface ); return true; } else { NOM_LOG_ERR( NOM, IMG_GetError() ); return false; } }
SpriteSheet::SpriteSheet ( const std::string& name ) : texID(0) { std::string configPath, imagePath; if (name[0] == '+') { configPath = ""; imagePath = "Images/" + name.substr(1, std::string::npos) + ".png"; } else { if (Preferences::Get("Sprite/Original", "blah") == "blah") //[ALISTAIR]: Why doesn't this work? { configPath = "NewResources/Sprites/" + name + ".xml"; imagePath = "NewResources/Sprites/" + name + ".png"; SDL_RWops* imageRWOps = ResourceManager::OpenFile(imagePath); if (!imageRWOps) { configPath = "Sprites/" + name + ".xml"; imagePath = "Sprites/" + name + ".png"; } } else { configPath = "Sprites/" + name + ".xml"; imagePath = "Sprites/" + name + ".png"; } } SDL_RWops* configRWOps = ResourceManager::OpenFile(configPath); SDL_RWops* imageRWOps = ResourceManager::OpenFile(imagePath); if (!imageRWOps) { // TODO: make this work gracefully LOG("Graphics::SpriteSheet", LOG_ERROR, "Failed to load image: %s", name.c_str()); exit(1); delete configRWOps; } surface = IMG_LoadTyped_RW(imageRWOps, 1, const_cast<char*>("PNG")); if (configRWOps) { // load config size_t confLength; char* confData = (char*)ResourceManager::ReadFull(&confLength, configRWOps, 1); TiXmlDocument* xmlDoc = new TiXmlDocument(name + ".xml"); confData = (char*)realloc(confData, confLength + 1); confData[confLength] = '\0'; xmlDoc->Parse(confData); free(confData); if (xmlDoc->Error()) { LOG("Graphics::SpriteSheet", LOG_ERROR, "XML error: %s", xmlDoc->ErrorDesc()); exit(1); } TiXmlElement* root = xmlDoc->RootElement(); assert(root); TiXmlElement* dimensions = root->FirstChild("dimensions")->ToElement(); assert(dimensions); TiXmlElement* scale = root->FirstChild("scale")->ToElement(); int rc; if (scale) { rc = scale->QueryFloatAttribute("factor", &scaleFactor); if (rc != TIXML_SUCCESS) scaleFactor = 1.0f; } else { scaleFactor = 1.0f; } rc = dimensions->QueryIntAttribute("x", &sheetTilesX); assert(rc == TIXML_SUCCESS); rc = dimensions->QueryIntAttribute("y", &sheetTilesY); assert(rc == TIXML_SUCCESS); assert(sheetTilesX > 0); assert(sheetTilesY > 0); tileSizeX = surface->w / sheetTilesX; tileSizeY = surface->h / sheetTilesY; if (root->FirstChild("rotational")) rotational = true; else rotational = false; delete xmlDoc; } else { // assume it's just one sprite sheetTilesX = 1; sheetTilesY = 1; rotational = false; tileSizeX = surface->w; tileSizeY = surface->h; scaleFactor = 1.0f; } }
/**************************************************************************** * DecodeBlock: the whole thing **************************************************************************** * This function must be fed with a complete compressed frame. ****************************************************************************/ static picture_t *DecodeBlock( decoder_t *p_dec, block_t **pp_block ) { decoder_sys_t *p_sys = p_dec->p_sys; block_t *p_block; picture_t *p_pic = NULL; SDL_Surface *p_surface; SDL_RWops *p_rw; if( pp_block == NULL || *pp_block == NULL ) return NULL; p_block = *pp_block; if( p_block->i_flags & BLOCK_FLAG_DISCONTINUITY ) { block_Release( p_block ); *pp_block = NULL; return NULL; } p_rw = SDL_RWFromConstMem( p_block->p_buffer, p_block->i_buffer ); /* Decode picture. */ p_surface = IMG_LoadTyped_RW( p_rw, 1, (char*)p_sys->psz_sdl_type ); if ( p_surface == NULL ) { msg_Warn( p_dec, "SDL_image couldn't load the image (%s)", IMG_GetError() ); goto error; } switch ( p_surface->format->BitsPerPixel ) { case 16: p_dec->fmt_out.i_codec = VLC_CODEC_RGB16; break; case 8: case 24: p_dec->fmt_out.i_codec = VLC_CODEC_RGB24; break; case 32: p_dec->fmt_out.i_codec = VLC_CODEC_RGB32; break; default: msg_Warn( p_dec, "unknown bits/pixel format (%d)", p_surface->format->BitsPerPixel ); goto error; } p_dec->fmt_out.video.i_width = p_surface->w; p_dec->fmt_out.video.i_height = p_surface->h; p_dec->fmt_out.video.i_aspect = VOUT_ASPECT_FACTOR * p_surface->w / p_surface->h; /* Get a new picture. */ p_pic = decoder_NewPicture( p_dec ); if ( p_pic == NULL ) goto error; switch ( p_surface->format->BitsPerPixel ) { case 8: { int i, j; uint8_t *p_src, *p_dst; uint8_t r, g, b; for ( i = 0; i < p_surface->h; i++ ) { p_src = (uint8_t*)p_surface->pixels + i * p_surface->pitch; p_dst = p_pic->p[0].p_pixels + i * p_pic->p[0].i_pitch; for ( j = 0; j < p_surface->w; j++ ) { SDL_GetRGB( *(p_src++), p_surface->format, &r, &g, &b ); *(p_dst++) = r; *(p_dst++) = g; *(p_dst++) = b; } } break; } case 16: { int i; uint8_t *p_src = p_surface->pixels; uint8_t *p_dst = p_pic->p[0].p_pixels; int i_pitch = p_pic->p[0].i_pitch < p_surface->pitch ? p_pic->p[0].i_pitch : p_surface->pitch; for ( i = 0; i < p_surface->h; i++ ) { vlc_memcpy( p_dst, p_src, i_pitch ); p_src += p_surface->pitch; p_dst += p_pic->p[0].i_pitch; } break; } case 24: { int i, j; uint8_t *p_src, *p_dst; uint8_t r, g, b; for ( i = 0; i < p_surface->h; i++ ) { p_src = (uint8_t*)p_surface->pixels + i * p_surface->pitch; p_dst = p_pic->p[0].p_pixels + i * p_pic->p[0].i_pitch; for ( j = 0; j < p_surface->w; j++ ) { SDL_GetRGB( *(uint32_t*)p_src, p_surface->format, &r, &g, &b ); *(p_dst++) = r; *(p_dst++) = g; *(p_dst++) = b; p_src += 3; } } break; } case 32: { int i, j; uint8_t *p_src, *p_dst; uint8_t r, g, b, a; for ( i = 0; i < p_surface->h; i++ ) { p_src = (uint8_t*)p_surface->pixels + i * p_surface->pitch; p_dst = p_pic->p[0].p_pixels + i * p_pic->p[0].i_pitch; for ( j = 0; j < p_surface->w; j++ ) { SDL_GetRGBA( *(uint32_t*)p_src, p_surface->format, &r, &g, &b, &a ); *(p_dst++) = b; *(p_dst++) = g; *(p_dst++) = r; *(p_dst++) = a; p_src += 4; } } break; } } p_pic->date = p_block->i_pts > 0 ? p_block->i_pts : p_block->i_dts; SDL_FreeSurface( p_surface ); block_Release( p_block ); *pp_block = NULL; return p_pic; error: if ( p_surface != NULL ) SDL_FreeSurface( p_surface ); block_Release( p_block ); *pp_block = NULL; return NULL; }
/**************************************************************************** * DecodeBlock: the whole thing **************************************************************************** * This function must be fed with a complete compressed frame. ****************************************************************************/ static int DecodeBlock( decoder_t *p_dec, block_t *p_block ) { decoder_sys_t *p_sys = p_dec->p_sys; picture_t *p_pic = NULL; SDL_Surface *p_surface; SDL_RWops *p_rw; if( p_block == NULL ) /* No Drain */ return VLCDEC_SUCCESS; if( p_block->i_flags & BLOCK_FLAG_CORRUPTED ) { block_Release( p_block ); return VLCDEC_SUCCESS; } p_rw = SDL_RWFromConstMem( p_block->p_buffer, p_block->i_buffer ); /* Decode picture. */ p_surface = IMG_LoadTyped_RW( p_rw, 1, (char*)p_sys->psz_sdl_type ); if ( p_surface == NULL ) { msg_Warn( p_dec, "SDL_image couldn't load the image (%s)", IMG_GetError() ); goto error; } switch ( p_surface->format->BitsPerPixel ) { case 16: p_dec->fmt_out.i_codec = VLC_CODEC_RGB16; break; case 8: case 24: p_dec->fmt_out.i_codec = VLC_CODEC_RGB24; break; case 32: p_dec->fmt_out.i_codec = VLC_CODEC_RGB32; break; default: msg_Warn( p_dec, "unknown bits/pixel format (%d)", p_surface->format->BitsPerPixel ); goto error; } p_dec->fmt_out.video.i_width = p_surface->w; p_dec->fmt_out.video.i_height = p_surface->h; p_dec->fmt_out.video.i_sar_num = 1; p_dec->fmt_out.video.i_sar_den = 1; /* Get a new picture. */ if( decoder_UpdateVideoFormat( p_dec ) ) goto error; p_pic = decoder_NewPicture( p_dec ); if ( p_pic == NULL ) goto error; switch ( p_surface->format->BitsPerPixel ) { case 8: { for ( int i = 0; i < p_surface->h; i++ ) { uint8_t *p_src = (uint8_t*)p_surface->pixels + i * p_surface->pitch; uint8_t *p_dst = p_pic->p[0].p_pixels + i * p_pic->p[0].i_pitch; for ( int j = 0; j < p_surface->w; j++ ) { uint8_t r, g, b; SDL_GetRGB( *(p_src++), p_surface->format, &r, &g, &b ); *(p_dst++) = r; *(p_dst++) = g; *(p_dst++) = b; } } break; } case 16: { uint8_t *p_src = p_surface->pixels; uint8_t *p_dst = p_pic->p[0].p_pixels; int i_pitch = p_pic->p[0].i_pitch < p_surface->pitch ? p_pic->p[0].i_pitch : p_surface->pitch; for ( int i = 0; i < p_surface->h; i++ ) { memcpy( p_dst, p_src, i_pitch ); p_src += p_surface->pitch; p_dst += p_pic->p[0].i_pitch; } break; } case 24: { for ( int i = 0; i < p_surface->h; i++ ) { uint8_t *p_src = (uint8_t*)p_surface->pixels + i * p_surface->pitch; uint8_t *p_dst = p_pic->p[0].p_pixels + i * p_pic->p[0].i_pitch; for ( int j = 0; j < p_surface->w; j++ ) { uint8_t r, g, b; SDL_GetRGB( *(uint32_t*)p_src, p_surface->format, &r, &g, &b ); *(p_dst++) = r; *(p_dst++) = g; *(p_dst++) = b; p_src += 3; } } break; } case 32: { for ( int i = 0; i < p_surface->h; i++ ) { uint8_t *p_src = (uint8_t*)p_surface->pixels + i * p_surface->pitch; uint8_t *p_dst = p_pic->p[0].p_pixels + i * p_pic->p[0].i_pitch; for ( int j = 0; j < p_surface->w; j++ ) { uint8_t r, g, b, a; SDL_GetRGBA( *(uint32_t*)p_src, p_surface->format, &r, &g, &b, &a ); *(p_dst++) = b; *(p_dst++) = g; *(p_dst++) = r; *(p_dst++) = a; p_src += 4; } } break; } } p_pic->date = (p_block->i_pts != VLC_TICK_INVALID) ? p_block->i_pts : p_block->i_dts; decoder_QueueVideo( p_dec, p_pic ); error: if ( p_surface != NULL ) SDL_FreeSurface( p_surface ); block_Release( p_block ); return VLCDEC_SUCCESS; }
bool tryRead(SDL_RWops &ops, const char *ext) { surf = IMG_LoadTyped_RW(&ops, 1, ext); return surf != 0; }
int main(int argc, char *argv[]) { int i, done = 0, flip = 0; SDL_Init(SDL_INIT_VIDEO); atexit(SDL_Quit); if ((IMG_Init(IMG_INIT_PNG) & IMG_INIT_PNG) == 0) { fprintf(stderr, "Failed to init PNG support.\n"); exit(1); } Uint32 flags = SDL_HWSURFACE; for (i = 1; i < argc; i++) { if (strcmp(argv[i], "-fullscreen") == 0) { flags |= SDL_FULLSCREEN; SDL_ShowCursor(SDL_DISABLE); } if (strcmp(argv[i], "-doublebuf") == 0) { flags |= SDL_DOUBLEBUF; } if (strcmp(argv[i], "-flip") == 0) { flip = 1; } } SDL_Surface *screen = SDL_SetVideoMode(SCRN_WIDTH, SCRN_HEIGHT, SCRN_DEPTH, flags); if (screen == NULL) { fprintf(stderr, "Failed to set video mode.\n"); exit(1); } SDL_WM_SetCaption(TITLE, *argv); SDL_RWops *rw1 = SDL_RWFromMem(moire1_start, moire1_size); SDL_RWops *rw2 = SDL_RWFromMem(moire2_start, moire2_size); SDL_Surface *surface1 = IMG_LoadTyped_RW(rw1, 1, "png"); SDL_Surface *surface2 = IMG_LoadTyped_RW(rw2, 1, "png"); SDL_SetAlpha(surface1, SDL_RLEACCEL | SDL_SRCALPHA, 0); SDL_SetAlpha(surface2, SDL_RLEACCEL, 0); i = 0; while (!done) { static SDL_Event event; if (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: done = 1; break; case SDL_KEYDOWN: done = event.key.keysym.sym == SDLK_ESCAPE; break; } } else { int ms = SDL_GetTicks(); blit_all(screen, surface1, surface2, SPEED * ms / 1000); SDL_UpdateRect(screen, 0, 0, 0, 0); if (flip) { SDL_Flip(screen); } SDL_Delay(10); } } return 0; }