PTdecode/CImg-1.3.0/examples/use_jpeg_buffer.cpp

changeset 5
1204ebf9340d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/PTdecode/CImg-1.3.0/examples/use_jpeg_buffer.cpp	Mon Aug 03 14:09:20 2009 +0100
     1.3 @@ -0,0 +1,115 @@
     1.4 +/*
     1.5 + #
     1.6 + #  File        : use_jpeg_buffer.cpp
     1.7 + #                ( C++ source file )
     1.8 + #
     1.9 + #  Description : Example of use for the CImg plugin 'plugins/jpeg_buffer.h'.
    1.10 + #                This file is a part of the CImg Library project.
    1.11 + #                ( http://cimg.sourceforge.net )
    1.12 + #
    1.13 + #  Copyright   : Paolo Prete
    1.14 + #                ( p4olo_prete(at)yahoo.it )
    1.15 + #
    1.16 + #  License     : CeCILL v2.0
    1.17 + #                ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html )
    1.18 + #
    1.19 + #  This software is governed by the CeCILL  license under French law and
    1.20 + #  abiding by the rules of distribution of free software.  You can  use,
    1.21 + #  modify and/ or redistribute the software under the terms of the CeCILL
    1.22 + #  license as circulated by CEA, CNRS and INRIA at the following URL
    1.23 + #  "http://www.cecill.info".
    1.24 + #
    1.25 + #  As a counterpart to the access to the source code and  rights to copy,
    1.26 + #  modify and redistribute granted by the license, users are provided only
    1.27 + #  with a limited warranty  and the software's author,  the holder of the
    1.28 + #  economic rights,  and the successive licensors  have only  limited
    1.29 + #  liability.
    1.30 + #
    1.31 + #  In this respect, the user's attention is drawn to the risks associated
    1.32 + #  with loading,  using,  modifying and/or developing or reproducing the
    1.33 + #  software by the user in light of its specific status of free software,
    1.34 + #  that may mean  that it is complicated to manipulate,  and  that  also
    1.35 + #  therefore means  that it is reserved for developers  and  experienced
    1.36 + #  professionals having in-depth computer knowledge. Users are therefore
    1.37 + #  encouraged to load and test the software's suitability as regards their
    1.38 + #  requirements in conditions enabling the security of their systems and/or
    1.39 + #  data to be ensured and,  more generally, to use and operate it in the
    1.40 + #  same conditions as regards security.
    1.41 + #
    1.42 + #  The fact that you are presently reading this means that you have had
    1.43 + #  knowledge of the CeCILL license and that you accept its terms.
    1.44 + #
    1.45 +*/
    1.46 +
    1.47 +// These includes are necessary to get the plug-in compile !
    1.48 +#include <cstdio>
    1.49 +#include <jpeglib.h>
    1.50 +#include <jerror.h>
    1.51 +
    1.52 +// Define plugin and include the CImg Library.
    1.53 +#define cimg_plugin "plugins/jpeg_buffer.h"
    1.54 +#include "CImg.h"
    1.55 +using namespace cimg_library;
    1.56 +
    1.57 +// The lines below are necessary when using a non-standard compiler as visualcpp6.
    1.58 +#ifdef cimg_use_visualcpp6
    1.59 +#define std
    1.60 +#endif
    1.61 +
    1.62 +// Main procedure
    1.63 +//----------------
    1.64 +int main() {
    1.65 +
    1.66 +  // Create a jpeg memory buffer from the content of a jpeg file.
    1.67 +  // (this is for testing purposes only)
    1.68 +  const char *filename_input = "foo.jpg";
    1.69 +  std::fprintf(stderr," - Reading file '%s'\n",filename_input);
    1.70 +  std::FILE *file_input = std::fopen(filename_input,"rb");
    1.71 +  if (!file_input) { std::fprintf(stderr,"Input JPEG file not found !"); std::exit(0); }
    1.72 +
    1.73 +  std::fprintf(stderr," - Construct input JPEG-coded buffer\n");
    1.74 +  unsigned buf_size = 500000; // Put the file size here !
    1.75 +  JOCTET *buffer_input = new JOCTET[buf_size];
    1.76 +  std::fread(buffer_input,sizeof(JOCTET),buf_size,file_input);
    1.77 +  std::fclose(file_input);
    1.78 +  // -> 'buffer_input' is now a valid jpeg-coded memory buffer.
    1.79 +
    1.80 +  // Create a CImg instance from the jpeg-coded buffer using the plug-in function.
    1.81 +  std::fprintf(stderr," - Create CImg instance from JPEG-coded buffer\n");
    1.82 +  CImg<unsigned char> img;
    1.83 +  img.load_jpeg_buffer(buffer_input, buf_size);
    1.84 +  delete[] buffer_input;
    1.85 +
    1.86 +  // Do you image processing stuff here ....
    1.87 +  // Here, we just mirror the image and write "hello".
    1.88 +  std::fprintf(stderr," - Do simple processing\n");
    1.89 +  const unsigned char purple[] = { 255, 0, 0 };
    1.90 +  const unsigned char black[] = { 0, 0, 0 };
    1.91 +  img.mirror('y').draw_text(0,0,"   Hello!   ",purple,black,1,57);
    1.92 +
    1.93 +  // Display image to see if everything's fine.
    1.94 +  img.display("Using 'jpeg_buffer.h' plugin");
    1.95 +
    1.96 +  // Define a new JOCTET array where the processed image has to be saved
    1.97 +  // (we don't know its dimension before compressing it, therefore we have to allocate enough memory )
    1.98 +  std::fprintf(stderr," - Construct output JPEG-coded buffer\n");
    1.99 +  JOCTET *buffer_output = new JOCTET[2*buf_size];
   1.100 +
   1.101 +  // Save processed image into this JOCTET buffer, compressed as jpeg.
   1.102 +  // This is done again by using the plug-in function.
   1.103 +  img.save_jpeg_buffer(buffer_output,buf_size,60);
   1.104 +  // Note that here, the variable 'buf_size' contains the length of the
   1.105 +  // data which have been written in the given output buffer.
   1.106 +
   1.107 +  // Copy the content of the above array into a new file
   1.108 +  // (it should give you a valid JPEG file then !)
   1.109 +  const char *filename_output = "foo_output.jpg";
   1.110 +  std::fprintf(stderr," - Save output file '%s'\n",filename_output);
   1.111 +  std::FILE* file_output = std::fopen(filename_output,"wb");
   1.112 +  std::fwrite(buffer_output, sizeof(JOCTET), buf_size, file_output);
   1.113 +  std::fclose(file_output);
   1.114 +  delete[] buffer_output;
   1.115 +
   1.116 +  std::fprintf(stderr," - All done !\n");
   1.117 +  return 0;
   1.118 +}