File: pldirectfbbmp.cpp

package info (click to toggle)
paintlib 2.6.2-14
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 7,920 kB
  • ctags: 3,874
  • sloc: cpp: 25,209; sh: 10,605; ansic: 1,891; makefile: 120
file content (302 lines) | stat: -rw-r--r-- 7,239 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
/--------------------------------------------------------------------
|
|      $Id: pldirectfbbmp.cpp,v 1.9 2004/06/12 14:52:46 uzadow Exp $
|      Bitmap class for SDL surfaces.
|
|      Copyright (c) 1996-2002 Ulrich von Zadow
|
\--------------------------------------------------------------------
*/

#include "plstdpch.h"
#include "pldirectfbbmp.h"
#include "plexcept.h"

#include <iostream>

using namespace std;

IDirectFB * PLDirectFBBmp::s_pDirectFB = 0;


void PLDirectFBBmp::SetDirectFB
  ( IDirectFB * pDirectFB
  )
{
  s_pDirectFB = pDirectFB;
}

PLDirectFBBmp::PLDirectFBBmp
  ()
    // Creates an empty bitmap.
{
  PLASSERT (s_pDirectFB);

  m_pSurface=0;
  internalCreate(16, 16, 32, false, false);

  PLASSERT_VALID(this);
}


PLDirectFBBmp::~PLDirectFBBmp
    ()
{
  // Free the memory.
  freeMembers();
}

void PLDirectFBBmp::CreateFromSurface
    ( IDirectFBSurface * pSurface,
      bool bOwnsSurface
    )
{
  freeMembers();

  m_pSurface = pSurface;
  m_bOwnsSurface = bOwnsSurface;

  DFBSurfacePixelFormat PixelFormat;
  m_pSurface->GetPixelFormat(m_pSurface, &PixelFormat); 
  int w, h;
  m_pSurface->GetSize(m_pSurface, &w, &h);

  bool bAlphaChannel = false;
  int bpp;
  bool bIsGreyscale = false;
  switch (PixelFormat) 
  {
    case DSPF_ARGB:
        bAlphaChannel = true;
        bpp = 32;
        break;
    case DSPF_RGB32:
        bpp = 32;
        break;
    case DSPF_RGB24:
        bpp = 24;
        break;
    case DSPF_RGB16:
        bpp = 16;
        break;
    case DSPF_A8:
        bpp = 8;
        bIsGreyscale = true;
        break;
    default:
        throw PLTextException (PL_ERRDFB, 
                "Unsupported pixel format in CreateFromSurface.");
  }

  if (bpp <= 8)
    m_pClrTab = new PLPixel32 [1 << bpp];
   else
    m_pClrTab = NULL;
  initLocals (w, h, bpp, bAlphaChannel, bIsGreyscale);
}


#ifdef _DEBUG
void PLDirectFBBmp::AssertValid () const
{
  PLBmp::AssertValid();
  PLASSERT (m_bpp == 32 || m_bpp == 24 || m_bpp == 16 || m_bpp == 8);
  DFBSurfacePixelFormat PixelFormat;
  m_pSurface->GetPixelFormat(m_pSurface, &PixelFormat);
  PLASSERT (PixelFormat == DSPF_ARGB || 
            PixelFormat == DSPF_A8 ||
            PixelFormat == DSPF_RGB16 || 
            PixelFormat == DSPF_RGB24 || 
            PixelFormat == DSPF_RGB32);
  int w, h;
  m_pSurface->GetSize(m_pSurface, &w, &h);
  PLASSERT (w == m_Size.x);
  PLASSERT (h == m_Size.y);
}
#endif

IDirectFBSurface * PLDirectFBBmp::GetSurface
    ()
{
  return m_pSurface;
}


long PLDirectFBBmp::GetMemUsed
    ()
    // Returns the memory used by the object.
{
  PLASSERT_VALID (this);

  return GetBytesPerLine()*GetHeight()+sizeof(IDirectFBSurface)+sizeof(*this);
}


long PLDirectFBBmp::GetBytesPerLine
    ()
    // Returns number of bytes used per line.
{
  return m_Size.x*m_bpp/8;
}


/////////////////////////////////////////////////////////////////////
// Static functions

long PLDirectFBBmp::GetBitsMemNeeded
    ( PLLONG width,
      PLLONG height,
      PLWORD BitsPerPixel
    )
    // Returns memory needed by bitmap bits. This is only approximate 
    // and doesn't take the stride into account.
{
  // Calculate memory per line.
  int LineMem = width*BitsPerPixel/8;

  // Multiply by number of lines
  return LineMem*height;
}


