blob: 65e86ff1d7f7b844fbb0dd54d7df2f2113217c43 [file] [log] [blame]
Austin Schuh8c794d52019-03-03 21:17:37 -08001/*
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.eu )
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*/
43
44// These includes are necessary to get the plug-in compile !
45#include <cstdio>
46#include <jpeglib.h>
47#include <jerror.h>
48
49// Define plugin and include the CImg Library.
50#define cimg_plugin "plugins/jpeg_buffer.h"
51#include "CImg.h"
52using namespace cimg_library;
53
54// Main procedure
55//----------------
56int main() {
57
58 // Create a jpeg memory buffer from the content of a jpeg file.
59 // (this is for testing purposes only)
60 const char *filename_input = "foo.jpg";
61 std::fprintf(stderr," - Reading file '%s'\n",filename_input);
62 std::FILE *file_input = std::fopen(filename_input,"rb");
63 if (!file_input) { std::fprintf(stderr,"Input JPEG file not found !"); std::exit(0); }
64
65 std::fprintf(stderr," - Construct input JPEG-coded buffer\n");
66 unsigned buf_size = 500000; // Put the file size here !
67 JOCTET *buffer_input = new JOCTET[buf_size];
68 if (std::fread(buffer_input,sizeof(JOCTET),buf_size,file_input)) std::fclose(file_input);
69 // -> 'buffer_input' is now a valid jpeg-coded memory buffer.
70
71 // Create a CImg instance from the jpeg-coded buffer using the plug-in function.
72 std::fprintf(stderr," - Create CImg instance from JPEG-coded buffer\n");
73 CImg<unsigned char> img;
74 img.load_jpeg_buffer(buffer_input, buf_size);
75 delete[] buffer_input;
76
77 // Do you image processing stuff here ....
78 // Here, we just mirror the image and write "hello".
79 std::fprintf(stderr," - Do simple processing\n");
80 const unsigned char purple[] = { 255, 0, 0 };
81 const unsigned char black[] = { 0, 0, 0 };
82 img.mirror('y').draw_text(0,0," Hello! ",purple,black,1,57);
83
84 // Display image to see if everything's fine.
85 img.display("Using 'jpeg_buffer.h' plugin");
86
87 // Define a new JOCTET array where the processed image has to be saved
88 // (we don't know its dimension before compressing it, therefore we have to allocate enough memory )
89 std::fprintf(stderr," - Construct output JPEG-coded buffer\n");
90 JOCTET *buffer_output = new JOCTET[2*buf_size];
91
92 // Save processed image into this JOCTET buffer, compressed as jpeg.
93 // This is done again by using the plug-in function.
94 img.save_jpeg_buffer(buffer_output,buf_size,60);
95 // Note that here, the variable 'buf_size' contains the length of the
96 // data which have been written in the given output buffer.
97
98 // Copy the content of the above array into a new file
99 // (it should give you a valid JPEG file then !)
100 const char *filename_output = "foo_output.jpg";
101 std::fprintf(stderr," - Save output file '%s'\n",filename_output);
102 std::FILE* file_output = std::fopen(filename_output,"wb");
103 std::fwrite(buffer_output, sizeof(JOCTET), buf_size, file_output);
104 std::fclose(file_output);
105 delete[] buffer_output;
106
107 std::fprintf(stderr," - All done !\n");
108 return 0;
109}