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