MCPcopy Create free account
hub / github.com/GreycLab/CImg / main

Function main

examples/use_jpeg_buffer.cpp:56–109  ·  view source on GitHub ↗

Main procedure ----------------

Source from the content-addressed store, hash-verified

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}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected