qa_colormap.cpp

00001
00002 /***************************************************************************
00003  *  qa_colormap.cpp - QA for colormap
00004  *
00005  *  Created: Tue Apr 01 10:04:27 2008
00006  *  Copyright  2007-2008  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023 
00024 /// @cond QA
00025 
00026 #include <fvutils/colormap/yuvcm.h>
00027 #include <fvutils/colormap/cmfile.h>
00028 #include <fvutils/color/colorspaces.h>
00029
00030 #include <fvwidgets/image_display.h>
00031 #include <core/exception.h>
00032
00033 #include <cstdlib>
00034 #include <cstdio>
00035
00036 using namespace fawkes;
00037
00038 #define BIGDEPTH 8
00039 
00040 int
00041 main(int argc, char **argv)
00042 {
00043
00044   const char *filename = "qatest.colormap";
00045   if ( argc > 1 ) {
00046     filename = argv[1];
00047   }
00048
00049   printf("Creating simple one-plane colormap\n");
00050   YuvColormap *cm = new YuvColormap();
00051
00052   for (unsigned int u = 100; u < 150; ++u) {
00053     for (unsigned int v = 100; v < 150; ++v) {
00054       cm->set(128, u, v, C_ORANGE);
00055     }
00056   }
00057
00058   unsigned char *imgb = malloc_buffer(YUV422_PLANAR, cm->width() * 2, cm->height() * 2);
00059   cm->to_image(imgb);
00060
00061   ImageDisplay *imgd = new ImageDisplay(cm->width() * 2, cm->height() * 2);
00062   imgd->show(imgb);
00063
00064   imgd->loop_until_quit();
00065
00066   delete cm;
00067
00068   printf("Trying to create colormap with illegal depth, should throw an exception..");
00069   try {
00070     cm = new YuvColormap(3);
00071     printf(" Test failed, colormap was created\n");
00072     delete cm;
00073   } catch (Exception &e) {
00074     printf(" Test succeeded, caught exception\n");
00075   }
00076
00077   printf("Trying colormap with depth of %u\n", BIGDEPTH);
00078   cm = new YuvColormap(BIGDEPTH);
00079
00080   for (unsigned int d = 0; d < cm->depth(); ++d) {
00081     unsigned int y = 256 / cm->depth() * d;
00082     printf("d=%u   y=%u   u=[%u,%u]  v=[%u,%u]\n", d, y,
00083            cm->depth() * d, cm->depth() * (d+1), cm->depth() * d, cm->depth() * (d+1));
00084
00085     for (unsigned int u = cm->deepness() / cm->depth() * d; u < cm->deepness() / cm->depth() * (d+1); ++u) {
00086       for (unsigned int v = cm->deepness() / cm->depth() * d; v < cm->deepness() / cm->depth() * (d+1); ++v) {
00087         cm->set(y, u, v, C_ORANGE);
00088       }
00089     }
00090
00091     cm->to_image(imgb, d);
00092     imgd->show(imgb);
00093     imgd->loop_until_quit();
00094   }
00095
00096   printf("Saving colormap to a file\n");
00097   ColormapFile cmf;
00098   cmf.add_colormap(cm);
00099   cmf.write(filename);
00100
00101   ColormapFile::ColormapBlockVector *blocks = cmf.colormap_blocks();
00102   printf("Written, created %zu blocks\n", blocks->size());
00103   delete blocks;
00104   delete cm;
00105
00106   cmf.clear();
00107
00108   printf("Reading colormap from file\n");
00109   cmf.read(filename);
00110
00111   Colormap *tcm = cmf.get_colormap();
00112   if ( (cm = dynamic_cast<YuvColormap *>(tcm)) == 0 ) {
00113     printf("Error, did not get valid yuv colormap\n");
00114   } else {
00115     printf("Showing all %u colormap levels\n", cm->depth());
00116     for (unsigned int d = 0; d < cm->depth(); ++d) {
00117       cm->to_image(imgb, d);
00118       imgd->show(imgb);
00119       imgd->loop_until_quit();
00120     }
00121   }
00122
00123   delete cm;
00124
00125   unsigned int depth = 4, width = 128, height = 128;
00126   printf("Trying colormap with low resolution, choosing %dx%dx%d\n", depth, width, height);
00127   cm = new YuvColormap(depth, width, height);
00128   printf("YuvColormap dimensions: %dx%dx%d\n", cm->depth(), cm->width(), cm->height());
00129   ColormapFile cmfr(depth, width, height);
00130   delete cm;
00131   cmfr.write(filename);
00132   cmfr.clear();
00133   cmfr.read(filename);
00134   cm = dynamic_cast<YuvColormap *>(cmfr.get_colormap());
00135   printf("Read back colormap dimensions %dx%dx%d\n",
00136          cm->depth(), cm->width(), cm->height());
00137   delete cm;
00138
00139   //delete imgd;
00140   //free(imgb);
00141   return 0;
00142 }
00143 
00144 /// @endcond