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 
35 using namespace fawkes;
36 using namespace firevision;
37 
38 #define BIGDEPTH 8
39 
40 int
41 main(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
firevision::ColormapFile::clear
virtual void clear()
Clear internal storage.
Definition: cmfile.cpp:234
firevision::YuvColormap::depth
virtual unsigned int depth() const
Definition: yuvcm.cpp:335
firevision::ImageDisplay
Definition: image_display.h:34
firevision::YuvColormap
Definition: yuvcm.h:39
firevision::FireVisionDataFile::read
virtual void read(const char *file_name)
Read file.
Definition: fvfile.cpp:289
firevision::ColormapFile::get_colormap
Colormap * get_colormap()
Get a freshly generated colormap based on current file content.
Definition: cmfile.cpp:168
firevision::YuvColormap::width
virtual unsigned int width() const
Definition: yuvcm.cpp:323
firevision::Colormap::to_image
virtual void to_image(unsigned char *yuv422_planar_buffer, unsigned int level=0)
Create image from LUT.
Definition: colormap.cpp:130
fawkes
firevision::FireVisionDataFile::write
virtual void write(const char *file_name)
Write file.
Definition: fvfile.cpp:242
firevision::ImageDisplay::loop_until_quit
void loop_until_quit()
Process SDL events until quit.
Definition: image_display.cpp:132
firevision::ColormapFile::ColormapBlockVector
Definition: cmfile.h:64
firevision::ColormapFile::colormap_blocks
ColormapBlockVector * colormap_blocks()
Get colormap blocks.
Definition: cmfile.cpp:135
firevision::ColormapFile::add_colormap
void add_colormap(Colormap *colormap)
Add colormap.
Definition: cmfile.cpp:93
firevision::YuvColormap::height
virtual unsigned int height() const
Definition: yuvcm.cpp:329
firevision::ImageDisplay::show
void show(colorspace_t colorspace, unsigned char *buffer)
Show image from given colorspace.
Definition: image_display.cpp:92
firevision::Colormap
Definition: colormap.h:40
firevision::ColormapFile
Definition: cmfile.h:58
firevision::YuvColormap::set
virtual void set(unsigned int y, unsigned int u, unsigned int v, color_t c)
Definition: yuvcm.cpp:193
firevision::YuvColormap::deepness
virtual unsigned int deepness() const
Definition: yuvcm.cpp:341
fawkes::Exception
Definition: exception.h:39