examples/test_wavelet/test_wavelet.c

Go to the documentation of this file.
00001 /*
00002    libit - Library for basic source and channel coding functions
00003    Copyright (C) 2005-2005 Vivien Chappelier, Herve Jegou
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public
00016    License along with this library; if not, write to the Free
00017    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 */
00019 
00020 /** @file test_wavelet.c Test program for wavelets */
00021 
00022 #include <it/io.h>
00023 #include <it/mat.h>
00024 #include <it/transform.h>
00025 #include <it/wavelet.h>
00026 #include <it/transform2D.h>
00027 #include <it/wavelet2D.h>
00028 #include <it/separable2D.h>
00029 
00030 /* scale the wavelet for display */
00031 it_function (rescale_image)
00032 {
00033   x += 128;
00034   if (x < 0)
00035     x = 0;
00036   if (x > 255)
00037     x = 255;
00038 
00039   return (x);
00040 }
00041 
00042 /* rescale the wavelet coefficients for sound I/O */
00043 it_function (rescale_sound)
00044 {
00045   x *= 1. / 32;
00046 
00047   return (x);
00048 }
00049 
00050 int main ()
00051 {
00052   it_wavelet_t *wavelet;
00053   it_wavelet2D_t *wavelet2D;
00054   it_separable2D_t *separable;
00055   const char *image_in = "../data/test.pgm";
00056   const char *image_out = "out.pgm";
00057   const char *sound_in = "../data/test.wav";
00058   const char *sound_out = "out.wav";
00059   char pnm_type, comments[1000];
00060   int  width, height, maxval;
00061   int  levels;
00062   int  length, channels, srate, depth;
00063   mat  m, mt;
00064   vec  v, vt;
00065 
00066   levels = 4;
00067 
00068   /* Read the sound */
00069   if (!wav_info (sound_in, &channels, &srate, &depth, &length)) {
00070     fprintf (stderr, "unable to open file %s\n", sound_in);
00071     return (1);
00072   }
00073   printf
00074     ("file name = %s\nchannels = %d\nsampling rate = %d\ndepth = %d\nlength = %d samples/channel\n",
00075      sound_in, channels, srate, depth, length);
00076 
00077   m = mat_wav_read (sound_in);
00078   v = m[0];     /* consider only the first channel */
00079 
00080   /* Transform the sound */
00081   vt = it_dwt (v, it_wavelet_lifting_53, levels);
00082   vec_delete (v);
00083 
00084   /* Write down the coefficients */
00085   v = vec_new_eval (vt, rescale_sound, NULL);
00086   m[0] = v;
00087   mat_wav_write ("wavelet.wav", m, srate, depth);
00088   vec_delete (v);
00089 
00090   /* Reconstruct the sound */
00091   v = it_idwt (vt, it_wavelet_lifting_53, levels);
00092   vec_delete (vt);
00093 
00094   m[0] = v;
00095   mat_wav_write (sound_out, m, srate, depth);
00096   mat_delete (m);
00097 
00098   /* Test the separable transform */
00099   /* Warning: note that for the wavelet with more than 1 level of   */
00100   /* decomposition, the transform obtained by applying the wavelet  */
00101   /* on the rows, then on the columns of the image is *not* what is */
00102   /* usually called the separable wavelet. See this example:        */
00103   /* separable(wavelet1D)                   wavelet2D               */
00104   /*                                                                */
00105   /*   +------+------+------+               +------+------+------+  */
00106   /*   | L1L1 | L1H1 | L1H0 |               | L1L1 | LH1  |      |  */
00107   /*   +------+------+------+               +------+------+  LH0 |  */
00108   /*   | H1L1 | H1H1 | H1H0 |               | HL1  | HH1  |      |  */
00109   /*   +------+------+------+               +------+------+------+  */
00110   /*   |      |      |      |               |             |      |  */
00111   /*   | H0L1 | H0H1 | H0H0 |               |    HL0      |  HH0 |  */
00112   /*   +------+------+------+               +-------------+------+  */
00113 
00114   m = mat_pgm_read (image_in);
00115   printf ("PGM %dx%d\n", mat_height (m), mat_width (m));
00116 
00117   levels = 1;
00118 
00119   /* create a 1D wavelet object */
00120   wavelet = it_wavelet_new (it_wavelet_lifting_97, levels);
00121   /* create a separable transform out of the wavelet */
00122   separable = it_separable2D_new (wavelet);
00123 
00124   /* Transform the image */
00125   mt = (mat) it_transform2D (separable, m);
00126   mat_delete (m);
00127 
00128   /* Write down the coefficients (shifted & saturated) */
00129   m = mat_new_eval (mt, rescale_image, NULL);
00130   mat_pgm_write ("wavelet_separable.pgm", m);
00131   mat_delete (m);
00132 
00133   /* Reconstruct the image */
00134   m = (mat) it_itransform2D (separable, mt);
00135   mat_delete (mt);
00136 
00137   it_delete (separable);
00138   it_delete (wavelet);
00139 
00140   mat_pgm_write ("separable.pgm", m);
00141   mat_delete (m);
00142 
00143   /* Read the image */
00144   pnm_info (image_in, &pnm_type, &width, &height, &maxval, comments, 1000);
00145   printf ("file name = %s\npnm type = %c\n%dx%d -> maxval=%d\ncomments=%s\n",
00146     image_in, pnm_type, width, height, maxval, "");
00147 
00148   m = mat_pgm_read (image_in);
00149 
00150   printf
00151     ("height(m) = %d\tmaxheight(m) = %d\nwidth(m) = %d\tmaxwidth(m) = %d\n",
00152      mat_height (m), mat_height_max (m), mat_width (m), mat_width_max (m));
00153 
00154   levels = 5;
00155 
00156   /* create a 2D wavelet object */
00157   wavelet2D = it_wavelet2D_new (it_wavelet_lifting_97, levels);
00158 
00159   /* Transform the image */
00160   mt = it_wavelet2D_transform (wavelet2D, m);
00161   mat_delete (m);
00162 
00163   /* Write down the coefficients (shifted & saturated) */
00164   m = mat_new_eval (mt, rescale_image, NULL);
00165   mat_pgm_write ("wavelet.pgm", m);
00166   mat_delete (m);
00167 
00168   /* Reconstruct the image */
00169   m = it_wavelet2D_itransform (wavelet2D, mt);
00170   mat_delete (mt);
00171 
00172   it_delete (wavelet2D);
00173 
00174   mat_pgm_write (image_out, m);
00175   mat_delete (m);
00176 
00177   return 0;
00178 }

Hosted by
Copyright (C) 2005-2006 Hervé Jégou
Vivien Chappelier
Francois Cayre
libit logo courtesy of Jonathan Delhumeau