Commit 81320d9f authored by Jan Lepper's avatar Jan Lepper

add isIMF(util::File&), getIMFInfo(), loadIMF() returning GenericImage

parent b763e438
......@@ -20,6 +20,7 @@
#include <il2ge/image_loader.h>
#include <render_util/image_loader.h>
#include <render_util/image.h>
#include <render_util/image_util.h>
#include <vector>
......@@ -30,15 +31,16 @@ namespace
{
bool isIMF(const vector<char> &data)
const string g_imf_header =
{
static const string imf_header =
{
'I', 'M', 'F', 0x1A, 0x31, 0x30, 0x00
};
'I', 'M', 'F', 0x1A, 0x31, 0x30, 0x00
};
if (data.size() > imf_header.size())
return imf_header.compare(0, string::npos, data.data(), imf_header.size()) == 0;
bool isIMF(const vector<char> &data)
{
if (data.size() > g_imf_header.size())
return g_imf_header.compare(0, string::npos, data.data(), g_imf_header.size()) == 0;
else
return false;
}
......@@ -51,7 +53,6 @@ loadImageFromIMF(const vector<char> &data, const char *field_name)
int width, height;
loadIMF(data, rgba_data, width, height, field_name);
return make_shared<render_util::GenericImage>(glm::ivec2(width, height), std::move(rgba_data), 4);
}
......@@ -82,10 +83,59 @@ loadImageFromMemory<render_util::ImageRGBA>(const std::vector<char> &data, const
}
bool isIMF(util::File &file)
{
bool is_imf = false;
try
{
vector<char> data(g_imf_header.size());
auto read = file.read(data.data(), data.size());
if (read == data.size())
{
is_imf = g_imf_header.compare(0, string::npos, data.data(), data.size()) == 0;
}
}
catch(...) {}
file.rewind();
return is_imf;
}
void getIMFInfo(util::File &file, int &w, int &h)
{
return ::getIMFInfo(file, w, h);
}
std::unique_ptr<render_util::GenericImage>
loadIMF(const std::vector<char> &data, int force_channels)
{
vector<unsigned char> image_data;
int width, height;
::loadIMF(data, image_data, width, height, "");
auto image = make_unique<render_util::GenericImage>(glm::ivec2(width, height),
std::move(image_data), 4);
if (force_channels && force_channels != 4)
{
auto new_image = render_util::image::makeGeneric(image, force_channels);
image = std::move(new_image);
}
return image;
}
std::shared_ptr<render_util::GenericImage> loadImageFromMemory(const std::vector<char> &data,
const char *name)
{
if (isIMF(data))
if (::isIMF(data))
return loadImageFromIMF(data, name);
else
return render_util::loadImageFromMemory<render_util::GenericImage>(data);
......@@ -95,7 +145,7 @@ std::shared_ptr<render_util::GenericImage> loadImageFromMemory(const std::vector
std::shared_ptr<render_util::ImageRGBA> loadImageRGBAFromMemory(const std::vector<char> &data,
const char *name)
{
if (isIMF(data))
if (::isIMF(data))
return loadImageRGBAFromIMF(data, name);
else
return render_util::loadImageFromMemory<render_util::ImageRGBA>(data);
......@@ -105,8 +155,8 @@ std::shared_ptr<render_util::ImageRGBA> loadImageRGBAFromMemory(const std::vecto
std::shared_ptr<render_util::ImageRGB> loadImageRGBFromMemory(const std::vector<char> &data,
const char *name)
{
assert(!isIMF(data));
if (isIMF(data))
assert(!::isIMF(data));
if (::isIMF(data))
{
abort();
}
......
......@@ -57,6 +57,21 @@ namespace
}
void getIMFInfo(util::File &file, int &w, int &h)
{
file.skip(8);
char size_header[4];
file.read(size_header, sizeof(size_header));
w = size_header[0] | (size_header[1] << 8);
h = size_header[2] | (size_header[3] << 8);
file.rewind();
}
void loadIMF(const std::vector<char> &src, std::vector<unsigned char> &out,
int &width, int &height, const std::string &name)
{
......
......@@ -18,9 +18,12 @@
#ifndef IMF_H
#define IMF_H
#include <file.h>
#include <vector>
#include <string>
void getIMFInfo(util::File &file, int &w, int &h);
void loadIMF(const std::vector<char> &src, std::vector<unsigned char> &data,
int &width, int &height, const std::string &name);
......
......@@ -21,6 +21,7 @@
#include <render_util/image.h>
#include <render_util/image_loader.h>
#include <file.h>
namespace il2ge
{
......@@ -32,11 +33,13 @@ std::shared_ptr<T> loadImageFromMemory(const std::vector<char> &data, const char
return render_util::loadImageFromMemory<T>(data);
}
template <>
std::shared_ptr<render_util::ImageRGBA>
loadImageFromMemory<render_util::ImageRGBA>(const std::vector<char> &data, const char *name);
bool isIMF(util::File &file);
void getIMFInfo(util::File&, int &w, int &h);
std::unique_ptr<render_util::GenericImage> loadIMF(const std::vector<char> &data, int force_channels);
std::shared_ptr<render_util::GenericImage> loadImageFromMemory(const std::vector<char> &data,
const char *name);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment