]> Dogcows Code - chaz/yoink/blobdiff - src/moof/image.cc
remove some unused stlplus modules
[chaz/yoink] / src / moof / image.cc
index b9076f0c9ec1a46d3808851136155971574c6d4a..09f797ca8f3fc3d9235fb7ccf4f1ad21f5f84eae 100644 (file)
@@ -1,13 +1,11 @@
 
-/*]  Copyright (c) 2009-2010, Charles McGarvey  [**************************
+/*]  Copyright (c) 2009-2011, Charles McGarvey  [*****************************
 **]  All rights reserved.
 *
-* vi:ts=4 sw=4 tw=75
-*
 * Distributable under the terms and conditions of the 2-clause BSD license;
 * see the file COPYING for a complete text of the license.
 *
-**************************************************************************/
+*****************************************************************************/
 
 #include <cstring>             // strncmp
 #include <fstream>
@@ -60,19 +58,20 @@ image::image(const std::string& path) :
        wrap_s_(GL_CLAMP),
        wrap_t_(GL_CLAMP)
 {
-       std::ifstream file(path.c_str());
-       if (!file.good()) throw std::runtime_error("no valid image found at " + path);
+       std::ifstream file(path.c_str(), std::ifstream::binary);
+       if (!file.good())
+               throw std::runtime_error("no valid image found at " + path);
 
        png_byte        signature[8];
        size_t          bytesRead;
 
-       int bpp;
+       int             bpp;
 
        png_byte        colors;
        png_bytepp      rows = 0;
 
        png_textp       texts = 0;
-       int                     nutext_s;
+       int             nutext_s;
 
        bytesRead = file.read((char*)signature, sizeof(signature)).gcount();
        if (bytesRead < sizeof(signature) ||
@@ -80,10 +79,11 @@ image::image(const std::string& path) :
 
        struct png
        {
-               png_structp context;
+               png_structp     context;
                png_infop       info;
                png() :
-                       context(png_create_read_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0)),
+                       context(png_create_read_struct(PNG_LIBPNG_VER_STRING,
+                                               0, 0, 0)),
                        info(png_create_info_struct(context))
                {
                        if (!context || !info) throw 0;
@@ -91,7 +91,7 @@ image::image(const std::string& path) :
                ~png()
                {
                        png_destroy_read_struct(context ? &context : 0,
-                                                                       info    ? &info    : 0, 0);
+                                       info ? &info : 0, 0);
                }
        } png;
 
@@ -105,17 +105,17 @@ image::image(const std::string& path) :
        colors = png_get_color_type(png.context, png.info);
        switch (colors)
        {
-               case PNG_COLOR_TYPE_PALETTE:
-                       png_set_palette_to_rgb(png.context);
-                       break;
+       case PNG_COLOR_TYPE_PALETTE:
+               png_set_palette_to_rgb(png.context);
+               break;
 
-               case PNG_COLOR_TYPE_GRAY:
-                       if (bpp < 8) png_set_expand(png.context);
-                       break;
+       case PNG_COLOR_TYPE_GRAY:
+               if (bpp < 8) png_set_expand(png.context);
+               break;
 
-               case PNG_COLOR_TYPE_GRAY_ALPHA:
-                       png_set_gray_to_rgb(png.context);
-                       break;
+       case PNG_COLOR_TYPE_GRAY_ALPHA:
+               png_set_gray_to_rgb(png.context);
+               break;
        }
 
        if (bpp == 16) png_set_strip_16(png.context);
@@ -127,14 +127,14 @@ image::image(const std::string& path) :
        depth_ = bpp * channels_;
 
        // read comments
-       bool    isTexture = false;
+       bool texture = false;
        png_get_text(png.context, png.info, &texts, &nutext_s);
        for (int i = 0; i < nutext_s; ++i)
        {
                if (strncmp(texts[i].key, "X-Yoink-Texture", 11) == 0)
                {
                        set_texture_info(texts[i].text);
-                       isTexture = true;
+                       texture = true;
                        break;
                }
        }
@@ -146,12 +146,13 @@ image::image(const std::string& path) :
        pixels_ = new char[width_ * pitch_];
 
        rows = new png_bytep[height_];
-       if (isTexture)
+       if (texture)
        {
                log_debug("texture detected; loading flipped");
                for (int i = 0; i < height_; ++i)
                {
-                       rows[height_-1-i] = (png_bytep)(pixels_ + i * channels_ * width_);
+                       rows[height_-1-i] = (png_bytep)(pixels_ +
+                                       i * channels_ * width_);
                }
        }
        else
@@ -159,7 +160,8 @@ image::image(const std::string& path) :
                log_debug("no texture attributes found");
                for (int i = 0; i < height_; ++i)
                {
-                       rows[i] = (png_bytep)(pixels_ + i * channels_ * width_);
+                       rows[i] = (png_bytep)(pixels_ +
+                                       i * channels_ * width_);
                }
        }
 
@@ -202,11 +204,9 @@ void image::set_as_icon() const
        );
 
        SDL_WM_SetIcon(context, 0);
-
        SDL_FreeSurface(context);
 }
 
-
 bool image::tile_coordinates(int index, scalar coords[8]) const
 {
        // make sure the index represents a real tile
@@ -227,7 +227,6 @@ bool image::tile_coordinates(int index, scalar coords[8]) const
        return true;
 }
 
-
 void image::bind() const
 {
        ASSERT(video::current() && "should have a video context set");
@@ -251,25 +250,20 @@ void image::reset_binding()
        global_object_ = 0;
 }
 
-
 /*
  * Upload the image to GL so that it will be accessible by a much more
  * manageable handle and hopefully reside in video memory.
  */
 void image::upload_to_gl() const
 {
-       if (object_)
-       {
-               // already loaded
-               return;
-       }
+       if (object_) return;    // already loaded
 
        glGenTextures(1, (GLuint*)&object_);
        glBindTexture(GL_TEXTURE_2D, (GLuint)object_);
 
        GLuint mode;
        if (channels_ == 3) mode = GL_RGB;
-       else                mode = GL_RGBA;
+       else mode = GL_RGBA;
        
        glTexImage2D
        //gluBuild2DMipmaps
@@ -299,11 +293,7 @@ void image::unload_from_gl() const
 {
        if (object_)
        {
-               if (object_ == global_object_)
-               {
-                       global_object_ = 0;
-               }
-
+               if (object_ == global_object_) global_object_ = 0;
                glDeleteTextures(1, (GLuint*)&object_);
                object_ = 0;
        }
@@ -327,7 +317,6 @@ void image::set_properties() const
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap_t_);
 }
 
-
 void image::set_texture_info(const std::string& info)
 {
        script script;
This page took 0.0244 seconds and 4 git commands to generate.