Пример #1
0
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;
}
Пример #2
0
/* kdGetImageInfoATX, kdGetImageInfoFromStreamATX: Construct an informational image object based on an image in a file or stream. */
KD_API KDImageATX KD_APIENTRY kdGetImageInfoATX(const KDchar *pathname)
{
    _KDImageATX *image = (_KDImageATX *)kdMalloc(sizeof(_KDImageATX));
    if(image == KD_NULL)
    {
        kdSetError(KD_ENOMEM);
        return KD_NULL;
    }
    image->levels = 0;

    KDStat st;
    if(kdStat(pathname, &st) == -1)
    {
        kdFree(image);
        kdSetError(KD_EIO);
        return KD_NULL;
    }
    image->size = (KDsize)st.st_size;

#if defined(__unix__) || defined(__APPLE__) || defined(__EMSCRIPTEN__)
    KDint fd = __kdOpen(pathname, O_RDONLY | O_CLOEXEC, 0);
    if(fd == -1)
#elif(_WIN32)
    WIN32_FIND_DATA data;
    HANDLE fd = FindFirstFileA(pathname, &data);
    if(fd == INVALID_HANDLE_VALUE)
#endif
    {
        kdFree(image);
        kdSetError(KD_EIO);
        return KD_NULL;
    }

    void *filedata = KD_NULL;
#if defined(__unix__) || defined(__APPLE__) || defined(__EMSCRIPTEN__)
    filedata = mmap(KD_NULL, image->size, PROT_READ, MAP_PRIVATE, fd, 0);
    if(filedata == MAP_FAILED)
#elif defined(_WIN32)
    HANDLE fm = CreateFileMapping(fd, KD_NULL, PAGE_READONLY, 0, 0, KD_NULL);
    if(fm)
    {
        filedata = MapViewOfFile(fm, FILE_MAP_READ, 0, 0, image->size);
    }
    if(filedata == KD_NULL)
#endif
    {
#if defined(__unix__) || defined(__APPLE__) || defined(__EMSCRIPTEN__)
        close(fd);
#elif defined(_WIN32)
        CloseHandle(fd);
#endif
        kdFree(image);
        kdSetError(KD_EIO);
        return KD_NULL;
    }

    KDint channels = 0;
    KDint error = stbi_info_from_memory(filedata, (KDint)image->size, &image->width, &image->height, &channels);
    switch(channels)
    {
        case(4):
        {
            image->format = KD_IMAGE_FORMAT_RGBA8888_ATX;
            image->alpha = KD_TRUE;
            break;
        }
        case(3):
        {
            image->format = KD_IMAGE_FORMAT_RGB888_ATX;
            image->alpha = KD_FALSE;
            break;
        }
        case(2):
        {
            image->format = KD_IMAGE_FORMAT_LUMALPHA88_ATX;
            image->alpha = KD_TRUE;
            break;
        }
        case(1):
        {
            image->format = KD_IMAGE_FORMAT_LUM8_ATX;
            image->alpha = KD_FALSE;
            break;
        }
        default:
        {
            error = 0;
            break;
        }
    }

#if defined(__unix__) || defined(__APPLE__) || defined(__EMSCRIPTEN__)
    munmap(filedata, image->size);
    close(fd);
#elif defined(_WIN32)
    UnmapViewOfFile(filedata);
    CloseHandle(fd);
#endif

    if(error == 0)
    {
        kdFree(image);
        kdSetError(KD_EILSEQ);
        return KD_NULL;
    }

    return image;
}