ImageBasedDensityProvider::ImageBasedDensityProvider(const std::string filename, const AABB model_aabb) { int desired_channel_count = 0; // keep original amount of channels int img_x, img_y, img_z; // stbi requires pointer to int rather than to coord_t image = stbi_load(filename.c_str(), &img_x, &img_y, &img_z, desired_channel_count); image_size = Point3(img_x, img_y, img_z); if (!image) { const char* reason = "[unknown reason]"; if (stbi_failure_reason()) { reason = stbi_failure_reason(); } logError("Cannot load image %s: '%s'.\n", filename.c_str(), reason); std::exit(-1); } { // compute aabb Point middle = model_aabb.getMiddle(); Point model_aabb_size = model_aabb.max - model_aabb.min; Point image_size2 = Point(image_size.x, image_size.y); float aabb_aspect_ratio = float(model_aabb_size.X) / float(model_aabb_size.Y); float image_aspect_ratio = float(image_size.x) / float(image_size.y); Point aabb_size; if (image_aspect_ratio < aabb_aspect_ratio) { aabb_size = image_size2 * model_aabb_size.X / image_size.x; } else { aabb_size = image_size2 * model_aabb_size.Y / image_size.y; } print_aabb = AABB(middle - aabb_size / 2, middle + aabb_size / 2); assert(aabb_size.X >= model_aabb_size.X && aabb_size.Y >= model_aabb_size.Y); } }
int main(int argc, char** argv) { if (argc < 2) { std::cerr << "Use " << argv[0] << " filename" << std::endl; return 1; } // load image int width, height, nc; float* rgba = stbi_loadf(argv[1], &width, &height, &nc, 4); if (stbi_failure_reason()) { std::cerr << "stbi: " << stbi_failure_reason() << std::endl; return 1; } // create summed area table of luminance image summed_area_table lum_sat; lum_sat.create_lum(rgba, width, height, 4); // apply median cut std::vector<sat_region> regions; median_cut(lum_sat, 9, regions); // max 2^n cuts // create 2d positions from regions std::vector<float2> lights; create_lights(regions, lights); // draw a marker into image for each position size_t i = 0; for (auto l = lights.begin(); l != lights.end(); ++l) { std::cout << "Light " << i++ << ": (" << l->x << ", " << l->y << ")" << std::endl; draw(rgba, width, height, *l); } // save image with marked samples std::vector<unsigned char> conv; conv.resize(width*height*4); for (size_t i = 0; i < width * height * 4; ++i) conv[i] = static_cast<unsigned char>(rgba[i]*255); stbi_write_bmp("test.bmp", width, height, 4, &conv[0]); return 0; }
bool LoadImage( Image* image, const char* vfsPath ) { memset(image, 0, sizeof(Image)); PHYSFS_File* file = PHYSFS_openRead(vfsPath); if(!file) { Error("Can't load '%s': %s", vfsPath, PHYSFS_getLastError()); return false; } stbi_set_flip_vertically_on_load(1); image->data = stbi_load_from_callbacks(&PhysFSCallbacks, file, &image->width, &image->height, &image->channelCount, STBI_default); PHYSFS_close(file); if(!image->data) { Error("Can't load '%s': %s", vfsPath, stbi_failure_reason()); return false; } image->type = GL_UNSIGNED_BYTE; return true; }
void FileImage::load_file() { FSFile fp(filename.c_str(), "r"); if (!fp.is_open()) { printf("Could not open image \"%s\"\n", filename.c_str()); return; } int w, h, channels; image = load_image(fp, fp.get_size(), &w, &h, &channels); width = w; height = h; fp.close(); if (image == NULL) { printf("Could not load image \"%s\": %s\n", filename.c_str(), stbi_failure_reason()); return; } if (!transparent.is_enabled()) return; #ifndef CHOWDREN_FORCE_TRANSPARENT if (channels != 1 && channels != 3) return; #endif set_transparent_color(transparent); }
Stb::ImagePtr Stb::Image::loadFromStream(std::istream & s, Format fmt) { ImagePtr image = std::static_pointer_cast<Image>(std::make_shared<Image::Wrapper>()); try { int imageFmt = UNKNOWN; image->m_Data = stbi_load_from_callbacks(&g_StbCB, &s, &image->m_Width, &image->m_Height, &imageFmt, fmt); if (!image->m_Data) throw std::runtime_error(stbi_failure_reason()); image->m_Format = static_cast<Format>(fmt == UNKNOWN ? imageFmt : fmt); if (image->m_Format == UNKNOWN) throw std::runtime_error("unable to determine pixel format for the image."); } catch (const std::exception & e) { if (image->m_Data) stbi_image_free(image->m_Data); std::clog << "Error reading image: " << e.what() << std::endl; image->m_Data = &g_DummyData; image->m_Width = 0; image->m_Height = 0; image->m_Format = UNKNOWN; } return image; }
bool ImageLoader::loadImageFromFile(const std::string& filename, std::vector<Uint8>& pixels, unsigned int& width, unsigned int& height) { // Clear the array (just in case) pixels.clear(); // Load the image and get a pointer to the pixels in memory int imgWidth, imgHeight, imgChannels; unsigned char* ptr = stbi_load(filename.c_str(), &imgWidth, &imgHeight, &imgChannels, STBI_rgb_alpha); if (ptr && imgWidth && imgHeight) { // Assign the image properties width = imgWidth; height = imgHeight; // Copy the loaded pixels to the pixel buffer pixels.resize(width * height * 4); memcpy(&pixels[0], ptr, pixels.size()); // Free the loaded pixels (they are now in our own pixel buffer) stbi_image_free(ptr); return true; } else { // Error, failed to load the image err() << "Failed to load image \"" << filename << "\". Reason : " << stbi_failure_reason() << std::endl; return false; } }
void Image::load() { flags |= USED; if (tex != 0 || image != NULL) return; if (flags & FILE) { ((FileImage*)this)->load_file(); return; } open_image_file(); image_file.set_item(handle, AssetFile::IMAGE_DATA); FileStream stream(image_file); hotspot_x = stream.read_int16(); hotspot_y = stream.read_int16(); action_x = stream.read_int16(); action_y = stream.read_int16(); int size = stream.read_uint32(); int w, h, channels; image = load_image(image_file, size, &w, &h, &channels); width = w; height = h; if (image == NULL) { std::cout << "Could not load image " << handle << std::endl; std::cout << stbi_failure_reason() << std::endl; return; } }
void Stb::Read(SoyPixelsImpl& Pixels,const ArrayBridge<char>& ArrayBuffer,TReadFunction ReadFunction) { #if defined(USE_STB) const stbi_uc* Buffer = reinterpret_cast<const stbi_uc*>( ArrayBuffer.GetArray() ); auto BufferSize = size_cast<int>( ArrayBuffer.GetDataSize() ); int Width = 0; int Height = 0; int Channels = 0; int RequestedChannels = 4; auto* DecodedPixels = stbi_load_from_memory( Buffer, BufferSize, &Width, &Height, &Channels, RequestedChannels ); if ( !DecodedPixels ) { // gr: as this is not thread safe, it could come out mangled :( maybe add a lock around error popping before read (maybe have to lock around the whole thing) auto* StbError = stbi_failure_reason(); if ( !StbError ) StbError = "Unknown error"; std::stringstream Error; Error << "Failed to read image pixels; " << StbError; throw Soy::AssertException( Error.str() ); } // convert output into pixels // gr: have to assume size auto Format = SoyPixelsFormat::GetFormatFromChannelCount( Channels ); SoyPixelsMeta Meta( Width, Height, Format ); SoyPixelsRemote OutputPixels( DecodedPixels, Meta.GetDataSize(), Meta ); Pixels.Copy( OutputPixels ); #else throw Soy::AssertException("No STB support, no image reading"); #endif }
void StbImageFactory::load(ResourceData *resource) { StbImage* stbresource = static_cast<StbImage*>(resource); int x,y,comp; stbi_uc* data = stbi_load(stbresource->path().toLocal8Bit().data(), &x, &y, &comp, stbresource->m_comp); if(data != 0) { logInfo( "StbImage loaded " << resource->name() << " (" << x << "x" << y << ")"); stbresource->m_data = data; stbresource->m_width = x; stbresource->m_height = y; // If we asked 0 components comp now contains the number of components if(stbresource->m_comp == 0) stbresource->m_comp = comp; stbresource->m_state = StbImage::STATE_LOADED; stbresource->buildTexture(); } else { logWarn( "StbImage failed to load " << resource->name() << "\n" << stbi_failure_reason()); } }
static int load_embedded_image(lua_State *L) { am_check_nargs(L, 1); const char *filename = luaL_checkstring(L, 1); am_embedded_file_record *rec = am_get_embedded_file(filename); if (rec == NULL) { return luaL_error(L, "embedded file not found: %s", filename); } int width, height; int components = 4; stbi_set_flip_vertically_on_load(1); stbi_uc *img_data = stbi_load_from_memory((stbi_uc const *)rec->data, rec->len, &width, &height, &components, 4); if (img_data == NULL) { return luaL_error(L, "error loading image %s: %s", filename, stbi_failure_reason()); } am_image_buffer *img = am_new_userdata(L, am_image_buffer); img->width = width; img->height = height; img->format = AM_PIXEL_FORMAT_RGBA8; int sz = width * height * pixel_format_size(img->format); am_buffer *imgbuf = am_new_userdata(L, am_buffer); imgbuf->size = sz; imgbuf->data = img_data; img->buffer = imgbuf; img->buffer_ref = img->ref(L, -1); lua_pop(L, 1); // pop imgbuf return 1; }
static int load_image(lua_State *L) { am_check_nargs(L, 1); char *errmsg; int len; const char *filename = luaL_checkstring(L, 1); void *data = am_read_resource(filename, &len, &errmsg); if (data == NULL) { lua_pushstring(L, errmsg); free(errmsg); return lua_error(L); } int width, height; int components = 4; stbi_set_flip_vertically_on_load(1); stbi_uc *img_data = stbi_load_from_memory((stbi_uc const *)data, len, &width, &height, &components, 4); free(data); if (img_data == NULL) { return luaL_error(L, "error loading image %s: %s", filename, stbi_failure_reason()); } am_image_buffer *img = am_new_userdata(L, am_image_buffer); img->width = width; img->height = height; img->format = AM_PIXEL_FORMAT_RGBA8; int sz = width * height * pixel_format_size(img->format); am_buffer *imgbuf = am_new_userdata(L, am_buffer); imgbuf->size = sz; imgbuf->data = img_data; img->buffer = imgbuf; img->buffer_ref = img->ref(L, -1); lua_pop(L, 1); // pop imgbuf return 1; }
void cImage::LoadFromPack( cPack * Pack, const std::string& FilePackPath ) { if ( NULL != Pack && Pack->IsOpen() && -1 != Pack->Exists( FilePackPath ) ) { SafeDataPointer PData; Pack->ExtractFileToMemory( FilePackPath, PData ); int w, h, c; Uint8 * data = stbi_load_from_memory( PData.Data, PData.DataSize, &w, &h, &c, mChannels ); if ( NULL != data ) { mPixels = data; mWidth = (eeUint)w; mHeight = (eeUint)h; if ( STBI_default == mChannels ) mChannels = (eeUint)c; mSize = mWidth * mHeight * mChannels; mLoadedFromStbi = true; } else { eePRINTL( "Failed to load image %s. Reason: %s", stbi_failure_reason(), FilePackPath.c_str() ); } } else { eePRINTL( "Failed to load image %s from pack.", FilePackPath.c_str() ); } }
Image load_image(const std::string& filename, Image::Format format) { int32_t width, height; auto data = stbi_load(filename.c_str(), &width, &height, nullptr, format); if (!data) std::cerr << stbi_failure_reason() << std::endl; return Image(width, height, format, data); }
bool Image::LoadFile( uint8_t * data, int32_t size ) { this->bpp = 4; stbi_convert_iphone_png_to_rgb(1); this->pixels = stbi_load_from_memory(data, (int)size, (int*)&this->width, (int*)&this->height, (int*)&this->original_bpp, this->bpp); if ( this->pixels == NULL ) { std::cout << "elix::Image:" << stbi_failure_reason() << std::endl; this->length = 0; return false; } if ( this->bpp != 4 ) { std::cout << "this->bpp:" << this->bpp << "-" << stbi_failure_reason() << std::endl; } this->length = this->width * this->height * this->bpp; return true; }
Texture2D loadTexture(u8 *image_data, u64 image_size, RenderContext *render_context, RenderContext::Format format, int force_components) { int tex_w, tex_h, tex_comps; stbi_set_flip_vertically_on_load(true); u8 *tex_data = (u8 *)stbi_loadf_from_memory((stbi_uc *)image_data, image_size, &tex_w, &tex_h, &tex_comps, force_components); const char *fail_reason = stbi_failure_reason(); Texture2D result = render_context->createTexture2D(tex_data, (u32)tex_w, (u32)tex_h, format); stbi_image_free(tex_data); return result; }
std::string StbContext::GetError() { // gr: as this is not thread safe, it could come out mangled :( maybe add a lock around error popping before read (maybe have to lock around the whole thing) auto* StbError = stbi_failure_reason(); if ( !StbError ) return "Unknown error"; return StbError; }
Bitmap Bitmap::bitmapFromFile(std::string filePath) { int width, height, channels; unsigned char* pixels = stbi_load(filePath.c_str(), &width, &height, &channels, 0); if(!pixels) throw std::runtime_error(stbi_failure_reason()); Bitmap bmp(width, height, (Format)channels, pixels); stbi_image_free(pixels); return bmp; }
//--------------------------------------------------------------------- Codec::DecodeResult STBIImageCodec::decode(DataStreamPtr& input) const { // Buffer stream into memory (TODO: override IO functions instead?) MemoryDataStream memStream(input, true); int width, height, components; stbi_uc* pixelData = stbi_load_from_memory(memStream.getPtr(), static_cast<int>(memStream.size()), &width, &height, &components, 0); if (!pixelData) { OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR, "Error decoding image: " + String(stbi_failure_reason()), "STBIImageCodec::decode"); } SharedPtr<ImageData> imgData(OGRE_NEW ImageData()); MemoryDataStreamPtr output; imgData->depth = 1; // only 2D formats handled by this codec imgData->width = width; imgData->height = height; imgData->num_mipmaps = 0; // no mipmaps in non-DDS imgData->flags = 0; switch( components ) { case 1: imgData->format = PF_BYTE_L; break; case 2: imgData->format = PF_BYTE_LA; break; case 3: imgData->format = PF_BYTE_RGB; break; case 4: imgData->format = PF_BYTE_RGBA; break; default: stbi_image_free(pixelData); OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND, "Unknown or unsupported image format", "STBIImageCodec::decode"); break; } size_t dstPitch = imgData->width * PixelUtil::getNumElemBytes(imgData->format); imgData->size = dstPitch * imgData->height; output.bind(OGRE_NEW MemoryDataStream(pixelData, imgData->size, true)); DecodeResult ret; ret.first = output; ret.second = imgData; return ret; }
TextureResourceData* TextureLoader_stbi(ResourceDataSource &dataSource, Allocator &alloc, bool forceRGBA) { stbi_io_callbacks callbacks; callbacks.read = read; callbacks.skip = skip; callbacks.eof = eof; int x, y, bpp; auto pixels = stbi_load_from_callbacks(&callbacks, &dataSource, &x, &y, &bpp, forceRGBA ? 4 : 0); if (!pixels) { #ifdef STB_DO_ERROR_PRINT DFLOG_WARN(stbi_failure_reason()); #endif return nullptr; } auto fmt = PixelFormat::INVALID; if (bpp == STBI_rgb) { fmt = PixelFormat::RGB; } else if (bpp == STBI_rgb_alpha) { fmt = PixelFormat::RGBA; } else { DFLOG_WARN("Parsed image with an invalid bpp"); stbi_image_free(pixels); return nullptr; } if (forceRGBA) fmt = PixelFormat::RGBA; auto resource = MAKE_NEW(alloc, TextureResourceData); resource->format = fmt; resource->mipLevels.resize(1); resource->mipLevels[0].width = x; resource->mipLevels[0].height = y; int compCount = forceRGBA ? 4 : bpp; resource->mipLevels[0].pixels.resize(compCount * x * y); memcpy(resource->mipLevels[0].pixels.data(), pixels, compCount * x * y); stbi_image_free(pixels); return resource; }
bool Texture::load(std::string filename) { int width, height, channels; unsigned char* ptr = stbi_load(filename.c_str(), &width, &height, &channels, STBI_rgb_alpha); bool result = false; if (ptr && width && height) result = load(ptr, width, height); else printLog("Failed to load image \"%s\". Reason : %s\n", filename.c_str(), stbi_failure_reason()); stbi_image_free(ptr); return result; }
cImage::cImage( std::string Path, const eeUint& forceChannels ) : mPixels(NULL), mWidth(0), mHeight(0), mChannels(forceChannels), mSize(0), mAvoidFree(false), mLoadedFromStbi(false) { int w, h, c; cPack * tPack = NULL; Uint8 * data = stbi_load( Path.c_str(), &w, &h, &c, mChannels ); if ( NULL == data ) { data = stbi_load( ( Sys::GetProcessPath() + Path ).c_str(), &w, &h, &c, mChannels ); } if ( NULL != data ) { mPixels = data; mWidth = (eeUint)w; mHeight = (eeUint)h; if ( STBI_default == mChannels ) mChannels = (eeUint)c; mSize = mWidth * mHeight * mChannels; mLoadedFromStbi = true; } else if ( cPackManager::instance()->FallbackToPacks() && NULL != ( tPack = cPackManager::instance()->Exists( Path ) ) ) { LoadFromPack( tPack, Path ); } else { std::string reason = "."; if ( NULL != stbi_failure_reason() ) { reason = ", reason: " + std::string( stbi_failure_reason() ); } eePRINTL( "Failed to load image %s. Reason: %s", Path.c_str(), reason.c_str() ); } }
bool STB_Image_Loader::decode(ResourceLoadOptions& options) { std::vector<uint8> data; StreamRead(options.stream, data); if( data.empty() ) return false; int width, height, comp; byte* pixelData = stbi_load_from_memory( &data[0], data.size(), &width, &height, &comp, 0 /* 0=auto-detect, 3=RGB, 4=RGBA */ ); if( !pixelData ) { const char* error = stbi_failure_reason(); LogError("STB image error: %s", error); return false; } // Build our image with the pixel data returned by stb_image. PixelFormat pf = PixelFormat::Unknown; switch( comp ) { case 0: case 1: break; case 3: pf = PixelFormat::R8G8B8; break; case 4: pf = PixelFormat::R8G8B8A8; break; } if( pf == PixelFormat::Unknown ) { LogError( "Implement support for more pixel formats" ); return false; } std::vector<byte> buffer; uint32 size = width*height*comp; buffer.resize(size); memcpy(&buffer[0], pixelData, size); free(pixelData); Image* image = static_cast<Image*>( options.resource ); image->setWidth( width ); image->setHeight( height ); image->setPixelFormat( pf ); image->setBuffer( buffer ); return true; }
bool Texture::load(const void* memFile, std::size_t size) { int width, height, channels; const unsigned char* buffer = static_cast<const unsigned char*>(memFile); unsigned char* ptr = stbi_load_from_memory(buffer, static_cast<int>(size), &width, &height, &channels, STBI_rgb_alpha); bool result = false; if (ptr && width && height) result = load(ptr, width, height); else printLog("Failed to load image from memory. Reason : %s\n", stbi_failure_reason()); stbi_image_free(ptr); return result; }
static unsigned char *read_zip_file(FILE *file, int offset, int *sizep) { int sig, method, csize, usize; int namelength, extralength; char *cdata, *udata; fseek(file, offset, 0); sig = getlong(file); if (sig != ZIP_LOCAL_FILE_SIG) { warn("zip: wrong signature for local file"); return NULL; } (void) getshort(file); /* version */ (void) getshort(file); /* general */ method = getshort(file); (void) getshort(file); /* file time */ (void) getshort(file); /* file date */ (void) getlong(file); /* crc-32 */ csize = getlong(file); /* csize */ usize = getlong(file); /* usize */ namelength = getshort(file); extralength = getshort(file); fseek(file, namelength + extralength, 1); if (method == 0 && csize == usize) { cdata = malloc(csize); fread(cdata, 1, csize, file); *sizep = csize; return (unsigned char*) cdata; } if (method == 8) { cdata = malloc(csize); fread(cdata, 1, csize, file); udata = malloc(usize); usize = stbi_zlib_decode_noheader_buffer(udata, usize, cdata, csize); free(cdata); if (usize < 0) { warn("zip: %s", stbi_failure_reason()); return NULL; } *sizep = usize; return (unsigned char*) udata; } warn("zip: unknown compression method"); return NULL; }
bool Texture::load(const std::string &path, GLenum min_filter, GLenum mag_filter, GLenum wrap_s, GLenum wrap_t) { int width, height, perPixComp; const char *image_path = path.c_str(); unsigned char* image = stbi_load(image_path, &width, &height, &perPixComp, 3); if (!image) { std::cout << "ERROR::TEXTURE::LOADING_FAILED\n" << stbi_failure_reason() << std::endl; return false; } else { load(image, width, height, min_filter, mag_filter, wrap_s, wrap_t); stbi_image_free(image); } return true; }
int Render::loadTexture(string texture_file, string texture_name) { //std::string path(GLSL_SOURCE_DIR); //path += "../../../sprites/boid.png"; //path += "../../../sprites/enjalot.jpg"; printf("LOAD TEXTURE!!!!!!!!!!!!!!\n"); //printf("path: %s\n", path.c_str()); //Load an image with stb_image int w,h,channels; int force_channels = 0; unsigned char *im = stbi_load( texture_file.c_str(), &w, &h, &channels, force_channels ); printf("after load w: %d h: %d channels: %d\n", w, h, channels); printf("im looking for the image at %s\n", texture_file.c_str()); if (im == NULL) { printf("fail!: %s\n", stbi_failure_reason()); printf("WTF\n"); } //load as gl texture glGenTextures(1, &gl_textures[texture_name]); glBindTexture(GL_TEXTURE_2D, gl_textures[texture_name]); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //better way to do this? if(channels == 3) { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, &im[0]); } else if (channels == 4) { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, &im[0]); } glBindTexture(GL_TEXTURE_2D,0); free(im); return 0; //success }
Image load_image( const char* filename ) { i32 width; i32 height; i32 format; // Using NULL because stbi_load takes an integer and nullptr // has type nullptr_t. u8* pixels = stbi_load( filename, &width, &height, &format, 0 ); defer( stbi_image_free( pixels ) ); if ( !pixels ) throw std::runtime_error( stbi_failure_reason() ); return load_image( width, height, (ImageFormat)format, pixels ); }
TexturePtr Texture::load(const char* path) { int width; int height; int comp; stbi_set_flip_vertically_on_load(1); // we need to flip the y axis due to OpenGL unsigned char* data = stbi_load(path, &width, &height, &comp, STBI_rgb_alpha); if (data == nullptr) { std::cout << "Texture::load error: there was a problem loading the texture " << path << std::endl; std::cout << "STBI: " << stbi_failure_reason() << std::endl; return nullptr; } return TexturePtr(new Texture(data, width, height, comp, path)); }
kit::Texture::Ptr kit::Texture::create2DFromFile(std::string filename, kit::Texture::InternalFormat format, kit::Texture::EdgeSamplingMode edgemode, kit::Texture::FilteringMode minfilter, kit::Texture::FilteringMode magfilter) { std::cout << "Loading texture from file " << filename.c_str() << std::endl; kit::Texture::Ptr returner = std::make_shared<kit::Texture>(Texture2D); returner->m_internalFormat = format; // Try to load data from file unsigned char* bufferdata; int x, y, n; stbi_set_flip_vertically_on_load(1); bufferdata = stbi_load(filename.c_str(), &x, &y, &n, 4); if (bufferdata == nullptr) { KIT_ERR(stbi_failure_reason()); x = 1; y = 1; n = 4; bufferdata = new unsigned char[4]; bufferdata[0] = 255; bufferdata[1] = 0; bufferdata[2] = 0; bufferdata[3] = 255; } // Set resolution returner->m_resolution = glm::uvec3(x, y, 0); // Specify storage and upload data to GPU KIT_GL(glTextureStorage2D(returner->m_glHandle, returner->calculateMipLevels(), returner->m_internalFormat, returner->m_resolution.x, returner->m_resolution.y)); KIT_GL(glTextureSubImage2D(returner->m_glHandle, 0, 0, 0, x, y, GL_RGBA, GL_UNSIGNED_BYTE, bufferdata)); // Free loaded data stbi_image_free(bufferdata); // Set parameters returner->setEdgeSamplingMode(edgemode); returner->setMinFilteringMode(minfilter); returner->setMagFilteringMode(magfilter); returner->setAnisotropicLevel(8.0f); // Generate mipmap returner->generateMipmap(); return returner; }
bool ImageLoader::loadImageFromMemory(const void* data, std::size_t dataSize, std::vector<Uint8>& pixels, Vector2u& size) { // Check input parameters if (data && dataSize) { // Clear the array (just in case) pixels.clear(); // Load the image and get a pointer to the pixels in memory int width = 0; int height = 0; int channels = 0; const unsigned char* buffer = static_cast<const unsigned char*>(data); unsigned char* ptr = stbi_load_from_memory(buffer, static_cast<int>(dataSize), &width, &height, &channels, STBI_rgb_alpha); if (ptr) { // Assign the image properties size.x = width; size.y = height; if (width && height) { // Copy the loaded pixels to the pixel buffer pixels.resize(width * height * 4); memcpy(&pixels[0], ptr, pixels.size()); } // Free the loaded pixels (they are now in our own pixel buffer) stbi_image_free(ptr); return true; } else { // Error, failed to load the image err() << "Failed to load image from memory. Reason: " << stbi_failure_reason() << std::endl; return false; } } else { err() << "Failed to load image from memory, no data provided" << std::endl; return false; } }