}
- bool init(const std::string& name, bool flipped = false)
+ void init(const std::string& name, bool flipped = false)
{
- std::string path = Image::getPath(name);
+ std::string path(name);
- logInfo << "opening image file " << path << std::endl;
- FILE* fp = fopen(path.c_str(), "rb");
- if (!fp) return false;
+ FILE* fp = Image::openFile(path);
+ if (!fp) return;
png_byte signature[8];
size_t bytesRead;
png_textp texts = 0;
int numTexts;
- logInfo("checking signature...");
bytesRead = fread(signature, 1, sizeof(signature), fp);
- logInfo << "reading " << bytesRead << " bytes of signature"
- << std::endl;
if (bytesRead < sizeof(signature) ||
png_sig_cmp(signature, 0, sizeof(signature)) != 0) goto cleanup;
- logInfo("creating png structures...");
pngObj = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
if (!pngObj) goto cleanup;
pngInfoEnd = png_create_info_struct(pngObj);
if (!pngInfoEnd) goto cleanup;
- logInfo("setting up long jump...");
if (setjmp(png_jmpbuf(pngObj))) goto cleanup;
png_init_io(pngObj, fp);
png_read_info(pngObj, pngInfo);
bpp = png_get_bit_depth(pngObj, pngInfo);
- logInfo << "texture bpp: " << bpp << std::endl;
colors = png_get_color_type(pngObj, pngInfo);
switch (colors)
{
channels = png_get_channels(pngObj, pngInfo);
mDepth = bpp * channels;
- logInfo << "texture channels: " << channels << std::endl;
if (channels == 3) mColorMode = GL_RGB;
else mColorMode = GL_RGBA;
// read comments
png_get_text(pngObj, pngInfo, &texts, &numTexts);
- logInfo << "num texts: " << numTexts << std::endl;
for (int i = 0; i < numTexts; ++i)
{
if (strncmp(texts[i].key, "TextureInfo", 11) == 0)
cleanup:
- logInfo("cleaning up...");
delete[] rows;
png_destroy_read_struct(pngObj ? &pngObj : 0,
pngInfo ? &pngInfo : 0,
pngInfoEnd ? &pngInfoEnd : 0);
fclose(fp);
-
- return mContext;
}
}
-std::string Image::getPath(const std::string& name)
+bool Image::getPath(std::string& name)
{
- if (boost::find_last(name, ".png"))
- {
- return Resource::getPath(name);
- }
- else
- {
- std::string path("images/");
- path += name;
- path += ".png";
- return Resource::getPath(path);
- }
+ return Resource::getPath(name, "images/", "png");
+}
+
+FILE* Image::openFile(std::string& name)
+{
+ return Resource::openFile(name, "images/", "png");
}