NXWidgets  1.19
crlepalettebitmap.cxx
Go to the documentation of this file.
1 /****************************************************************************
2  * NxWidgets/libnxwidgets/src/crlepalettebitmap.hxx
3  *
4  * Copyright (C) 2012 Gregory Nutt. All rights reserved.
5  * Author: Gregory Nutt <gnutt@nuttx.org>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  * 3. Neither the name NuttX, NxWidgets, nor the names of its contributors
18  * me be used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *
34  ****************************************************************************
35  *
36  * Portions of this package derive from Woopsi (http://woopsi.org/) and
37  * portions are original efforts. It is difficult to determine at this
38  * point what parts are original efforts and which parts derive from Woopsi.
39  * However, in any event, the work of Antony Dzeryn will be acknowledged
40  * in most NxWidget files. Thanks Antony!
41  *
42  * Copyright (c) 2007-2011, Antony Dzeryn
43  * All rights reserved.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions are met:
47  *
48  * * Redistributions of source code must retain the above copyright
49  * notice, this list of conditions and the following disclaimer.
50  * * Redistributions in binary form must reproduce the above copyright
51  * notice, this list of conditions and the following disclaimer in the
52  * documentation and/or other materials provided with the distribution.
53  * * Neither the names "Woopsi", "Simian Zombie" nor the
54  * names of its contributors may be used to endorse or promote products
55  * derived from this software without specific prior written permission.
56  *
57  * THIS SOFTWARE IS PROVIDED BY Antony Dzeryn ``AS IS'' AND ANY
58  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
59  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
60  * DISCLAIMED. IN NO EVENT SHALL Antony Dzeryn BE LIABLE FOR ANY
61  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
62  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
63  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
64  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
65  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
66  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67  *
68  ****************************************************************************/
69 
70 /****************************************************************************
71  * Included Files
72  ****************************************************************************/
73 
74 #include <nuttx/config.h>
75 
76 #include <cstdint>
77 #include <cstdbool>
78 #include <cstring>
79 
80 #include <nuttx/nx/nxglib.h>
81 
82 #include "nxconfig.hxx"
83 #include "crlepalettebitmap.hxx"
84 
85 /****************************************************************************
86  * Pre-Processor Definitions
87  ****************************************************************************/
88 
89 /****************************************************************************
90  * Method Implementations
91  ****************************************************************************/
92 
93 using namespace NXWidgets;
94 
95 /**
96  * Constructor.
97  *
98  * @param bitmap The bitmap structure being wrapped.
99  */
100 
102 {
103  m_bitmap = bitmap;
104  m_lut = bitmap->lut[0];
105  startOfImage();
106 }
107 
108 /**
109  * Get the bitmap's color format.
110  *
111  * @return The bitmap's width.
112  */
113 
114 const uint8_t CRlePaletteBitmap::getColorFormat(void) const
115 {
116  return m_bitmap->fmt;
117 }
118 
119 /**
120  * Get the bitmap's color format.
121  *
122  * @return The bitmap's color format.
123  */
124 
125 const uint8_t CRlePaletteBitmap::getBitsPerPixel(void) const
126 {
127  return m_bitmap->bpp;
128 }
129 
130 /**
131  * Get the bitmap's width (in pixels/columns).
132  *
133  * @return The bitmap's pixel depth.
134  */
135 
136 const nxgl_coord_t CRlePaletteBitmap::getWidth(void) const
137 {
138  return m_bitmap->width;
139 }
140 
141 /**
142  * Get the bitmap's height (in rows).
143  *
144  * @return The bitmap's height.
145  */
146 
147 const nxgl_coord_t CRlePaletteBitmap::getHeight(void) const
148 {
149  return m_bitmap->height;
150 }
151 
152 /**
153  * Get the bitmap's width (in bytes).
154  *
155  * @return The bitmap's width.
156  */
157 
158 const size_t CRlePaletteBitmap::getStride(void) const
159 {
160  // This only works if the bpp is an even multiple of 8-bit bytes
161 
162  return (m_bitmap->width * m_bitmap->bpp) >> 3;
163 }
164 
165 /**
166  * Use the colors associated with a selected image.
167  *
168  * @param selected. true: Use colors for a selected widget,
169  * false: Use normal (default) colors.
170  */
171 
173 {
174  m_lut = m_bitmap->lut[selected ? 1 : 0];
175 }
176 
177 /**
178  * Get one row from the bit map image using the selected LUT.
179  *
180  * @param x The offset into the row to get
181  * @param y The row number to get
182  * @param width The number of pixels to get from the row
183  * @param data The memory location provided by the caller
184  * in which to return the data. This should be at least
185  * (getWidth()*getBitsPerPixl() + 7)/8 bytes in length
186  * and properly aligned for the pixel color format.
187  * @param True if the run was returned successfully.
188  */
189 
190 bool CRlePaletteBitmap::getRun(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t width,
191  FAR void *data)
192 {
193  // Check ranges. Casts to unsigned int are ugly but permit one-sided comparisons
194 
195  if (((unsigned int)x < (unsigned int)m_bitmap->width) &&
196  ((unsigned int)(x + width) <= (unsigned int)m_bitmap->width))
197  {
198  // Seek to the requested row
199 
200  if (!seekRow(y))
201  {
202  return false;
203  }
204 
205  // Offset to the starting X position
206 
207  if (x > 0)
208  {
209  if (!skipPixels(x))
210  {
211  return false;
212  }
213  }
214 
215  // Then copy the requested number of pixels
216 
217  return copyPixels(width, data);
218  }
219 
220  return false;
221 }
222 
223 /**
224  * Reset to the beginning of the image
225  */
226 
228 {
229  m_row = 0;
230  m_col = 0;
231  m_rle = &m_bitmap->data[0];
233 }
234 
235 /**
236  * Advance position data ahead. Called after npixels have
237  * have been consume.
238  *
239  * @param npixels The number of pixels to advance
240  * @return False if this goes beyond the end of the image
241  */
242 
243 bool CRlePaletteBitmap::advancePosition(nxgl_coord_t npixels)
244 {
245  // Advance to the next column after consuming 'npixels' on this colum
246  int newcol = m_col + npixels;
247 
248  // Have we consumed the entire row?
249 
250  while (newcol >= m_bitmap->width)
251  {
252  // Advance to the next row
253 
254  newcol -= m_bitmap->width;
255  m_row++;
256 
257  // If we still have pixels to account for but we have exceeded the
258  // the size of the bitmap, then return false
259 
260  if (newcol > 0 && m_row >= m_bitmap->height)
261  {
262  return false;
263  }
264  }
265 
266  m_col = newcol;
267  return true;
268 }
269 
270 /**
271  * Seek ahead the specific number of pixels -- discarding
272  * and advancing.
273  *
274  * @param npixels The number of pixels to skip
275  */
276 
277 bool CRlePaletteBitmap::skipPixels(nxgl_coord_t npixels)
278 {
279  do
280  {
281  // Skip values in the current RLE entry
282 
283  if (m_remaining > npixels)
284  {
285  // Subtract from the number of pixels remaining
286 
287  m_remaining -= npixels;
288  return advancePosition(npixels);
289  }
290 
291  // Not enough (or just enough) in the current entry. Take what we
292  // can from the current entry and move to the next entry
293 
294  npixels -= m_remaining;
296  {
297  return false;
298  }
299 
300  m_rle++;
302  }
303  while (npixels > 0);
304 
305  return true;
306 }
307 
308 /** Seek to the beginning of the next row
309  *
310  * @return False if this was the last row of the image
311  */
312 
314 {
315  return skipPixels(m_bitmap->width - m_col);
316 }
317 
318 /** Seek to the beignning specific row
319  *
320  * @param row The row number to seek to
321  * @return False if this goes beyond the end of the image
322  */
323 
324 bool CRlePaletteBitmap::seekRow(nxgl_coord_t row)
325 {
326  // Is the current position already past the requested position?
327 
328  if (row < m_row || (row == m_row && m_col != 0))
329  {
330  // Yes.. rewind to the beginning of the image
331 
332  startOfImage();
333  }
334 
335  // Seek ahead, row-by-row until we are at the beginning of
336  // the requested row
337 
338  while (m_row < row)
339  {
340  if (!nextRow())
341  {
342  return false;
343  }
344  }
345 
346  return true;
347 }
348 
349 /** Copy the pixels from the current RLE entry the specified number of times.
350  *
351  * @param npixels The number of pixels to copy. Must be less than or equal
352  * to m_remaining (not checked here, must be handled by higher level code).
353  * @param data The memory location provided by the caller
354  * in which to return the data. This should be at least
355  * (getWidth()*getBitsPerPixl() + 7)/8 bytes in length
356  * and properly aligned for the pixel color format.
357  */
358 
359 void CRlePaletteBitmap::copyColor(nxgl_coord_t npixels, FAR void *data)
360 {
361  // Right now, only a single pixel depth is supported
362 
364  nxwidget_pixel_t color = nxlut[m_rle->lookup];
365 
366  // Copy the requested pixels
367 
368  FAR nxwidget_pixel_t *dest = (FAR nxwidget_pixel_t *)data;
369  for (int i = 0; i < npixels; i++)
370  {
371  *dest++ = color;
372  }
373 
374  // Adjust the number of pixels remaining in the RLE entry
375 
376  m_remaining -= npixels;
377 }
378 
379 /** Copy pixels from the current position
380  *
381  * @param npixels The number of pixels to copy
382  * @param data The memory location provided by the caller
383  * in which to return the data. This should be at least
384  * (getWidth()*getBitsPerPixl() + 7)/8 bytes in length
385  * and properly aligned for the pixel color format.
386  * @return False if this goes beyond the end of the image
387  */
388 
389 bool CRlePaletteBitmap::copyPixels(nxgl_coord_t npixels, FAR void *data)
390 {
391  FAR nxwidget_pixel_t *ptr = (FAR nxwidget_pixel_t *)data;
392  do
393  {
394  // Get values in the current RLE entry
395 
396  if (m_remaining > npixels)
397  {
398  // Copy npixels from the current RLE entry (it won't be empty)
399 
400  copyColor(npixels, (FAR void *)ptr);
401 
402  // Then advance row/column position information
403 
404  return advancePosition(npixels);
405  }
406 
407  // Not enough (or just enough) in the current entry. Take what we
408  // can from the current entry and move to the next entry
409 
410  uint8_t nTaken = m_remaining; // Temporary.. copyColor clobbers m_remaining
411  npixels -= m_remaining; // New number of pixels needed
412 
413  // Copy all of the colors available in this RLE entry
414 
415  copyColor(m_remaining, (FAR void *)ptr);
416 
417  // Then advance row/column position information
418 
419  if (!advancePosition(nTaken))
420  {
421  return false;
422  }
423 
424  // Advance the destination pointer by the number of pixels taken
425  // from the RLE entry
426 
427  ptr += nTaken;
428 
429  // Then move to the next RLE entry
430 
431  m_rle++;
433  }
434  while (npixels > 0);
435 
436  return true;
437 }
CRlePaletteBitmap(const struct SRlePaletteBitmap *bitmap)
const nxgl_coord_t getHeight(void) const
bool copyPixels(nxgl_coord_t npixels, FAR void *data)
FAR const struct SRlePaletteBitmap * m_bitmap
const nxgl_coord_t getWidth(void) const
uint8_t nxwidget_pixel_t
Definition: nxconfig.hxx:442
void copyColor(nxgl_coord_t npixels, FAR void *data)
FAR const struct SRlePaletteBitmapEntry * m_rle
bool getRun(nxgl_coord_t x, nxgl_coord_t y, nxgl_coord_t width, FAR void *data)
const size_t getStride(void) const
bool advancePosition(nxgl_coord_t npixels)
FAR const struct SRlePaletteBitmapEntry * data
const uint8_t getBitsPerPixel(void) const
bool seekRow(nxgl_coord_t row)
const uint8_t getColorFormat(void) const
bool skipPixels(nxgl_coord_t npixels)
static const NXWidgets::SRlePaletteBitmapEntry bitmap[]