image_dither_unittest.cpp

Go to the documentation of this file.
00001 /*
00002  * File Name: image_dither_unittest.cpp
00003  */
00004 
00005 /*
00006  * This file is part of uds-plugin-images.
00007  *
00008  * uds-plugin-images is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation, either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * uds-plugin-images is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00020  */
00021 
00022 /**
00023  * Copyright (C) 2008 iRex Technologies B.V.
00024  * All rights reserved.
00025  */
00026 
00027 #include <cassert>
00028 #include <iostream>
00029 #include "image_dither_unittest.h"
00030 
00031 using namespace std;
00032 using namespace images;
00033 
00034 TestImageDither::TestImageDither (void)
00035 {
00036     g_type_init ();
00037 }
00038 
00039 TestImageDither::~TestImageDither (void)
00040 {
00041 }
00042 
00043 void TestImageDither::set_image(const std::string & filepath)
00044 {
00045     img_path = filepath;
00046 }
00047 
00048 void TestImageDither::test (void)
00049 {
00050     // Load the image.
00051     GdkPixbuf * pixbuf = gdk_pixbuf_new_from_file (img_path.c_str (), 0);
00052 
00053     // Set all fields of attrs_src.
00054     BitmapAttributes attrs_src, attrs_dst, attrs_tmp;
00055 
00056     attrs_src.width = gdk_pixbuf_get_width (pixbuf);
00057     attrs_src.height = gdk_pixbuf_get_height (pixbuf);
00058     attrs_src.rowstride = gdk_pixbuf_get_rowstride (pixbuf);
00059     attrs_src.data = gdk_pixbuf_get_pixels (pixbuf);
00060     attrs_src.bytes_per_pixel = gdk_pixbuf_get_n_channels (pixbuf);
00061 
00062     // Convert the image data to be in 8bits.
00063     ImageDither dither;
00064     dither.dither_to_8bits (&attrs_src, &attrs_dst);
00065 
00066     // Convert the image data to be in 24bits for saving.
00067     convert_8bits_to_24bits (&attrs_dst, &attrs_tmp);
00068 
00069     // Save to image file to verify.
00070     save_24bits_to_file (&attrs_tmp);
00071 
00072     // Free the memory.
00073     g_object_unref (pixbuf);
00074     delete [] attrs_dst.data;
00075     // delete [] attrs_tmp.data;
00076 }
00077 
00078 void TestImageDither::convert_8bits_to_24bits (
00079                           const BitmapAttributes * attrs_src,
00080                           BitmapAttributes * attrs_dst)
00081 {
00082     assert (attrs_src);
00083     assert (attrs_dst);
00084     assert (attrs_src->data);
00085     assert (attrs_src->bytes_per_pixel == 1);
00086     assert (attrs_src->rowstride >=
00087           (attrs_src->width * attrs_src->bytes_per_pixel));
00088 
00089     attrs_dst->width = attrs_src->width;
00090     attrs_dst->height = attrs_src->height;
00091     attrs_dst->rowstride = images::ImageDither::get_rowstride (
00092                                          attrs_dst->width, 3, 4);
00093     attrs_dst->bytes_per_pixel = 3;
00094     attrs_dst->data = _convert_8bits_to_24bits (attrs_src->data,
00095                           attrs_src->width,
00096                           attrs_src->height);
00097 }
00098 
00099 unsigned char *
00100 TestImageDither::_convert_8bits_to_24bits (const unsigned char *data,
00101                                            int w, int h)
00102 {
00103     int rowstride_src = images::ImageDither::get_rowstride (w, 1, 4);
00104 
00105     // Get the memory.
00106     int rowstride_dst = images::ImageDither::get_rowstride (w, 3, 4);
00107     unsigned char * dst = new unsigned char[rowstride_dst * h];
00108 
00109     // Walk throuth the whole pixels buffer.
00110     unsigned char *p_src, *p_dst;
00111     for (int y = 0; y < h; y++)
00112     {
00113         p_src = (unsigned char *) (data + y * rowstride_src);
00114         p_dst = dst + y * rowstride_dst;
00115 
00116         for (int x = 0; x < w; x++)
00117         {
00118             p_dst[0] = *p_src;
00119             p_dst[1] = *p_src;
00120             p_dst[2] = *p_src;
00121 
00122             p_src++;
00123             p_dst += 3;
00124         }
00125     }
00126 
00127     return dst;
00128 }
00129 
00130 void TestImageDither::save_24bits_to_file (const BitmapAttributes * attrs)
00131 {
00132     std::string filepath = img_path;
00133     filepath.append (".new");
00134     
00135     GdkPixbuf * pixbuf = gdk_pixbuf_new_from_data (attrs->data,
00136                      GDK_COLORSPACE_RGB,
00137                      false,
00138                      8,
00139                      attrs->width,
00140                      attrs->height,
00141                      attrs->rowstride, 0, 0);
00142 
00143     gdk_pixbuf_save (pixbuf, filepath.c_str(), "jpeg", NULL, NULL);
00144 
00145     g_object_unref (pixbuf);
00146 }
00147 
00148 
00149 int main ()
00150 {
00151    const char* imgpath_list[] = {
00152    "/data/wa/uds/trunk/sample/Rome1.JPG",
00153    "/data/wa/uds/trunk/sample/Vatican2.JPG",
00154    "/data/wa/uds/trunk/sample/Rome/Rome1.JPG",
00155    "/data/wa/uds/trunk/sample/Rome/Rome2.JPG",
00156    "/data/wa/uds/trunk/sample/alpha/Btn_200_27_transparent_blackborder.png",
00157    "/data/wa/uds/trunk/sample/alpha/Btn_310_27_black.png",
00158    "/data/wa/uds/trunk/sample/alpha/Btn_200_27_black.png",
00159    "/data/wa/uds/trunk/sample/alpha/Btn_310_27_transparent_blackborder.png",
00160    "/data/wa/uds/trunk/sample/alpha/Btn_150_27_transparent_blackborder.png",
00161    "/data/wa/uds/trunk/sample/alpha/Btn_150_27_black.png",
00162    "/data/wa/uds/trunk/sample/alpha/Btn_640_44_black.png",
00163    "/data/wa/uds/trunk/sample/alpha/Btn_640_44_grey.png",
00164    "/data/wa/uds/trunk/sample/alpha/Btn_130_27_transparent_blackborder.png",
00165    "/data/wa/uds/trunk/sample/alpha/Btn_130_27_black.png",
00166    "/data/wa/uds/trunk/sample/Pisa1.JPG",
00167    "/data/wa/uds/trunk/sample/openoffice/maximize.bmp",
00168    "/data/wa/uds/trunk/sample/openoffice/report.bmp",
00169    "/data/wa/uds/trunk/sample/openoffice/cancel_up.bmp",
00170    "/data/wa/uds/trunk/sample/openoffice/euro_3.bmp",
00171    "/data/wa/uds/trunk/sample/openoffice/up.bmp",
00172    "/data/wa/uds/trunk/sample/openoffice/end.bmp",
00173    "/data/wa/uds/trunk/sample/openoffice/okay_up.bmp",
00174    "/data/wa/uds/trunk/sample/openoffice/euro_2.bmp",
00175    "/data/wa/uds/trunk/sample/openoffice/down.bmp",
00176    "/data/wa/uds/trunk/sample/openoffice/Import_4.bmp",
00177    "/data/wa/uds/trunk/sample/openoffice/Import_1.bmp",
00178    "/data/wa/uds/trunk/sample/openoffice/XML-Import_2-1.bmp",
00179    "/data/wa/uds/trunk/sample/openoffice/MS-Import_2-1.bmp",
00180    "/data/wa/uds/trunk/sample/openoffice/minimize.bmp",
00181    "/data/wa/uds/trunk/sample/openoffice/MS-Import_2-3.bmp",
00182    "/data/wa/uds/trunk/sample/openoffice/cancel_down.bmp",
00183    "/data/wa/uds/trunk/sample/openoffice/XML-Import_2-3.bmp",
00184    "/data/wa/uds/trunk/sample/openoffice/euro_1.bmp",
00185    "/data/wa/uds/trunk/sample/openoffice/XML-Import_2-4.bmp",
00186    "/data/wa/uds/trunk/sample/openoffice/okay_down.bmp",
00187    "/data/wa/uds/trunk/sample/openoffice/XML-Import_2-2.bmp",
00188    "/data/wa/uds/trunk/sample/openoffice/MS-Import_2-2.bmp",
00189    "/data/wa/uds/trunk/sample/openoffice/Import_3.bmp",
00190    "/data/wa/uds/trunk/sample/Florence3.JPG",
00191    "/data/wa/uds/trunk/sample/Florence/Florence1.JPG",
00192    "/data/wa/uds/trunk/sample/Florence/Florence3.JPG",
00193    "/data/wa/uds/trunk/sample/Florence/Florence4.JPG",
00194    "/data/wa/uds/trunk/sample/Florence/Florence2.JPG",
00195    "/data/wa/uds/trunk/sample/Venice3.JPG",
00196    "/data/wa/uds/trunk/sample/Screenshot.png",
00197    "/data/wa/uds/trunk/sample/Venice1.JPG",
00198    "/data/wa/uds/trunk/sample/Venice4.JPG",
00199    "/data/wa/uds/trunk/sample/OURS/OURS2.JPG",
00200    "/data/wa/uds/trunk/sample/OURS/OURS3.JPG",
00201    "/data/wa/uds/trunk/sample/OURS/OURS4.JPG",
00202    "/data/wa/uds/trunk/sample/OURS/OURS5.JPG",
00203    "/data/wa/uds/trunk/sample/OURS/OURS1.JPG",
00204    "/data/wa/uds/trunk/sample/Florence4.JPG",
00205    "/data/wa/uds/trunk/sample/Venice6.JPG",
00206    "/data/wa/uds/trunk/sample/Florence2.JPG",
00207    "/data/wa/uds/trunk/sample/Vatican/Vatican2.JPG",
00208    "/data/wa/uds/trunk/sample/Vatican/Vatican1.JPG",
00209    "/data/wa/uds/trunk/sample/Venice2.JPG",
00210    "/data/wa/uds/trunk/sample/Rome2.JPG",
00211    "/data/wa/uds/trunk/sample/Venice5.JPG",
00212    "/data/wa/uds/trunk/sample/Florence.jpg",
00213    "/data/wa/uds/trunk/sample/消化系统示意图.JPG",
00214    "/data/wa/uds/trunk/sample/Venice/Venice3.JPG",
00215    "/data/wa/uds/trunk/sample/Venice/Venice1.JPG",
00216    "/data/wa/uds/trunk/sample/Venice/Venice4.JPG",
00217    "/data/wa/uds/trunk/sample/Venice/Venice6.JPG",
00218    "/data/wa/uds/trunk/sample/Venice/Venice2.JPG",
00219    "/data/wa/uds/trunk/sample/Venice/Venice5.JPG",
00220    "/data/wa/uds/trunk/sample/Vatican1.JPG",
00221    "/data/wa/uds/trunk/sample/PISA/Pisa1.JPG",
00222    "/data/wa/uds/trunk/sample/gif/flowers.gif",
00223    "/data/wa/uds/trunk/sample/gif/sky.gif",
00224    "/data/wa/uds/trunk/sample/gif/flower.gif",
00225    "/data/wa/uds/trunk/sample/gif/apples.gif",
00226    "/data/wa/uds/trunk/sample/gif/bigapple.gif"
00227    };
00228 
00229     TestImageDither test;
00230     std::string filepath;
00231 
00232     int n_images = sizeof(imgpath_list) / sizeof(imgpath_list[0]);
00233     for (int i = 0; i < n_images; i++)
00234     {
00235         filepath = imgpath_list[i];
00236         test.set_image(filepath);
00237         test.test();
00238     }
00239 
00240     return 0;
00241 }
00242 
Generated by  doxygen 1.6.2-20100208