pixel_buffer.h

Go to the documentation of this file.
00001 /*
00002 **  ClanLib SDK
00003 **  Copyright (c) 1997-2011 The ClanLib Team
00004 **
00005 **  This software is provided 'as-is', without any express or implied
00006 **  warranty.  In no event will the authors be held liable for any damages
00007 **  arising from the use of this software.
00008 **
00009 **  Permission is granted to anyone to use this software for any purpose,
00010 **  including commercial applications, and to alter it and redistribute it
00011 **  freely, subject to the following restrictions:
00012 **
00013 **  1. The origin of this software must not be misrepresented; you must not
00014 **     claim that you wrote the original software. If you use this software
00015 **     in a product, an acknowledgment in the product documentation would be
00016 **     appreciated but is not required.
00017 **  2. Altered source versions must be plainly marked as such, and must not be
00018 **     misrepresented as being the original software.
00019 **  3. This notice may not be removed or altered from any source distribution.
00020 **
00021 **  Note: Some of the libraries ClanLib may link to may have additional
00022 **  requirements or restrictions.
00023 **
00024 **  File Author(s):
00025 **
00026 **    Magnus Norddahl
00027 **    Harry Storbacka
00028 **    Mark Page
00029 */
00030 
00033 
00034 #pragma once
00035 
00036 #include "../api_display.h"
00037 #include "../../Core/System/sharedptr.h"
00038 #include "pixel_format.h"
00039 #include "../../Core/Math/rect.h"
00040 #include "texture_format.h"
00041 #include "buffer_usage.h"
00042 
00043 class CL_Size;
00044 class CL_Rect;
00045 class CL_PixelFormat;
00046 class CL_Palette;
00047 class CL_Color;
00048 class CL_Colorf;
00049 class CL_PixelBuffer;
00050 class CL_PixelBuffer_Impl;
00051 class CL_ResourceManager;
00052 class CL_VirtualDirectory;
00053 class CL_IODevice;
00054 class CL_GraphicContext;
00055 class CL_PixelBufferProvider;
00056 
00060 enum CL_PixelBufferDirection
00061 {
00063         cl_data_to_gpu,
00064 
00066         cl_data_from_gpu
00067 };
00068 
00072 class CL_API_DISPLAY CL_PixelBuffer
00073 {
00076 
00077 public:
00079         CL_PixelBuffer();
00080 
00088         CL_PixelBuffer(int width, int height, CL_TextureFormat sized_format, const void *data = 0, bool only_reference_data = false);
00089 
00099         CL_PixelBuffer(CL_GraphicContext &gc, int width, int height, CL_PixelBufferDirection direction = cl_data_to_gpu, CL_TextureFormat sized_format = cl_rgba8, const void *data = 0, CL_BufferUsage usage = cl_usage_stream_draw);
00100 
00107         CL_PixelBuffer(CL_GraphicContext &gc, const CL_PixelBuffer &pbuff, CL_PixelBufferDirection direction = cl_data_to_gpu, CL_BufferUsage usage = cl_usage_stream_draw);
00108 
00116         CL_PixelBuffer(int width, int height, CL_TextureFormat sized_format, const CL_Palette &palette, const void *data = 0);
00117 
00121         CL_PixelBuffer(const CL_StringRef &fullname);
00122 
00127         CL_PixelBuffer(const CL_StringRef &filename, const CL_VirtualDirectory &dir);
00128 
00133         CL_PixelBuffer(CL_IODevice &file, const CL_String &image_type);
00134 
00135         virtual ~CL_PixelBuffer();
00136 
00140 
00141 public:
00143         bool is_null() const { return !impl; }
00144 
00146         void throw_if_null() const;
00147 
00149         CL_PixelBuffer copy() const;
00150 
00152         CL_PixelBuffer copy(const CL_Rect &rect) const;
00153 
00155         const CL_Palette *get_palette() const;
00156 
00158         int get_width() const;
00159 
00161         int get_height() const;
00162 
00164         CL_Size get_size() const;
00165 
00167         unsigned int get_pitch() const;
00168 
00170         void *get_data();
00171 
00172         const void *get_data() const;
00173 
00175         unsigned char *get_data_uint8() { return reinterpret_cast<unsigned char*>(get_data()); }
00176         const unsigned char *get_data_uint8() const { return reinterpret_cast<const unsigned char*>(get_data()); }
00177 
00179         unsigned short *get_data_uint16() { return reinterpret_cast<unsigned short*>(get_data()); }
00180         const unsigned short *get_data_uint16() const { return reinterpret_cast<const unsigned short*>(get_data()); }
00181 
00183         unsigned int *get_data_uint32() { return reinterpret_cast<unsigned int*>(get_data()); }
00184         const unsigned int *get_data_uint32() const { return reinterpret_cast<const unsigned int*>(get_data()); }
00185 
00187         void *get_line(int line) { unsigned char *d = get_data_uint8(); return d + line * get_pitch(); }
00188         const void *get_line(int line) const { const unsigned char *d = get_data_uint8(); return d + line * get_pitch(); }
00189 
00191         unsigned char *get_line_uint8(int line) { return reinterpret_cast<unsigned char*>(get_line(line)); }
00192         const unsigned char *get_line_uint8(int line) const { return reinterpret_cast<const unsigned char*>(get_line(line)); }
00193 
00195         unsigned short *get_line_uint16(int line) { return reinterpret_cast<unsigned short*>(get_line(line)); }
00196         const unsigned short *get_line_uint16(int line) const { return reinterpret_cast<const unsigned short*>(get_line(line)); }
00197 
00199         unsigned int *get_line_uint32(int line) { return reinterpret_cast<unsigned int*>(get_data()); }
00200         const unsigned int *get_line_uint32(int line) const { return reinterpret_cast<const unsigned int*>(get_line(line)); }
00201 
00203         bool has_colorkey() const;
00204 
00206         unsigned int get_colorkey() const;
00207 
00211         int get_bytes_per_pixel() const;
00212 
00214         unsigned int get_red_mask() const;
00215 
00217         unsigned int get_green_mask() const;
00218 
00220         unsigned int get_blue_mask() const;
00221 
00223         unsigned int get_alpha_mask() const;
00224 
00226         CL_TextureFormat get_format() const;
00227 
00231         CL_PixelBufferProvider *get_provider() const;
00232 
00234         CL_Colorf get_pixel(int x, int y);
00235 
00239 
00240 public:
00241 
00243         void lock(CL_BufferAccess access);
00244 
00246         void unlock();
00247 
00249         void upload_data(const CL_Rect &dest_rect, const void *data);
00250 
00252         operator bool () const;
00253 
00258         void convert(CL_PixelBuffer &target) const;
00259 
00266         void convert(CL_PixelBuffer &target, const CL_Rect &dest_rect, const CL_Rect &src_rect = CL_Rect()) const;
00267 
00269         CL_PixelBuffer to_format(CL_TextureFormat sized_format) const;
00270 
00277         void set_colorkey(bool enabled, unsigned int colorkey);
00278 
00280         void flip_vertical();
00281 
00285         void premultiply_alpha();
00286 
00290 
00291 private:
00292         CL_SharedPtr<CL_PixelBuffer_Impl> impl;
00293         friend class CL_PixelBuffer_Impl;
00295 };
00296