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 <fvutils/colormap/yuvcm.h>
27 #include <fvutils/colormap/cmfile.h>
28 #include <fvutils/color/colorspaces.h>
29 
30 #include <fvwidgets/image_display.h>
31 #include <core/exception.h>
32 
33 #include <cstdlib>
34 #include <cstdio>
35 
36 using namespace fawkes;
37 using namespace firevision;
38 
39 #define BIGDEPTH 8
40 
41 int
42 main(int argc, char **argv)
43 {
44 
45  const char *filename = "qatest.colormap";
46  if ( argc > 1 ) {
47  filename = argv[1];
48  }
49 
50  printf("Creating simple one-plane colormap\n");
51  YuvColormap *cm = new YuvColormap();
52 
53  for (unsigned int u = 100; u < 150; ++u) {
54  for (unsigned int v = 100; v < 150; ++v) {
55  cm->set(128, u, v, C_ORANGE);
56  }
57  }
58 
59  unsigned char *imgb = malloc_buffer(YUV422_PLANAR, cm->width() * 2, cm->height() * 2);
60  cm->to_image(imgb);
61 
62  ImageDisplay *imgd = new ImageDisplay(cm->width() * 2, cm->height() * 2);
63  imgd->show(imgb);
64 
65  imgd->loop_until_quit();
66 
67  delete cm;
68 
69  printf("Trying to create colormap with illegal depth, should throw an exception..");
70  try {
71  cm = new YuvColormap(3);
72  printf(" Test failed, colormap was created\n");
73  delete cm;
74  } catch (Exception &e) {
75  printf(" Test succeeded, caught exception\n");
76  }
77 
78  printf("Trying colormap with depth of %u\n", BIGDEPTH);
79  cm = new YuvColormap(BIGDEPTH);
80 
81  for (unsigned int d = 0; d < cm->depth(); ++d) {
82  unsigned int y = 256 / cm->depth() * d;
83  printf("d=%u y=%u u=[%u,%u] v=[%u,%u]\n", d, y,
84  cm->depth() * d, cm->depth() * (d+1), cm->depth() * d, cm->depth() * (d+1));
85 
86  for (unsigned int u = cm->deepness() / cm->depth() * d; u < cm->deepness() / cm->depth() * (d+1); ++u) {
87  for (unsigned int v = cm->deepness() / cm->depth() * d; v < cm->deepness() / cm->depth() * (d+1); ++v) {
88  cm->set(y, u, v, C_ORANGE);
89  }
90  }
91 
92  cm->to_image(imgb, d);
93  imgd->show(imgb);
94  imgd->loop_until_quit();
95  }
96 
97  printf("Saving colormap to a file\n");
98  ColormapFile cmf;
99  cmf.add_colormap(cm);
100  cmf.write(filename);
101 
103  printf("Written, created %zu blocks\n", blocks->size());
104  delete blocks;
105  delete cm;
106 
107  cmf.clear();
108 
109  printf("Reading colormap from file\n");
110  cmf.read(filename);
111 
112  Colormap *tcm = cmf.get_colormap();
113  if ( (cm = dynamic_cast<YuvColormap *>(tcm)) == 0 ) {
114  printf("Error, did not get valid yuv colormap\n");
115  } else {
116  printf("Showing all %u colormap levels\n", cm->depth());
117  for (unsigned int d = 0; d < cm->depth(); ++d) {
118  cm->to_image(imgb, d);
119  imgd->show(imgb);
120  imgd->loop_until_quit();
121  }
122  }
123 
124  delete cm;
125 
126  unsigned int depth = 4, width = 128, height = 128;
127  printf("Trying colormap with low resolution, choosing %dx%dx%d\n", depth, width, height);
128  cm = new YuvColormap(depth, width, height);
129  printf("YuvColormap dimensions: %dx%dx%d\n", cm->depth(), cm->width(), cm->height());
130  ColormapFile cmfr(depth, width, height);
131  delete cm;
132  cmfr.write(filename);
133  cmfr.clear();
134  cmfr.read(filename);
135  cm = dynamic_cast<YuvColormap *>(cmfr.get_colormap());
136  printf("Read back colormap dimensions %dx%dx%d\n",
137  cm->depth(), cm->width(), cm->height());
138  delete cm;
139 
140  //delete imgd;
141  //free(imgb);
142  return 0;
143 }
144 
145 /// @endcond
Simple image display.
Definition: image_display.h:38
virtual unsigned int deepness() const
Get deepness of colormap.
Definition: yuvcm.cpp:343
void add_colormap(Colormap *colormap)
Add colormap.
Definition: cmfile.cpp:98
virtual void to_image(unsigned char *yuv422_planar_buffer, unsigned int level=0)
Create image from LUT.
Definition: colormap.cpp:129
virtual unsigned int width() const
Get width of colormap.
Definition: yuvcm.cpp:322
Fawkes library namespace.
Colormap interface.
Definition: colormap.h:38
Colormap * get_colormap()
Get a freshly generated colormap based on current file content.
Definition: cmfile.cpp:169
YUV Colormap.
Definition: yuvcm.h:39
virtual void write(const char *file_name)
Write file.
Definition: fvfile.cpp:262
virtual void read(const char *file_name)
Read file.
Definition: fvfile.cpp:308
Colormap file.
Definition: cmfile.h:55
Base class for exceptions in Fawkes.
Definition: exception.h:36
ColormapBlockVector * colormap_blocks()
Get colormap blocks.
Definition: cmfile.cpp:134
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:182
Vector of colormap blocks.
Definition: cmfile.h:61
void loop_until_quit()
Process SDL events until quit.
virtual void clear()
Clear internal storage.
Definition: cmfile.cpp:238
virtual unsigned int depth() const
Get depth of colormap.
Definition: yuvcm.cpp:336
virtual unsigned int height() const
Get height of colormap.
Definition: yuvcm.cpp:329
void show(colorspace_t colorspace, unsigned char *buffer)
Show image from given colorspace.