]> git.tdb.fi Git - libs/gui.git/blobdiff - source/gbase/image.cpp
Exception changes
[libs/gui.git] / source / gbase / image.cpp
index 4fc74221057d16ba62af5e945d93e5ca9526667d..3360b72e7610d0cdf68eb8d9e75a6993682e1a91 100644 (file)
@@ -6,7 +6,6 @@
 #include <msp/io/file.h>
 #include <msp/io/memory.h>
 #endif
-#include <msp/core/except.h>
 #include "image.h"
 
 using namespace std;
@@ -32,13 +31,13 @@ struct Image::Private
 Image::Private::Private()
 {
 #ifdef WITH_DEVIL
-       id=0;
+       id = 0;
 #endif
 #ifdef WITH_LIBPNG
-       fmt=RGB;
-       width=0;
-       height=0;
-       data=0;
+       fmt = RGB;
+       width = 0;
+       height = 0;
+       data = 0;
 #endif
 }
 
@@ -48,23 +47,23 @@ namespace {
 #ifdef WITH_LIBPNG
 void read(png_struct *png, png_byte *data, png_size_t size)
 {
-       IO::Base *in=reinterpret_cast<IO::Base *>(png_get_io_ptr(png));
+       IO::Base *in = reinterpret_cast<IO::Base *>(png_get_io_ptr(png));
        in->read(reinterpret_cast<char *>(data), size);
 }
 
 void load_png(IO::Base &in, Image::Private &priv)
 {
-       png_struct *png=0;
-       png_info *info=0;
-       priv.data=0;
+       png_struct *png = 0;
+       png_info *info = 0;
+       priv.data = 0;
 
        try
        {
-               png=png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
-               info=png_create_info_struct(png);
+               png = png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
+               info = png_create_info_struct(png);
 
                if(setjmp(png_jmpbuf(png)))
-                       throw Exception("Error loading PNG image");
+                       throw bad_image_data("PNG error");
 
                png_set_read_fn(png, &in, read);
                png_read_info(png, info);
@@ -73,26 +72,26 @@ void load_png(IO::Base &in, Image::Private &priv)
                int depth;
                int color;
                png_get_IHDR(png, info, &width, &height, &depth, &color, 0, 0, 0);
-               priv.width=width;
-               priv.height=height;
+               priv.width = width;
+               priv.height = height;
                if(depth!=8)
-                       throw Exception("Only 8-bit PNG images are supported");
+                       throw unsupported_image_format("depth!=8");
                switch(color)
                {
-               case PNG_COLOR_TYPE_PALETTE:    priv.fmt=COLOR_INDEX; break;
-               case PNG_COLOR_TYPE_GRAY:       priv.fmt=LUMINANCE; break;
-               case PNG_COLOR_TYPE_GRAY_ALPHA: priv.fmt=LUMINANCE_ALPHA; break;
-               case PNG_COLOR_TYPE_RGB:        priv.fmt=RGB; break;
-               case PNG_COLOR_TYPE_RGB_ALPHA:  priv.fmt=RGBA; break;
-               default: throw Exception("Unknown color type");
+               case PNG_COLOR_TYPE_PALETTE:    priv.fmt = COLOR_INDEX; break;
+               case PNG_COLOR_TYPE_GRAY:       priv.fmt = LUMINANCE; break;
+               case PNG_COLOR_TYPE_GRAY_ALPHA: priv.fmt = LUMINANCE_ALPHA; break;
+               case PNG_COLOR_TYPE_RGB:        priv.fmt = RGB; break;
+               case PNG_COLOR_TYPE_RGB_ALPHA:  priv.fmt = RGBA; break;
+               default: throw unsupported_image_format("unknown color type");
                }
 
-               unsigned nchans=png_get_channels(png, info);
+               unsigned nchans = png_get_channels(png, info);
                if(nchans==4 && priv.fmt==RGB)
                        png_set_strip_alpha(png);
 
-               unsigned rowstride=priv.width*nchans;
-               priv.data=new char[rowstride*priv.height];
+               unsigned rowstride = priv.width*nchans;
+               priv.data = new char[rowstride*priv.height];
                for(unsigned y=0; y<priv.height; ++y)
                        png_read_row(png, reinterpret_cast<png_byte *>(priv.data+rowstride*(priv.height-1-y)), 0);
 
@@ -111,14 +110,14 @@ void load_png(IO::Base &in, Image::Private &priv)
 #ifdef WITH_DEVIL
 void ensure_devil_image(unsigned &id)
 {
-       static bool init_done=false;
+       static bool init_done = false;
 
        if(!init_done)
        {
                ilInit();
                ilEnable(IL_ORIGIN_SET);
                ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
-               init_done=true;
+               init_done = true;
        }
 
        if(!id)
@@ -133,7 +132,7 @@ Image::Image():
        priv(new Private)
 {
 #if !defined(WITH_DEVIL) && !defined(WITH_LIBPNG)
-       throw Exception("Image needs either DevIL or libpng support");
+       throw runtime_error("no image support");
 #endif
 }
 
@@ -164,9 +163,9 @@ void Image::load_file(const string &fn)
                ensure_devil_image(priv->id);
                ilBindImage(priv->id);
                if(!ilLoadImage(const_cast<char *>(fn.c_str())))
-                       throw Exception("Error loading image "+fn);
+                       throw bad_image_data("IL error");
 #else
-               throw Exception("Not a PNG image and DevIL support not compiled in");
+               throw unsupported_image_format("DevIL needed for non-PNG images");
 #endif
        }
        (void)fn;
@@ -187,9 +186,9 @@ void Image::load_memory(const void *data, unsigned size)
                ensure_devil_image(priv->id);
                ilBindImage(priv->id);
                if(!ilLoadL(IL_TYPE_UNKNOWN, const_cast<void *>(data), size))
-                       throw Exception("Error loading image from memory");
+                       throw bad_image_data("IL error");
 #else
-               throw Exception("Not a PNG image and DevIL support not compiled in");
+               throw unsupported_image_format("DevIL needed for non-PNG images");
 #endif
        }
        (void)data;
@@ -215,7 +214,8 @@ PixelFormat Image::get_format() const
                case IL_RGBA: return RGBA;
                case IL_BGR: return BGR;
                case IL_BGRA: return BGRA;
-               default: throw InvalidParameterValue("Unknown pixel format in image");
+               // XXX bad, should throw when loading
+               default: throw invalid_argument("unknown pixel format in image");
                }
        }
 #endif