Simple Image Loading LibrarY 0.1.0
SILLYImage.cpp
1/***********************************************************************
2 filename: SILLYImage.cpp
3 created: 10 Jun 2006
4 author: Olivier Delannoy
5
6 purpose: Implementation of the Image class
7*************************************************************************/
8/***************************************************************************
9 * Copyright (C) 2004 - 2006 Paul D Turner & The CEGUI Development Team
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining
12 * a copy of this software and associated documentation files (the
13 * "Software"), to deal in the Software without restriction, including
14 * without limitation the rights to use, copy, modify, merge, publish,
15 * distribute, sublicense, and/or sell copies of the Software, and to
16 * permit persons to whom the Software is furnished to do so, subject to
17 * the following conditions:
18 *
19 * The above copyright notice and this permission notice shall be
20 * included in all copies or substantial portions of the Software.
21 *
22 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
26 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
27 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
28 * OTHER DEALINGS IN THE SOFTWARE.
29 ***************************************************************************/
30#ifdef HAVE_CONFIG_H
31#include <config.h>
32#endif
33
34#include "SILLYImage.h"
35
36#ifndef SILLY_OPT_INLINE
37#define inline
38#include "SILLYImage.icpp"
39#undef inline
40#endif
41#include "SILLYImageLoaderManager.h"
42
43
44// Start section of namespace SILLY
45namespace SILLY
46{
47
49 : d_bpp(0), d_pixels(0), d_data(&source), d_imageContext(0), d_imageLoader(0)
50{
51}
52
53
55{
56 ImageLoaderList::iterator iter = ImageLoaderManager::getSingleton().begin();
57 for (; ! d_imageLoader && iter != ImageLoaderManager::getSingleton().end() ; ++iter)
58 {
59 d_imageContext = (*iter)->loadHeader(d_pfSource, d_data);
60 if (d_imageContext)
61 d_imageLoader = (*iter);
62 }
63 assert((! d_imageLoader || d_imageContext) && "ASSERT: Internal state of image invalid");
64 return d_imageLoader != 0;
65
66}
67
69{
70 switch (resultFormat)
71 {
72 case PF_A1B5G5R5:
73 d_bpp = 2;
74 break;
75 case PF_RGB:
76 d_bpp = 3;
77 break;
78
79 case PF_RGBA:
80 d_bpp = 4;
81 break;
82 //default:
83 // Unsupported format
84 };
85
86 if (! allocate())
87 {
88 return false;
89 }
90 d_imageContext->setDestination(d_pixels, getWidth() * getHeight() * d_bpp, resultFormat);
91
92 if (! d_imageLoader->loadImageData(order, d_data, d_imageContext))
93 {
94 delete [] d_pixels;
95 return false;
96 }
97 return true;
98}
99
100bool Image::allocate()
101{
102 delete [] d_pixels;
103 d_pixels = 0;
104 d_pixels = new byte[d_bpp * getWidth() * getHeight()];
105 return d_pixels != 0;
106}
107
108} // End section of namespace SILLY
Simple Image Loading LibrarY namespace.
PixelFormat
List all pixel format supported.
Definition SILLYBase.h:60
PixelOrigin
List all pixel origin supported.
Definition SILLYBase.h:71
This is an abstract class used to provide data to the loader.
Image(DataSource &data)
Constructor.
bool loadImageHeader()
Retrieve the information concerning the image object.
bool loadImageData(PixelFormat resultFormat=PF_RGBA, PixelOrigin origin=PO_TOP_LEFT)
Load the image pixels in memory and store them in resultFormat.
size_t getWidth() const
Retrieve the width of the image.
size_t getHeight() const
Retrieve the height of the image.
void setDestination(byte *pixels, size_t length, PixelFormat format)
Set the destination of the loading.
virtual bool loadImageData(PixelOrigin origin, DataSource *data, ImageContext *context)=0
Parse the pixels data of the image and fill the header struct.