Guest User

Untitled

a guest
Apr 2nd, 2015
278
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  #
  3.  #  File        : gmic_use_lib.cpp
  4.  #                ( C++ source file )
  5.  #
  6.  #  Description : Show how to call the G'MIC interpreter from a C++ source code.
  7.  #
  8.  #  Copyright   : David Tschumperle
  9.  #                ( http://tschumperle.users.greyc.fr/ )
  10.  #
  11.  #  License     : CeCILL v2.0
  12.  #                ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html )
  13.  #
  14.  #  This software is governed by the CeCILL  license under French law and
  15.  #  abiding by the rules of distribution of free software.  You can  use,
  16.  #  modify and/ or redistribute the software under the terms of the CeCILL
  17.  #  license as circulated by CEA, CNRS and INRIA at the following URL
  18.  #  "http://www.cecill.info".
  19.  #
  20.  #  As a counterpart to the access to the source code and  rights to copy,
  21.  #  modify and redistribute granted by the license, users are provided only
  22.  #  with a limited warranty  and the software's author,  the holder of the
  23.  #  economic rights,  and the successive licensors  have only  limited
  24.  #  liability.
  25.  #
  26.  #  In this respect, the user's attention is drawn to the risks associated
  27.  #  with loading,  using,  modifying and/or developing or reproducing the
  28.  #  software by the user in light of its specific status of free software,
  29.  #  that may mean  that it is complicated to manipulate,  and  that  also
  30.  #  therefore means  that it is reserved for developers  and  experienced
  31.  #  professionals having in-depth computer knowledge. Users are therefore
  32.  #  encouraged to load and test the software's suitability as regards their
  33.  #  requirements in conditions enabling the security of their systems and/or
  34.  #  data to be ensured and, more generally, to use and operate it in the
  35.  #  same conditions as regards security.
  36.  #
  37.  #  The fact that you are presently reading this means that you have had
  38.  #  knowledge of the CeCILL license and that you accept its terms.
  39.  #
  40. */
  41.  
  42. /*
  43.     Note : To compile this example, using g++, use :
  44.  
  45.     g++ -o gmic_use_lib gmic_use_lib.cpp -lgmic -lfftw3
  46. */
  47.  
  48. /* Uncomment the two lines below if you want to use the CImg library along with the G'MIC library.
  49. */
  50. //#include "CImg.h"
  51. //using namespace cimg_library;
  52.  
  53. #include <cmath>
  54. #include "gmic.h"
  55.  
  56. int main() {
  57.  
  58.  // First step : Create a list of input images.
  59.  //--------------------------------------------
  60.  std::fprintf(stderr,"\n- 1st step : Create input list of images.\n");
  61.  
  62.  gmic_list<float> images;                            // List of images, will contain all images pixel data.
  63.  gmic_list<char> images_names;                       // List of images names. Can be left empty if no names are associated to images.
  64.  images.assign(5);                                   // Assign list to contain 5 images.
  65.  for (unsigned int i = 0; i<images._width; ++i) {
  66.    gmic_image<float>& img = images._data[i];
  67.    img.assign(256,256,1,3);                          // Assign i-th image of the list to be of size 256x256x1x3 (2d color image).
  68.  
  69.    std::fprintf(stderr,"    Input image %u =  %ux%ux%ux%u, buffer : %p\n",i,
  70.                 images._data[i]._width,
  71.                 images._data[i]._height,
  72.                 images._data[i]._depth,
  73.                 images._data[i]._spectrum,
  74.                 images._data[i]._data);
  75.  
  76.    // Fill each image buffer with sinus values (with different frequencies).
  77.    float *ptr = img._data;
  78.    for (unsigned int c = 0; c<img._spectrum; ++c)
  79.      for (unsigned int y = 0; y<img._height; ++y)
  80.        for (unsigned int x = 0; x<img._width; ++x)
  81.          *(ptr++) = std::cos(x/(1.+i))*std::sin(y/(1.+i+c));
  82.  }
  83.  
  84.  // Second step : Call G'MIC API to process input images.
  85.   //------------------------------------------------------
  86.   std::fprintf(stderr,"\n- 2st step : Call G'MIC interpreter.\n");
  87.  
  88.   try {
  89.  
  90.     // Here you can call any G'MIC command you want !
  91.    // (here, create a deformed average of the input images, and save it as a BMP file).
  92.    gmic("-+ -n 0,255 -flower 8 -sharpen 100 -o foo1.bmp",images,images_names);
  93.  
  94.  } catch (gmic_exception &e) { // Catch exception, if an error occured in the interpreter.
  95.    std::fprintf(stderr,"\n- Error encountered when calling G'MIC : '%s'\n",e.what());
  96.    return 0;
  97.  }
  98.  
  99.  // Third step (alternative) : Call G'MIC API to process input images.
  100.  //---------------------------------------------------------------------
  101.  std::fprintf(stderr,"\n- 3rd step (alternative) : Call G'MIC interpreter from empty instance.\n");
  102.  
  103.  gmic gmic_instance;   // Construct first an empty 'gmic' instance.
  104.  
  105.  try {
  106.  
  107.    // Here, we use the already constructed 'gmic' instance. The same instance can be used
  108.    // several times.
  109.    gmic_instance.run("-blur 5 -sharpen 1000 -n 0,255 -o foo2.bmp",images,images_names);
  110.    std::fputc('\n',stderr);
  111.    gmic_instance.run("--resize 50%,50% -to_rgba[-1] -rotate[-1] 30 -drop_shadow[-1] 0,13 "
  112.                      "-blur_radial[0] 10% -blend alpha -o foo3.bmp",images,images_names);
  113.  
  114.  } catch (gmic_exception &e) { // Catch exception, if an error occured in the interpreter.
  115.    std::fprintf(stderr,"\n- Error encountered when calling G'MIC : '%s'\n",e.what());
  116.    return 0;
  117.  }
  118.  
  119.  // Fourth step : get back modified image data.
  120.  //---------------------------------------------
  121.  std::fprintf(stderr,"\n- 4th step : Returned %u output images.\n",images._width);
  122.  for (unsigned int i = 0; i<images._width; ++i) {
  123.    std::fprintf(stderr,"   Output image %u = %ux%ux%ux%u, buffer : %p\n",i,
  124.                 images._data[i]._width,
  125.                 images._data[i]._height,
  126.                 images._data[i]._depth,
  127.                 images._data[i]._spectrum,
  128.                 images._data[i]._data);
  129.  }
  130.  
  131.  // Fourth step : Free image resources.
  132.  //-------------------------------------
  133.  images.assign(0U);
  134.  
  135.  // That's it !
  136.  //-------------
  137.  std::fprintf(stderr,"\n- That's it !\n");
  138.  return 0;
  139. }
RAW Paste Data