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