nux-1.16.0
|
00001 /* 00002 * Copyright 2010 Inalogic® Inc. 00003 * 00004 * This program is free software: you can redistribute it and/or modify it 00005 * under the terms of the GNU Lesser General Public License, as 00006 * published by the Free Software Foundation; either version 2.1 or 3.0 00007 * of the License. 00008 * 00009 * This program is distributed in the hope that it will be useful, but 00010 * WITHOUT ANY WARRANTY; without even the implied warranties of 00011 * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR 00012 * PURPOSE. See the applicable version of the GNU Lesser General Public 00013 * License for more details. 00014 * 00015 * You should have received a copy of both the GNU Lesser General Public 00016 * License along with this program. If not, see <http://www.gnu.org/licenses/> 00017 * 00018 * Authored by: Jay Taoko <jaytaoko@inalogic.com> 00019 * 00020 */ 00021 00022 00023 #include "NuxCore/NuxCore.h" 00024 #include "ImageSurface.h" 00025 #include "GdkGraphics.h" 00026 00027 #include "NuxCore/Logger.h" 00028 00029 namespace nux 00030 { 00031 namespace 00032 { 00033 logging::Logger logger("nux.image"); 00034 } 00035 00036 GdkGraphics::GdkGraphics() 00037 : pixbuf_(0) 00038 { 00039 } 00040 00041 GdkGraphics::GdkGraphics(GdkPixbuf* pixbuf) 00042 : pixbuf_(pixbuf) 00043 { 00044 } 00045 00046 GdkGraphics::GdkGraphics(const char* filename) 00047 : pixbuf_(0) 00048 { 00049 LoadImage(filename); 00050 } 00051 00052 GdkGraphics::~GdkGraphics() 00053 { 00054 if (pixbuf_) 00055 g_object_unref(pixbuf_); 00056 } 00057 00058 bool GdkGraphics::LoadImage(const char* filename) 00059 { 00060 if (pixbuf_) 00061 g_object_unref(pixbuf_); 00062 00063 GError* error = 0; 00064 pixbuf_ = gdk_pixbuf_new_from_file(filename, &error); 00065 00066 if (error) 00067 { 00068 LOG_ERROR(logger) << error->message; 00069 g_error_free(error); 00070 return false; 00071 } 00072 00073 return true; 00074 } 00075 00076 NBitmapData* GdkGraphics::GetBitmap() const 00077 { 00078 if (!pixbuf_) 00079 { 00080 LOG_WARN(logger) << "No pixbuf loaded"; 00081 return 0; 00082 } 00083 00084 unsigned int width = gdk_pixbuf_get_width(pixbuf_); 00085 unsigned int height = gdk_pixbuf_get_height(pixbuf_); 00086 int channels = gdk_pixbuf_get_n_channels(pixbuf_); 00087 int src_pitch = gdk_pixbuf_get_rowstride(pixbuf_); 00088 00089 NTextureData* texture = NULL; 00090 00091 if (channels == 4) 00092 { 00093 texture = new NTextureData(BITFMT_R8G8B8A8, width, height, 1); 00094 } 00095 else if (channels == 3) 00096 { 00097 texture = new NTextureData(BITFMT_R8G8B8, width, height, 1); 00098 } 00099 else if (channels == 1) 00100 { 00101 texture = new NTextureData(BITFMT_A8, width, height, 1); 00102 } 00103 else 00104 { 00105 LOG_ERROR(logger) << __func__ << ": Invalid number of channels"; 00106 return 0; 00107 } 00108 00109 ImageSurface& image_surface = texture->GetSurface(0); 00110 t_u8* dest = image_surface.GetPtrRawData(); 00111 int dest_pitch = image_surface.GetPitch(); 00112 00113 guchar* src = gdk_pixbuf_get_pixels(pixbuf_); 00114 00115 for (unsigned int i = 0; i < height; ++i) 00116 { 00117 Memcpy (dest, src + i*src_pitch, width*channels); 00118 dest += dest_pitch; 00119 } 00120 return texture; 00121 } 00122 } 00123