//----------------------------------------------------------------------------------------------- Texture::Texture( const std::string& imageFilePath ) : m_openglTextureID( 0 ) , m_size( 0, 0 ) { int numComponents = 0; // Filled in for us to indicate how many color/alpha components the image had (e.g. 3=RGB, 4=RGBA) int numComponentsRequested = 0; // don't care; we support 3 (RGB) or 4 (RGBA) unsigned char* imageData = stbi_load( imageFilePath.c_str(), &m_size.x, &m_size.y, &numComponents, numComponentsRequested ); if( imageData == nullptr ) return; // Enable texturing OpenGLRenderer::EnableTexture2D(); // Tell OpenGL that our pixel data is single-byte aligned OpenGLRenderer::PixelStore(); // Ask OpenGL for an unused texName (ID number) to use for this texture OpenGLRenderer::GenerateTextures( &m_openglTextureID ); // Tell OpenGL to bind (set) this as the currently active texture OpenGLRenderer::BindTexture2D( m_openglTextureID ); // Set texture clamp vs. wrap (repeat) OpenGLRenderer::SetTexture2DWrapS( REPEAT ); // GL_CLAMP or GL_REPEAT OpenGLRenderer::SetTexture2DWrapT( REPEAT ); // GL_CLAMP or GL_REPEAT // Set magnification (texel > pixel) and minification (texel < pixel) filters OpenGLRenderer::SetTexture2DMagnificationFilter( LINEAR ); // one of: GL_NEAREST, GL_LINEAR, GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR_MIPMAP_LINEAR OpenGLRenderer::SetTexture2DMinificationFilter( LINEAR_MIPMAP_LINEAR ); // one of: GL_NEAREST, GL_LINEAR, GL_NEAREST_MIPMAP_NEAREST, GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_NEAREST, GL_LINEAR_MIPMAP_LINEAR //OpenGLRenderer::SetTexture2DMaxLevel( 5 ); pixelDataFormat bufferFormat = RGBA; // the format our source pixel data is currently in; any of: GL_RGB, GL_RGBA, GL_LUMINANCE, GL_LUMINANCE_ALPHA, ... if( numComponents == 1 ) bufferFormat = LUMINANCE; else if( numComponents == 3 ) bufferFormat = RGB; pixelDataFormat internalFormat = bufferFormat; // the format we want the texture to me on the card; allows us to translate into a different texture format as we upload to OpenGL OpenGLRenderer::SetTexture2DImage( // Upload this pixel data to our new OpenGL texture 0, // Which mipmap level to use as the "root" (0 = the highest-quality, full-res image), if mipmaps are enabled internalFormat, // Type of texel format we want OpenGL to use for this texture internally on the video card m_size.x, // Texel-width of image; for maximum compatibility, use 2^N + 2^B, where N is some integer in the range [3,10], and B is the border thickness [0,1] m_size.y, // Texel-height of image; for maximum compatibility, use 2^M + 2^B, where M is some integer in the range [3,10], and B is the border thickness [0,1] 0, // Border size, in texels (must be 0 or 1) bufferFormat, // Pixel format describing the composition of the pixel data in buffer imageData ); // Location of the actual pixel data bytes/buffer OpenGLRenderer::GenerateMipmapHint(); OpenGLRenderer::GenerateMipmapTexture2D(); stbi_image_free( imageData ); s_textureRegistry[ imageFilePath ] = this; }
Texture Texture::loadFile(const std::string& fileName, const unsigned int& channelCount) { Texture t; HGLT_ASSERT( channelCount > 0 && channelCount <= 4, "Invalid Image Channel Count." ); if( channelCount <= 0 || channelCount > 4 ) return t; glGenTextures(1, &t.m_nGLId); HGLT_ASSERT( t.m_nGLId != hglt::invalidGLID, "Cannot create an OpenGL Texture ID." ); int x; int y; int comp; unsigned char * data = stbi_load(fileName.c_str(), &x, &y, &comp, channelCount); HGLT_ASSERT(data != nullptr, std::string("Unable to load:" + fileName).c_str()); if( data == nullptr ) { glDeleteTextures(1, &t.m_nGLId); t.m_nGLId = hglt::invalidGLID; return t; } GLint format; switch(channelCount) { case 1: format = GL_RED; break; case 2: format = GL_RG; break; case 3: format = GL_RGB; break; case 4: format = GL_RGBA; break; } t.bind(); glTexImage2D(GL_TEXTURE_2D, 0, format, x, y, 0, format, GL_UNSIGNED_BYTE, data); 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_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); t.unbind(); stbi_image_free(data); t.m_filename = fileName; return t; }
Image::Image(const String &filename, uint16 hframes, uint16 vframes) { this->filename = filename; this->hframes = hframes; this->vframes = vframes; width = 0; height = 0; handlex = 0; handley = 0; gltex = 0; lastU = 1.0; lastV = 1.0; // TAREA: Cargar el buffer de la imagen int bWidth=0; int bHeight=0; unsigned char* buffer = stbi_load(filename.ToCString(),&bWidth,&bHeight,NULL,4); unsigned char* buffer1 = NULL; width=(uint16)bWidth; height=(uint16)bHeight; int width1=(int)pow(2,ceil(Log2(width))); int height1=(int)pow(2,ceil(Log2(height))); if ((width1!=width)||(height1!=height)) { bWidth = width1; bHeight = height1; buffer1=new unsigned char[bWidth*bHeight*4]; for (int i=0; i<(height);i++) { for (int j=0; j<(width); j++) { for (int k=0; k<4;k++) { buffer1[(j*width1+i)*4 + k]=buffer[(j*width+i)*4 + k]; } } } lastU=(double)width/width1; lastV=(double)height/height1; } // Generamos la textura if ( buffer ) { // TAREA: Generar la textura de OpenGL glGenTextures(1, &gltex); Bind(); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_NEAREST); if (buffer1) glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,bWidth,bHeight,0,GL_RGBA,GL_UNSIGNED_BYTE,buffer1); else glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,bWidth,bHeight,0,GL_RGBA,GL_UNSIGNED_BYTE,buffer); stbi_image_free(buffer); delete[] buffer1; } }
Font::Font(const String& filename) : Image(filename, 16, 16) { int widthImage = 0; int heightImage = 0; int nComponents = 4; unsigned int nFrames = 16 * 16; uint16 widthFrame = GetWidth(); uint16 heightFrame = GetHeight(); uint16 nLine = -1; unsigned char* pixels = stbi_load( filename.ToCString(), &widthImage, &heightImage, &nComponents, nComponents); for(unsigned int n = 0; n < (unsigned int)nFrames; n++) { Glyph glyph(0, 0, widthFrame, heightFrame); uint16 row = n / 16; uint16 column = n % 16; for(unsigned int posY = (unsigned int) (row * heightFrame); posY < (unsigned int) ((row + 1) * heightFrame) ; posY++ ) { for(unsigned int posX = (unsigned int)( column * widthFrame ); posX < (unsigned int)( (column + 1) * widthFrame ); posX++) { unsigned char pixelR = pixels[(posY * widthImage + posX) * 4 + 0]; unsigned char pixelG = pixels[(posY * widthImage + posX) * 4 + 1]; unsigned char pixelB = pixels[(posY * widthImage + posX) * 4 + 2]; unsigned char* pixelA = &pixels[(posY * widthImage + posX) * 4 + 3]; if( Glyph::IsYellow( pixelR, pixelG, pixelB)) { glyph.SetInitialCoordinates(posX, posY); *pixelA = 0; } else if( Glyph::IsRed( pixelR, pixelG, pixelB)) { glyph.SetFinalCoordinates(posX, posY); *pixelA = 0; } else if( Glyph::IsBlack( pixelR, pixelG, pixelB)) { *pixelA = 0; } } } glyphs.Add(glyph); } glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, widthImage, heightImage, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); stbi_image_free(pixels); }
void createBackground(HWND hwnd){ int w,h, comp; U8* data = stbi_load("images\\drawing.jpg",&w,&h,&comp,4); U8* bmdata; // fprintf(stderr,"loaded %p (%d,%d) %d components\n",data,w,h,comp); //Since we have DI data, use CreateDIBSection BITMAPINFOHEADER bi; bi.biSize = sizeof(BITMAPINFOHEADER); bi.biWidth=640; bi.biHeight = -480; bi.biPlanes = 1; bi.biBitCount=32; bi.biCompression = BI_RGB; bi.biSizeImage = 0; bi.biXPelsPerMeter = 0; bi.biYPelsPerMeter = 0; bi.biClrUsed = 0; bi.biClrImportant = 0; HDC screenDC = GetDC(0); HDC bufferDC = CreateCompatibleDC(screenDC); bmBk = CreateDIBSection( bufferDC, (BITMAPINFO*)&bi, DIB_RGB_COLORS, (void**)&bmdata, NULL, 0); union sPixel { U32 value; struct sRGB { U8 r; U8 g; U8 b; U8 a; }rgb; }; DeleteObject(bufferDC); ReleaseDC(hwnd,screenDC); // We need to reverse r and b positions... // memcpy(bmdata,data,640*480*4); sPixel* src=(sPixel*)data; sPixel* dest=(sPixel*)bmdata; int i; for(i=0;i<640*480;i++){ sPixel pixel = *src++; U8 t = pixel.rgb.r; pixel.rgb.r = pixel.rgb.b; pixel.rgb.b = t; *dest++ = pixel; } stbi_image_free(data); // HBITMAP mybm = CreateBitmap(w,h,1,24,data); // fprintf(stderr,"created bitmap %p \n",bmBk); }
Texture *TextureDeserializer::deserializeHdr(stream in){ unsigned char *buf = new unsigned char[in.length]; in.read(buf, in.length); int x, y, components; float *target = (float *)stbi_hdr_load_from_memory(buf, in.length, &x, &y, &components, 4); Texture *t = new Texture(x, y, GL_RGBA16F); t->SetContent(target); stbi_image_free(target); return t; }
Texture *TextureDeserializer::deserializeBmp(stream in) { unsigned char *buf = new unsigned char[in.length]; in.read(buf, in.length); int x, y, components; unsigned char *target = stbi_bmp_load_from_memory(buf, in.length, &x, &y, &components, 4); Texture *t = new Texture(x, y); t->SetContent(target); stbi_image_free(target); return t; }
image( const std::string &filepath ) { unsigned char *stbi_data = stbi_load( filepath.c_str(), &w, &h, &n, 0 ); if ( !stbi_data ) { throw std::runtime_error( "Failed load image from " + filepath ); } data.assign( stbi_data, stbi_data + (w * h * n) ); stbi_image_free( stbi_data ); }
JNIEXPORT void JNICALL Java_com_original_Convert_createBMP(JNIEnv * myEnv, jobject obj, jstring jpeg_PathJ, jstring bmp_PathJ) { jpeg_Path = myEnv->GetStringUTFChars(jpeg_PathJ, NULL); bmp_Path = myEnv->GetStringUTFChars(bmp_PathJ, NULL); LOGI("Destination = %s",bmp_Path); init(); stbi_write_bmp(bmp_Path, imageWidth, imageHeight, comp, image); stbi_image_free(image); LOGI("Looking Good"); }
unsigned char* loadPic(char* path){ // load an image unsigned char* image; unsigned char* image0 = stbi_load(path, &width, &height, &comp, 0); //flip image vertically image = (unsigned char*)malloc(sizeof(unsigned char)*width*height*comp); for (int y = 0, stride = width*comp; y < height; y++) memcpy(image + (height - 1 - y)*stride, image0 + y*stride, stride); // vertical flip stbi_image_free(image0); // release the original image return image; }
Resource* ImageLoader::loadInternal(State* state, string dir) { Image* ret; int width, height, comp; unsigned char* data = stbi_load(dir.c_str(),&width,&height,&comp,0); ret = new Image(dir, width, height, comp, data); stbi_image_free(data); return ret; }
static void test_correct(char const *filename) { int x0, y0, n0; int x1, y1, n1; unsigned char *data0 = stbi_load(filename, &x0, &y0, &n0, 0); unsigned char *data1 = stbi_orig_load(filename, &x1, &y1, &n1, 0); printf("%dx%d n=%d\n", x0, y0, n0); if (x0 != x1 || y0 != y1 || n0 != n1) panic("image dimension mismatch!\n"); if (memcmp(data0, data1, x0*y0*n0) != 0) panic("image data mismatch!\n"); stbi_image_free(data0); stbi_image_free(data1); printf("%s decodes correctly.\n", filename); }
void Texture::LoadTexture(std::string fileName){ int numComponents; m_data = stbi_load((fileName).c_str(), &m_width, &m_height, &numComponents, 4); if (m_data == NULL){ LOG(FATAL) << "Error: Unable to load texture: " << fileName << std::endl; stbi_image_free(m_data); return; } }
Waifu2x::eWaifu2xError Waifu2x::LoadMatBySTBI(cv::Mat &float_image, const std::vector<char> &img_data) { int x, y, comp; stbi_uc *data = stbi_load_from_memory((const stbi_uc *)img_data.data(), img_data.size(), &x, &y, &comp, 4); if (!data) return eWaifu2xError_FailedOpenInputFile; int type = 0; switch (comp) { case 1: case 3: case 4: type = CV_MAKETYPE(CV_8U, comp); break; default: return eWaifu2xError_FailedOpenInputFile; } float_image = cv::Mat(cv::Size(x, y), type); const auto LinePixel = float_image.step1() / float_image.channels(); const auto Channel = float_image.channels(); const auto Width = float_image.size().width; const auto Height = float_image.size().height; assert(x == Width); assert(y == Height); assert(Channel == comp); auto ptr = float_image.data; for (int i = 0; i < y; i++) { for (int j = 0; j < x; j++) { for (int ch = 0; ch < Channel; ch++) ptr[(i * LinePixel + j) * comp + ch] = data[(i * x + j) * comp + ch]; } } stbi_image_free(data); if (comp >= 3) { // RGBだからBGRに変換 for (int i = 0; i < y; i++) { for (int j = 0; j < x; j++) std::swap(ptr[(i * LinePixel + j) * comp + 0], ptr[(i * LinePixel + j) * comp + 2]); } } return eWaifu2xError_OK; }
void IblSpecularPass::init() { std::cout << "generating ibl prefilter map\n"; glGenFramebuffers(1, &captureFBO); glGenRenderbuffers(1, &captureRBO); glBindFramebuffer(GL_FRAMEBUFFER, captureFBO); glBindRenderbuffer(GL_RENDERBUFFER, captureRBO); glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, 512, 512); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, captureRBO); // pbr: load the HDR environment map // --------------------------------- if (data) { glGenTextures(1, &hdrTexture); glBindTexture(GL_TEXTURE_2D, hdrTexture); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB16F, width, height, 0, GL_RGB, GL_FLOAT, data); // note how we specify the texture's data value to be float glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); stbi_image_free(data); } else { std::cout << "Failed to load HDR image." << std::endl; } // pbr: setup cubemap to render to and attach to framebuffer // --------------------------------------------------------- //unsigned int prefilterMap; glGenTextures(1, &prefilterMap); glBindTexture(GL_TEXTURE_CUBE_MAP, prefilterMap); for (unsigned int i = 0; i < 6; ++i) { glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB16F, 128, 128, 0, GL_RGB, GL_FLOAT, nullptr); } glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); // be sure to set minifcation filter to mip_linear glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // generate mipmaps for the cubemap so OpenGL automatically allocates the required memory. glGenerateMipmap(GL_TEXTURE_CUBE_MAP); }
void Texture::LoadFromFile() { FILE* file = fopen(this->filename, "rb"); imgData = stbi_load_from_file(file, &width, &height, &components, 4); fclose(file); glGenTextures(1, &glID); glBindTexture(GL_TEXTURE_2D, glID); gluBuild2DMipmaps(GL_TEXTURE_2D, 4, width, height, GL_RGBA, GL_UNSIGNED_BYTE, imgData); stbi_image_free(imgData); glBindTexture(GL_TEXTURE_2D, 0); }
Bitmap Bitmap::bitmapFromFile(std::string path) { int width, height, channels; unsigned char *pixels = stbi_load(path.c_str(), &width, &height, &channels, 0); if (!pixels) throw std::runtime_error(std::string("Error loading bitmap at path ") + path + ":\n" + std::string(stbi_failure_reason())); Bitmap bmp(width, height, (Format) channels, pixels); stbi_image_free(pixels); return bmp; }
void DBTexture::writeCompressedToRaw() { int w = m_width, h = m_height, n = m_numComp; byte* data = stbi_load_from_memory(m_compressedData.data(), int(m_compressedData.size_bytes()), &w, &h, &n, m_numComp); assert(w == m_width && h == m_height && n == m_numComp); uint dataSize = m_width * m_height * m_numComp * m_pixColSize; m_rawData.resize(dataSize); memcpy(m_rawData.data(), data, dataSize); stbi_image_free(data); }
bool Texture::load(char* fileName) { glActiveTexture(GL_TEXTURE1); glGenTextures(1, &m_textureID); glBindTexture(GL_TEXTURE_2D, m_textureID); int bytesPerPixel = 0; unsigned char* data = stbi_load(fileName, &m_width, &m_heigth, &bytesPerPixel, 0); //flip image vertically unsigned char* s = data; for (int y = 0; y<m_heigth / 2; y++) { unsigned char* e = data + (m_heigth - y - 1)*m_width*bytesPerPixel; for (int x = 0; x<m_width*bytesPerPixel; x++) { unsigned char temp = *s; *s = *e; *e = temp; s++; e++; } } //send image data to the new texture if (bytesPerPixel < 3) { printf("ERROR: Unable to load texture image %s\n", fileName); return false; } else if (bytesPerPixel == 3) { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_width, m_heigth, 0, GL_RGB, GL_UNSIGNED_BYTE, data); } else if (bytesPerPixel == 4) { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_width, m_heigth, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); } else { printf("RESOLVED: Unknown format for bytes per pixel in texture image %s, changed to 4\n", fileName); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_width, m_heigth, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); } stbi_image_free(data); glGenerateMipmap(GL_TEXTURE_2D); printf("SUCCESS: Texture image %s loaded\n", filepath); unUse(); return true; }
void Texture::initFail(GLenum type, std::string fname, std::vector<std::array<GLenum, 2> > params) { std::cout << "InitFail " << fname; try { FILE *file = fopen(fname.c_str(), "rb"); if (!file) return ; int width, height, comp; unsigned char* imgdata = stbi_load_from_file(file, &width, &height, &comp, 0); fclose(file); //png::image<png::rgba_pixel> image (fname); //GLubyte* imgdata = extractData(image, false, false); glGenTextures(1, &textureID); glBindTexture(GL_TEXTURE_2D, textureID); glTexImage2D(GL_TEXTURE_2D, 0, type, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, imgdata); std::cout << comp << " " << textureID << " " << GL_RGB << " " << type << " " << width << " " << height << std::endl; glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); for(int i=0; i<params.size(); i++) { glTexParameteri(GL_TEXTURE_2D, params[i][0], params[i][1]); } glBindTexture(GL_TEXTURE_2D, 0); stbi_image_free(imgdata); inited = true; std::cout << " - success" << std::endl; } catch(png::std_error& e) { std::cout << " - fail" << std::endl; } }
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; }
//bpp == the resulting bits per pixel //bpp == the source image bits per pixel //req_bpp == use this instead of the source bool info_from_bytes( QuickVec<unsigned char> &out_buffer, const unsigned char* bytes, int byteOffset, int byteLength, const char* _id, int *w, int *h, int* bpp, int* bpp_source, int req_bpp = 4 ) { //get a io file pointer to the image snow::io::iosrc* src = snow::io::iosrc_from_mem( (void*)(bytes + byteOffset), byteLength ); if(!src) { snow::log(1, "/ snow / cannot open bytes from %s", _id); return false; } //always use callbacks because we use snow abstracted IO stbi_io_callbacks stbi_snow_callbacks = { snow_stbi_read, snow_stbi_skip, snow_stbi_eof }; unsigned char *data = stbi_load_from_callbacks(&stbi_snow_callbacks, src, w, h, bpp_source, req_bpp); //we are done with the src snow::io::close(src); snow::log(2, "/ snow / image / w:%d h:%d source bpp:%d bpp:%d\n", *w, *h, *bpp_source, req_bpp); if(data != NULL) { int _w = *w; int _h = *h; int _bpp = *bpp_source; //if a requested bpp was given, override it if(req_bpp != 0) { _bpp = req_bpp; } //actual used bpp *bpp = _bpp; //work out the total length of the output buffer unsigned int length = _w * _h * _bpp; //store it out_buffer.Set(data, length); //clean up used memory stbi_image_free(data); } //data != NULL return true; } //info_from_bytes
void Bitmap::destroy() { if(data && loaded) { stbi_image_free(data); } else if(data && !loaded) { delete [] data; } reset(); }
Texture::Texture(const Adapter &adapter, const std::string &name) { GLsizei width, height, comp; GLvoid *data = stbi_load(adapter.filename<Texture>(name).c_str(), &width, &height, &comp, 0); glGenTextures(1, &_id); glBindTexture(GL_TEXTURE_2D, _id); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); stbi_image_free(data); }
// TODO(jose): I don´t like this helper API void LoadTexture(ShadertoyState *state, char *filename[], ShadertoyPass pass, int channel_id) { int comp = STBI_rgb; Texture tex[6]; for (int i = 0; i < 6; ++i) { tex[i].data = stbi_load(filename[i], &tex[i].width, &tex[i].height, &comp, 0); tex[i].format = comp == STBI_rgb ? SHADERTOY_TEXTURE_FORMAT_RGB : SHADERTOY_TEXTURE_FORMAT_RGBA; } ShadertoyLoadCubemap(state, tex, pass, channel_id); for (int i = 0; i < 6; ++i) { stbi_image_free(tex[i].data); } }
//OpenGL Shutdown Logic void glShutdown() { glDeleteVertexArrays(1, &gMipmapVao); glDeleteBuffers(1, &gMipmapTexCoordBuf); glDeleteBuffers(1, &gMipmapPosBuf); glDeleteTextures(1, &gMipmapTexture); glDeleteShader(gMipmapFS); glDeleteShader(gMipmapVS); glDeleteProgram(gMipmapProg); stbi_image_free(gMipmapTexImage); }
GLuint LoadTextureReleaseData (const std::string& filename, GLenum wrap_mode) { int width, height; U8* pixels = stbi_load(filename.c_str(), &width, &height, nullptr, 4); if (pixels == nullptr) { LOG_ERROR("Could not load image: " << filename); return 0; } auto textureID = CreateRGBATexture(width, height, pixels, wrap_mode); stbi_image_free(pixels); return textureID; }
GLuint CreateTextureFromFile(const std::string& filename) { int width, height; U8* pixels = stbi_load(filename.c_str(), &width, &height, nullptr, 4); if (pixels == nullptr) { LOG_ERROR("Could not load image: " << filename); return 0; } auto textureID = CreateTextureFromPixels(width, height, pixels); stbi_image_free(pixels); return textureID; }
sprite_t *loadImage32(u8 *png, int size) { int x, y, n, fd; u8 *tbuf; u32 *ibuf; sprite_t *sbuf; tbuf = malloc(size); memcpy(tbuf,png,size); ibuf = (u32*)stbi_load_from_memory(tbuf, size, &x, &y, &n, 4); free(tbuf); if (!ibuf) return 0; // couldn't decode image sbuf = (sprite_t*)malloc(sizeof(sprite_t) + x * y * 4); if (!sbuf) { stbi_image_free(ibuf); return 0; // out of memory } sbuf->width = x; sbuf->height = y; sbuf->bitdepth = 4; sbuf->format = 0; sbuf->hslices = x / 32; sbuf->vslices = y / 32; // color_t *src = (color_t*)ibuf; u32 *dst = (u32*)((u32)sbuf + sizeof(sprite_t)); for (int j=0; j<y; j++) for (int i=0; i<x; i++) dst[i + j*x] = ibuf[i + j*x]; /* Invalidate data associated with sprite in cache */ data_cache_hit_writeback_invalidate( sbuf->data, sbuf->width * sbuf->height * sbuf->bitdepth ); stbi_image_free(ibuf); return sbuf; }
// Load any given image GLuint g2LoadImage(const char* ImagePath, int* Width, int* Height, int* Channels, bool Wrap) { // Does the image already exist in the dictionary? __g2ImagesListIt Result = __g2ImageList.find(std::string(ImagePath)); // If nothing is found... if(Result == __g2ImageList.end()) { // Create a new image instance __g2Image Image; strcpy(Image.FileName, ImagePath); // Allocate image; fail out on error unsigned char* DataBuffer = stbi_load(ImagePath, &Image.Width, &Image.Height, &Image.Channels, 4); if(DataBuffer == NULL) return -1; // Allocate an OpenGL texture glGenTextures(1, &Image.GlTextureID); glBindTexture(GL_TEXTURE_2D, Image.GlTextureID); gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, Image.Width, Image.Height, Image.Channels == 3 ? GL_RGB : GL_RGBA, GL_UNSIGNED_BYTE, DataBuffer); // Release internal buffer stbi_image_free(DataBuffer); // Set certain properties of texture glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR); // Wrap texture around glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, Wrap ? GL_REPEAT : GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, Wrap ? GL_REPEAT : GL_CLAMP); // Done setting image parameters glDisable(GL_TEXTURE_2D); // Place in post-back as desired if(Width != NULL) *Width = Image.Width; if(Height != NULL) *Height = Image.Height; if(Channels != NULL) *Channels = Image.Channels; // Save to dictionary __g2ImageList.insert(std::pair<std::string, __g2Image>(std::string(Image.FileName), Image)); return Image.GlTextureID; } // Return what was already known return Result->second.GlTextureID; }