Fawkes API Fawkes Development Version
qa_colormap.cpp
1
2/***************************************************************************
3 * qa_colormap.cpp - QA for colormap
4 *
5 * Created: Tue Apr 01 10:04:27 2008
6 * Copyright 2007-2008 Tim Niemueller [www.niemueller.de]
7 *
8 ****************************************************************************/
9
10/* This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version. A runtime exception applies to
14 * this software (see LICENSE.GPL_WRE file mentioned below for details).
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Library General Public License for more details.
20 *
21 * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22 */
23
24/// @cond QA
25
26#include <core/exception.h>
27#include <fvutils/color/colorspaces.h>
28#include <fvutils/colormap/cmfile.h>
29#include <fvutils/colormap/yuvcm.h>
30#include <fvwidgets/image_display.h>
31
32#include <cstdio>
33#include <cstdlib>
34
35using namespace fawkes;
36using namespace firevision;
37
38#define BIGDEPTH 8
39
40int
41main(int argc, char **argv)
42{
43 const char *filename = "qatest.colormap";
44 if (argc > 1) {
45 filename = argv[1];
46 }
47
48 printf("Creating simple one-plane colormap\n");
49 YuvColormap *cm = new YuvColormap();
50
51 for (unsigned int u = 100; u < 150; ++u) {
52 for (unsigned int v = 100; v < 150; ++v) {
53 cm->set(128, u, v, C_ORANGE);
54 }
55 }
56
57 unsigned char *imgb = malloc_buffer(YUV422_PLANAR, cm->width() * 2, cm->height() * 2);
58 cm->to_image(imgb);
59
60 ImageDisplay *imgd = new ImageDisplay(cm->width() * 2, cm->height() * 2);
61 imgd->show(imgb);
62
63 imgd->loop_until_quit();
64
65 delete cm;
66
67 printf("Trying to create colormap with illegal depth, should throw an exception..");
68 try {
69 cm = new YuvColormap(3);
70 printf(" Test failed, colormap was created\n");
71 delete cm;
72 } catch (Exception &e) {
73 printf(" Test succeeded, caught exception\n");
74 }
75
76 printf("Trying colormap with depth of %u\n", BIGDEPTH);
77 cm = new YuvColormap(BIGDEPTH);
78
79 for (unsigned int d = 0; d < cm->depth(); ++d) {
80 unsigned int y = 256 / cm->depth() * d;
81 printf("d=%u y=%u u=[%u,%u] v=[%u,%u]\n",
82 d,
83 y,
84 cm->depth() * d,
85 cm->depth() * (d + 1),
86 cm->depth() * d,
87 cm->depth() * (d + 1));
88
89 for (unsigned int u = cm->deepness() / cm->depth() * d;
90 u < cm->deepness() / cm->depth() * (d + 1);
91 ++u) {
92 for (unsigned int v = cm->deepness() / cm->depth() * d;
93 v < cm->deepness() / cm->depth() * (d + 1);
94 ++v) {
95 cm->set(y, u, v, C_ORANGE);
96 }
97 }
98
99 cm->to_image(imgb, d);
100 imgd->show(imgb);
101 imgd->loop_until_quit();
102 }
103
104 printf("Saving colormap to a file\n");
105 ColormapFile cmf;
106 cmf.add_colormap(cm);
107 cmf.write(filename);
108
110 printf("Written, created %zu blocks\n", blocks->size());
111 delete blocks;
112 delete cm;
113
114 cmf.clear();
115
116 printf("Reading colormap from file\n");
117 cmf.read(filename);
118
119 Colormap *tcm = cmf.get_colormap();
120 if ((cm = dynamic_cast<YuvColormap *>(tcm)) == 0) {
121 printf("Error, did not get valid yuv colormap\n");
122 } else {
123 printf("Showing all %u colormap levels\n", cm->depth());
124 for (unsigned int d = 0; d < cm->depth(); ++d) {
125 cm->to_image(imgb, d);
126 imgd->show(imgb);
127 imgd->loop_until_quit();
128 }
129 }
130
131 delete cm;
132
133 unsigned int depth = 4, width = 128, height = 128;
134 printf("Trying colormap with low resolution, choosing %dx%dx%d\n", depth, width, height);
135 cm = new YuvColormap(depth, width, height);
136 printf("YuvColormap dimensions: %dx%dx%d\n", cm->depth(), cm->width(), cm->height());
137 ColormapFile cmfr(depth, width, height);
138 delete cm;
139 cmfr.write(filename);
140 cmfr.clear();
141 cmfr.read(filename);
142 cm = dynamic_cast<YuvColormap *>(cmfr.get_colormap());
143 printf("Read back colormap dimensions %dx%dx%d\n", cm->depth(), cm->width(), cm->height());
144 delete cm;
145
146 //delete imgd;
147 //free(imgb);
148 return 0;
149}
150
151/// @endcond
Base class for exceptions in Fawkes.
Definition: exception.h:36
Vector of colormap blocks.
Definition: cmfile.h:61
Colormap file.
Definition: cmfile.h:55
virtual void clear()
Clear internal storage.
Definition: cmfile.cpp:230
ColormapBlockVector * colormap_blocks()
Get colormap blocks.
Definition: cmfile.cpp:131
Colormap * get_colormap()
Get a freshly generated colormap based on current file content.
Definition: cmfile.cpp:164
void add_colormap(Colormap *colormap)
Add colormap.
Definition: cmfile.cpp:89
Colormap interface.
Definition: colormap.h:37
virtual void to_image(unsigned char *yuv422_planar_buffer, unsigned int level=0)
Create image from LUT.
Definition: colormap.cpp:126
virtual void read(const char *file_name)
Read file.
Definition: fvfile.cpp:290
virtual void write(const char *file_name)
Write file.
Definition: fvfile.cpp:243
Simple image display.
Definition: image_display.h:36
void loop_until_quit()
Process SDL events until quit.
void show(colorspace_t colorspace, unsigned char *buffer)
Show image from given colorspace.
YUV Colormap.
Definition: yuvcm.h:36
virtual unsigned int height() const
Get height of colormap.
Definition: yuvcm.cpp:330
virtual unsigned int depth() const
Get depth of colormap.
Definition: yuvcm.cpp:336
virtual unsigned int width() const
Get width of colormap.
Definition: yuvcm.cpp:324
virtual void set(unsigned int y, unsigned int u, unsigned int v, color_t c)
Set color class for given YUV value.
Definition: yuvcm.cpp:194
virtual unsigned int deepness() const
Get deepness of colormap.
Definition: yuvcm.cpp:342
Fawkes library namespace.