WError WImage::Load(std::string filename, bool bDynamic) { // check if file exists FILE* fp; fopen_s(&fp, filename.c_str(), "r"); if (fp) fclose(fp); else return WError(W_FILENOTFOUND); int n = 4, w, h; unsigned char *data; if (stbi_info(filename.c_str(), &w, &h, &n) == 0) return WError(W_INVALIDFILEFORMAT); data = stbi_load(filename.c_str(), &w, &h, &n, 0); if (!data) return WError(W_INVALIDFILEFORMAT); // convert from 8-bit char components to 32-bit float components float* pixels = new float[w*h*n]; for (int i = 0; i < w*h*n; i++) { float f = (float)data[i] / (float)(unsigned char)(-1); pixels[i] = f; } free(data); WError err = CreateFromPixelsArray(pixels, w, h, bDynamic, n); delete[] pixels; return err; }
ImageInfo getImageDimensions(const std::string & filePath){ std::string path = ofToDataPath(filePath, true); ImageInfo info; int ret = stbi_info(path.c_str(), &info.width, &info.height, &info.nChannels); info.valid = (ret != 0); if(!info.valid){ ofLogError("ofxApp::utils") << "getImageDimensions() failed for image \"" << filePath << "\""; ofPixels pix; bool loadOK = ofLoadImage(pix, filePath); if(loadOK){ info.width = pix.getWidth(); info.height = pix.getHeight(); info.valid = true; info.nChannels = pix.getNumPlanes(); } } return info; }
bool cImage::GetInfo( const std::string& path, int * width, int * height, int * channels ) { bool res = stbi_info( path.c_str(), width, height, channels ) != 0; if ( !res && cPackManager::instance()->FallbackToPacks() ) { std::string npath( path ); cPack * tPack = cPackManager::instance()->Exists( npath ); if ( NULL != tPack ) { SafeDataPointer PData; tPack->ExtractFileToMemory( npath, PData ); res = 0 != stbi_info_from_memory( PData.Data, PData.DataSize, width, height, channels ); } } return res; }
u64 load_pixel_frame(u64 texture, int x, int y, int x2, int y2){ texture_info texinfo; if(!try_get_texture_info(texture, &texinfo)){ char namebuf[100]; get_textures(texture, namebuf, sizeof(namebuf)); stbi_info(namebuf, &texinfo.width, &texinfo.height, &texinfo.comp); set_texture_info(texture, texinfo); } vec2 size = vec2_new(texinfo.width, texinfo.height); texture_section sec = {.uv_offset = vec2_div(vec2_new(x, y), size), .uv_size = vec2_div(vec2_new(x2 - x, y2 - y), size), .render_offset = vec2_new(0,0), .pixel_size = vec2_new(x2 - x, y2 - y)}; //sec.uv_offset.y = 1.0 - sec.uv_offset.y; add_texture_sections(texture, sec); return 0; } u64 load_pixel_frame_center_of_mass(u64 texture, int x, int y, int x2, int y2, int cx, int cy){ texture_info texinfo; if(!try_get_texture_info(texture, &texinfo)){ char namebuf[100]; get_textures(texture, namebuf, sizeof(namebuf)); stbi_info(namebuf, &texinfo.width, &texinfo.height, &texinfo.comp); set_texture_info(texture, texinfo); } vec2 size = vec2_new(texinfo.width, texinfo.height); int center_x = (x2 + x) / 2, center_y = (y2 + y) / 2; texture_section sec = {.uv_offset = vec2_div(vec2_new(x, y), size), .uv_size = vec2_div(vec2_new(x2 - x, y2 - y), size), .render_offset = vec2_new(center_x - cx, center_y - cy), .pixel_size = vec2_new(x2 - x, y2 - y)}; add_texture_sections(texture, sec); return 0; } CREATE_TABLE2(animation, u64, animation_state); struct{ u32 count; i32 * texture; u64 * texid; }loaded_textures; i32 get_animation_gltexture(u64 tex){ u64 * id = memmem(loaded_textures.texid, loaded_textures.count * sizeof(u64), &tex, sizeof(u64)); if(id != NULL){ ASSERT(*id == tex); return loaded_textures.texture[id - loaded_textures.texid]; } char buffer[100]; get_textures(tex, buffer, sizeof(buffer)); i32 newtex = load_gl_texture(buffer); list_push(loaded_textures.texture, loaded_textures.count, newtex); list_push2(loaded_textures.texid, loaded_textures.count, tex); return newtex; }
int main(int argc, char **argv) { int w,h; //test_ycbcr(); #if 0 // test hdr asserts for (h=0; h < 100; h += 2) for (w=0; w < 200; ++w) hdr_data[h][w][0] = (float) rand(), hdr_data[h][w][1] = (float) rand(), hdr_data[h][w][2] = (float) rand(); stbi_write_hdr("output/test.hdr", 200,200,3,hdr_data[0][0]); #endif if (argc > 1) { int i, n; for (i=1; i < argc; ++i) { int res; int w2,h2,n2; unsigned char *data; printf("%s\n", argv[i]); res = stbi_info(argv[1], &w2, &h2, &n2); data = stbi_load(argv[i], &w, &h, &n, 4); if (data) free(data); else printf("Failed &n\n"); data = stbi_load(argv[i], &w, &h, 0, 1); if (data) free(data); else printf("Failed 1\n"); data = stbi_load(argv[i], &w, &h, 0, 2); if (data) free(data); else printf("Failed 2\n"); data = stbi_load(argv[i], &w, &h, 0, 3); if (data) free(data); else printf("Failed 3\n"); data = stbi_load(argv[i], &w, &h, &n, 4); assert(data); assert(w == w2 && h == h2 && n == n2); assert(res); if (data) { char fname[512]; stb_splitpath(fname, argv[i], STB_FILE); stbi_write_png(stb_sprintf("output/%s.png", fname), w, h, 4, data, w*4); stbi_write_bmp(stb_sprintf("output/%s.bmp", fname), w, h, 4, data); stbi_write_tga(stb_sprintf("output/%s.tga", fname), w, h, 4, data); stbi_write_png_to_func(dummy_write,0, w, h, 4, data, w*4); stbi_write_bmp_to_func(dummy_write,0, w, h, 4, data); stbi_write_tga_to_func(dummy_write,0, w, h, 4, data); free(data); } else printf("FAILED 4\n"); } } else { int i, nope=0; #ifdef PNGSUITE_PRIMARY char **files = stb_readdir_files("pngsuite/primary"); #else char **files = stb_readdir_files("images"); #endif for (i=0; i < stb_arr_len(files); ++i) { int n; char **failed = NULL; unsigned char *data; printf("."); //printf("%s\n", files[i]); data = stbi_load(files[i], &w, &h, &n, 0); if (data) free(data); else stb_arr_push(failed, "&n"); data = stbi_load(files[i], &w, &h, 0, 1); if (data) free(data); else stb_arr_push(failed, "1"); data = stbi_load(files[i], &w, &h, 0, 2); if (data) free(data); else stb_arr_push(failed, "2"); data = stbi_load(files[i], &w, &h, 0, 3); if (data) free(data); else stb_arr_push(failed, "3"); data = stbi_load(files[i], &w, &h, 0, 4); if (data) ; else stb_arr_push(failed, "4"); if (data) { char fname[512]; #ifdef PNGSUITE_PRIMARY int w2,h2; unsigned char *data2; stb_splitpath(fname, files[i], STB_FILE_EXT); data2 = stbi_load(stb_sprintf("pngsuite/primary_check/%s", fname), &w2, &h2, 0, 4); if (!data2) printf("FAILED: couldn't load 'pngsuite/primary_check/%s\n", fname); else { if (w != w2 || h != w2 || 0 != memcmp(data, data2, w*h*4)) { int x,y,c; if (w == w2 && h == h2) for (y=0; y < h; ++y) for (x=0; x < w; ++x) for (c=0; c < 4; ++c) assert(data[y*w*4+x*4+c] == data2[y*w*4+x*4+c]); printf("FAILED: %s loaded but didn't match PRIMARY_check 32-bit version\n", files[i]); } free(data2); } #else stb_splitpath(fname, files[i], STB_FILE); stbi_write_png(stb_sprintf("output/%s.png", fname), w, h, 4, data, w*4); #endif free(data); } if (failed) { int j; printf("FAILED: "); for (j=0; j < stb_arr_len(failed); ++j) printf("%s ", failed[j]); printf(" -- %s\n", files[i]); } } printf("Tested %d files.\n", i); } return 0; }