]> git.tdb.fi Git - libs/gl.git/blobdiff - source/texture2d.cpp
Fix filename handling in the Blender exporter on Windows
[libs/gl.git] / source / texture2d.cpp
index fa92f1c97e52b3c980485827280def8e1c0057c2..0e76128c7bc5666da5828f11fe98f5398ecaf380 100644 (file)
@@ -1,6 +1,7 @@
 #include <msp/core/raii.h>
 #include <msp/gl/extensions/arb_direct_state_access.h>
 #include <msp/gl/extensions/arb_texture_storage.h>
+#include <msp/graphics/imageloader.h>
 #include "bindable.h"
 #include "buffer.h"
 #include "error.h"
@@ -18,17 +19,17 @@ class Texture2D::AsyncLoader: public Resource::AsyncLoader
 private:
        Texture2D &texture;
        IO::Seekable &io;
-       bool srgb_conversion;
        Buffer pixel_buffer;
        char *mapped_address;
        Graphics::Image image;
+       Graphics::ImageLoader *img_loader;
        unsigned n_bytes;
        int phase;
 
 public:
        AsyncLoader(Texture2D &, IO::Seekable &);
+       ~AsyncLoader();
 
-       void set_srgb_conversion(bool);
        virtual bool needs_sync() const;
        virtual bool process();
 };
@@ -149,25 +150,25 @@ void Texture2D::sub_image(unsigned level, int x, int y, unsigned wd, unsigned ht
        sub_image(level, x, y, wd, ht, data);
 }
 
-void Texture2D::image(const Graphics::Image &img, unsigned lv, bool srgb)
+void Texture2D::image(const Graphics::Image &img, unsigned lv)
 {
-       image(img, lv, srgb, false);
+       image(img, lv, false);
 }
 
-void Texture2D::image(const Graphics::Image &img, unsigned lv, bool srgb, bool from_buffer)
+void Texture2D::image(const Graphics::Image &img, unsigned lv, bool from_buffer)
 {
        unsigned w = img.get_width();
        unsigned h = img.get_height();
        PixelFormat fmt = pixelformat_from_image(img);
        if(width==0)
-               storage(make_pixelformat(get_components(fmt), get_component_type(fmt), srgb), w, h, lv);
+               storage(make_pixelformat(get_components(fmt), get_component_type(fmt), use_srgb_format), w, h, lv);
        else if(w!=width || h!=height || (lv && lv!=levels))
                throw incompatible_data("Texture2D::image");
 
        PixelStore pstore = PixelStore::from_image(img);
        BindRestore _bind_ps(pstore);
 
-       image(0, from_buffer ? 0 : img.get_data());
+       image(0, from_buffer ? 0 : img.get_pixels());
 }
 
 unsigned Texture2D::get_n_levels() const
@@ -188,11 +189,9 @@ void Texture2D::get_level_size(unsigned level, unsigned &w, unsigned &h) const
                h = 1;
 }
 
-Resource::AsyncLoader *Texture2D::load(IO::Seekable &io, const Resources *res)
+Resource::AsyncLoader *Texture2D::load(IO::Seekable &io, const Resources *)
 {
        AsyncLoader *ldr = new AsyncLoader(*this, io);
-       if(res)
-               ldr->set_srgb_conversion(res->get_srgb_conversion());
        return ldr;
 }
 
@@ -248,15 +247,17 @@ void Texture2D::Loader::storage_levels(PixelFormat fmt, unsigned w, unsigned h,
 Texture2D::AsyncLoader::AsyncLoader(Texture2D &t, IO::Seekable &i):
        texture(t),
        io(i),
-       srgb_conversion(false),
        pixel_buffer(PIXEL_UNPACK_BUFFER),
        mapped_address(0),
+       img_loader(Graphics::ImageLoader::open_io(io)),
        phase(0)
 { }
 
-void Texture2D::AsyncLoader::set_srgb_conversion(bool c)
+Texture2D::AsyncLoader::~AsyncLoader()
 {
-       srgb_conversion = c;
+       if(mapped_address)
+               pixel_buffer.unmap();
+       delete img_loader;
 }
 
 bool Texture2D::AsyncLoader::needs_sync() const
@@ -268,9 +269,7 @@ bool Texture2D::AsyncLoader::process()
 {
        if(phase==0)
        {
-               /* TODO Enhance the ImageLoader system so that the image can be loaded
-               directly to the buffer */
-               image.load_io(io);
+               image.load_headers(*img_loader);
                n_bytes = image.get_stride()*image.get_height();
        }
        else if(phase==1)
@@ -279,13 +278,11 @@ bool Texture2D::AsyncLoader::process()
                mapped_address = reinterpret_cast<char *>(pixel_buffer.map());
        }
        else if(phase==2)
-       {
-               const char *data = reinterpret_cast<const char *>(image.get_data());
-               copy(data, data+n_bytes, mapped_address);
-       }
+               image.load_into(*img_loader, mapped_address);
        else if(phase==3)
        {
                Bind _bind_buf(pixel_buffer, PIXEL_UNPACK_BUFFER);
+               mapped_address = 0;
                if(!pixel_buffer.unmap())
                {
                        phase = 1;
@@ -299,7 +296,7 @@ bool Texture2D::AsyncLoader::process()
                        else
                                glGenTextures(1, &texture.id);
                }
-               texture.image(image, 0, srgb_conversion, true);
+               texture.image(image, 0, true);
        }
 
        ++phase;