Mon, 03 Aug 2009 23:41:04 +0100
added dep/*.d and obj/*.o to hgignore
1 /*
2 #
3 # File : use_jpeg_buffer.cpp
4 # ( C++ source file )
5 #
6 # Description : Example of use for the CImg plugin 'plugins/jpeg_buffer.h'.
7 # This file is a part of the CImg Library project.
8 # ( http://cimg.sourceforge.net )
9 #
10 # Copyright : Paolo Prete
11 # ( p4olo_prete(at)yahoo.it )
12 #
13 # License : CeCILL v2.0
14 # ( http://www.cecill.info/licences/Licence_CeCILL_V2-en.html )
15 #
16 # This software is governed by the CeCILL license under French law and
17 # abiding by the rules of distribution of free software. You can use,
18 # modify and/ or redistribute the software under the terms of the CeCILL
19 # license as circulated by CEA, CNRS and INRIA at the following URL
20 # "http://www.cecill.info".
21 #
22 # As a counterpart to the access to the source code and rights to copy,
23 # modify and redistribute granted by the license, users are provided only
24 # with a limited warranty and the software's author, the holder of the
25 # economic rights, and the successive licensors have only limited
26 # liability.
27 #
28 # In this respect, the user's attention is drawn to the risks associated
29 # with loading, using, modifying and/or developing or reproducing the
30 # software by the user in light of its specific status of free software,
31 # that may mean that it is complicated to manipulate, and that also
32 # therefore means that it is reserved for developers and experienced
33 # professionals having in-depth computer knowledge. Users are therefore
34 # encouraged to load and test the software's suitability as regards their
35 # requirements in conditions enabling the security of their systems and/or
36 # data to be ensured and, more generally, to use and operate it in the
37 # same conditions as regards security.
38 #
39 # The fact that you are presently reading this means that you have had
40 # knowledge of the CeCILL license and that you accept its terms.
41 #
42 */
44 // These includes are necessary to get the plug-in compile !
45 #include <cstdio>
46 #include <jpeglib.h>
47 #include <jerror.h>
49 // Define plugin and include the CImg Library.
50 #define cimg_plugin "plugins/jpeg_buffer.h"
51 #include "CImg.h"
52 using namespace cimg_library;
54 // The lines below are necessary when using a non-standard compiler as visualcpp6.
55 #ifdef cimg_use_visualcpp6
56 #define std
57 #endif
59 // Main procedure
60 //----------------
61 int main() {
63 // Create a jpeg memory buffer from the content of a jpeg file.
64 // (this is for testing purposes only)
65 const char *filename_input = "foo.jpg";
66 std::fprintf(stderr," - Reading file '%s'\n",filename_input);
67 std::FILE *file_input = std::fopen(filename_input,"rb");
68 if (!file_input) { std::fprintf(stderr,"Input JPEG file not found !"); std::exit(0); }
70 std::fprintf(stderr," - Construct input JPEG-coded buffer\n");
71 unsigned buf_size = 500000; // Put the file size here !
72 JOCTET *buffer_input = new JOCTET[buf_size];
73 std::fread(buffer_input,sizeof(JOCTET),buf_size,file_input);
74 std::fclose(file_input);
75 // -> 'buffer_input' is now a valid jpeg-coded memory buffer.
77 // Create a CImg instance from the jpeg-coded buffer using the plug-in function.
78 std::fprintf(stderr," - Create CImg instance from JPEG-coded buffer\n");
79 CImg<unsigned char> img;
80 img.load_jpeg_buffer(buffer_input, buf_size);
81 delete[] buffer_input;
83 // Do you image processing stuff here ....
84 // Here, we just mirror the image and write "hello".
85 std::fprintf(stderr," - Do simple processing\n");
86 const unsigned char purple[] = { 255, 0, 0 };
87 const unsigned char black[] = { 0, 0, 0 };
88 img.mirror('y').draw_text(0,0," Hello! ",purple,black,1,57);
90 // Display image to see if everything's fine.
91 img.display("Using 'jpeg_buffer.h' plugin");
93 // Define a new JOCTET array where the processed image has to be saved
94 // (we don't know its dimension before compressing it, therefore we have to allocate enough memory )
95 std::fprintf(stderr," - Construct output JPEG-coded buffer\n");
96 JOCTET *buffer_output = new JOCTET[2*buf_size];
98 // Save processed image into this JOCTET buffer, compressed as jpeg.
99 // This is done again by using the plug-in function.
100 img.save_jpeg_buffer(buffer_output,buf_size,60);
101 // Note that here, the variable 'buf_size' contains the length of the
102 // data which have been written in the given output buffer.
104 // Copy the content of the above array into a new file
105 // (it should give you a valid JPEG file then !)
106 const char *filename_output = "foo_output.jpg";
107 std::fprintf(stderr," - Save output file '%s'\n",filename_output);
108 std::FILE* file_output = std::fopen(filename_output,"wb");
109 std::fwrite(buffer_output, sizeof(JOCTET), buf_size, file_output);
110 std::fclose(file_output);
111 delete[] buffer_output;
113 std::fprintf(stderr," - All done !\n");
114 return 0;
115 }