long PLDirectFBBmp::GetMemNeeded
    ( PLLONG width,
      PLLONG height,
      PLWORD BitsPerPixel
    )
    // Returns memory needed by a bitmap with the specified attributes.
    // This is only approximate and doesn't take the stride into account.
    // GetMemUsed is more accurate.
{
  int HeaderMem = sizeof (PLDirectFBBmp);
  if (BitsPerPixel < 16)
  { // Palette memory
    HeaderMem += (1 << BitsPerPixel)*sizeof (PLPixel32);
  }

  return HeaderMem+GetBitsMemNeeded (width, height, BitsPerPixel);
}


/////////////////////////////////////////////////////////////////////
// Local functions


void PLDirectFBBmp::internalCreate
    ( PLLONG Width,
      PLLONG Height,
      PLWORD BitsPerPixel,
      bool bAlphaChannel,
      bool bIsGreyscale
    )
    // Create a new empty bitmap. Bits are uninitialized.
    // Assumes that no memory is allocated before the call.
    // The bitmap is stored in a DirectFB system memory surface.
{
  PLASSERT (BitsPerPixel==32 || BitsPerPixel==24 ||
            BitsPerPixel==16 || BitsPerPixel==8);
  DFBSurfaceDescription Desc;
  Desc.flags = DFBSurfaceDescriptionFlags (DSDESC_CAPS | DSDESC_WIDTH | 
            DSDESC_HEIGHT | DSDESC_PIXELFORMAT);
  Desc.caps = DSCAPS_SYSTEMONLY;
  Desc.width = Width;
  Desc.height = Height;
  switch(BitsPerPixel) 
  {
    case 32:
      if (bAlphaChannel) 
        Desc.pixelformat = DSPF_ARGB;
      else
        Desc.pixelformat = DSPF_RGB32;
      break;
    case 24:
      Desc.pixelformat = DSPF_RGB24;
      break;
    case 16:
      Desc.pixelformat = DSPF_RGB16;
      break;
    case 8:
      Desc.pixelformat = DSPF_A8;
      break;
  }
  DFBResult err = s_pDirectFB->CreateSurface(s_pDirectFB, &Desc, &m_pSurface);
  if (err)
    throw PLTextException(PL_ERRDFB, "pDFB->CreateSurface() failed."); 

  if (BitsPerPixel <= 8)
    m_pClrTab = new PLPixel32 [1 << BitsPerPixel];
   else
    m_pClrTab = NULL;
  initLocals (Width, Height, BitsPerPixel, bAlphaChannel, bIsGreyscale);
  m_bOwnsSurface = true;
}


void PLDirectFBBmp::initLineArray
    ()
    // Note that this function unlocks the surface after getting the pixel offsets,
    // which is probably ok for system memory surfaces but will definitely break 
    // for video memory surfaces.
{
  void * pPixels;
  int Pitch;
  m_pSurface->Lock(m_pSurface, DFBSurfaceLockFlags (DSLF_READ | DSLF_WRITE), 
            &pPixels, &Pitch); 
  m_pLineArray = new PLBYTE * [m_Size.y];
  for (int y=0; y<m_Size.y; y++)
    m_pLineArray[y] = (PLBYTE*)(pPixels) + y*Pitch;
  m_pSurface->Unlock(m_pSurface);
}

void PLDirectFBBmp::freeMembers
    ()
{
  if (m_bOwnsSurface) {
    DFBResult err = m_pSurface->Release(m_pSurface);
    if (err) {
      // This should definitely throw an exception...
      cerr << __FILE__ << __LINE__ << ": " << DirectFBErrorString(err) << endl;
    }
    m_pSurface = 0;
  }

  delete [] m_pLineArray;
  m_pLineArray = NULL;
}

/*
/--------------------------------------------------------------------
|
|      $Log: pldirectfbbmp.cpp,v $
|      Revision 1.9  2004/06/12 14:52:46  uzadow
|      Added CreateFromSurface function.
|
|      Revision 1.8  2004/06/09 21:34:53  uzadow
|      Added 16 bpp support to plbitmap, planybmp and pldirectfbbmp
|
|      Revision 1.7  2004/06/06 12:56:38  uzadow
|      Doxygenified documentation.
|
|      Revision 1.6  2004/04/16 20:31:17  uzadow
|      Added 24 bpp support.
|
|      Revision 1.5  2004/02/15 22:43:31  uzadow
|      Added 8-bit-support to DFBBitmap and PLFilterFill
|
|      Revision 1.4  2003/11/21 23:35:44  uzadow
|      Removed files built by the autotools.
|
|      Revision 1.3  2003/07/29 21:27:41  uzadow
|      Fixed PLDirectFBBmp::GetBytesPerLine(), im2* Makefiles
|
|      Revision 1.2  2003/07/27 18:08:38  uzadow
|      Added plfilterfliprgb
|
|      Revision 1.1  2003/07/27 13:50:48  uzadow
|      Added support for DirectFB surfaces.
|
|
|
\--------------------------------------------------------------------
*/