roswrap/src/toojpeg/toojpeg.h
Go to the documentation of this file.
1 #include "sick_scan/sick_scan_base.h" /* Base definitions included in all header files, added by add_sick_scan_base_header.py. Do not edit this line. */
2 // //////////////////////////////////////////////////////////
3 // toojpeg.h
4 // written by Stephan Brumme, 2018-2019
5 // see https://create.stephan-brumme.com/toojpeg/
6 //
7 
8 // This is a compact baseline JPEG/JFIF writer, written in C++ (but looks like C for the most part).
9 // Its interface has only one function: writeJpeg() - and that's it !
10 //
11 // basic example:
12 // => create an image with any content you like, e.g. 1024x768, RGB = 3 bytes per pixel
13 // auto pixels = new unsigned char[1024*768*3];
14 // => you need to define a callback that receives the compressed data byte-by-byte from my JPEG writer
15 // void myOutput(unsigned char oneByte) { fputc(oneByte, myFileHandle); } // save byte to file
16 // => let's go !
17 // TooJpeg::writeJpeg(myOutput, mypixels, 1024, 768);
18 
19 #pragma once
20 
21 namespace TooJpeg
22 {
23  // write one byte (to disk, memory, ...)
24  typedef void (*WRITE_ONE_BYTE)(unsigned char);
25  // this callback is called for every byte generated by the encoder and behaves similar to fputc
26  // if you prefer stylish C++11 syntax then it can be a lambda, too:
27  // auto myOutput = [](unsigned char oneByte) { fputc(oneByte, output); };
28 
29  // output - callback that stores a single byte (writes to disk, memory, ...)
30  // pixels - stored in RGB format or grayscale, stored from upper-left to lower-right
31  // width,height - image size
32  // isRGB - true if RGB format (3 bytes per pixel); false if grayscale (1 byte per pixel)
33  // quality - between 1 (worst) and 100 (best)
34  // downsample - if true then YCbCr 4:2:0 format is used (smaller size, minor quality loss) instead of 4:4:4, not relevant for grayscale
35  // comment - optional JPEG comment (0/NULL if no comment), must not contain ASCII code 0xFF
36  bool writeJpeg(WRITE_ONE_BYTE output, const void* pixels, unsigned short width, unsigned short height,
37  bool isRGB = true, unsigned char quality = 90, bool downsample = false, const char* comment = nullptr);
38 } // namespace TooJpeg
39 
40 // My main inspiration was Jon Olick's Minimalistic JPEG writer
41 // ( https://www.jonolick.com/code.html => direct link is https://www.jonolick.com/uploads/7/9/2/1/7921194/jo_jpeg.cpp ).
42 // However, his code documentation is quite sparse - probably because it wasn't written from scratch and is (quote:) "based on a javascript jpeg writer",
43 // most likely Andreas Ritter's code: https://github.com/eugeneware/jpeg-js/blob/master/lib/encoder.js
44 //
45 // Therefore I wrote the whole lib from scratch and tried hard to add tons of comments to my code, especially describing where all those magic numbers come from.
46 // And I managed to remove the need for any external includes ...
47 // yes, that's right: my library has no (!) includes at all, not even #include <stdlib.h>
48 // Depending on your callback WRITE_ONE_BYTE, the library writes either to disk, or in-memory, or wherever you wish.
49 // Moreover, no dynamic memory allocations are performed, just a few bytes on the stack.
50 //
51 // In contrast to Jon's code, compression can be significantly improved in many use cases:
52 // a) grayscale JPEG images need just a single Y channel, no need to save the superfluous Cb + Cr channels
53 // b) YCbCr 4:2:0 downsampling is often about 20% more efficient (=smaller) than the default YCbCr 4:4:4 with only little visual loss
54 //
55 // TooJpeg 1.2+ compresses about twice as fast as jo_jpeg (and about half as fast as libjpeg-turbo).
56 // A few benchmark numbers can be found on my website https://create.stephan-brumme.com/toojpeg/#benchmark
57 //
58 // Last but not least you can optionally add a JPEG comment.
59 //
60 // Your C++ compiler needs to support a reasonable subset of C++11 (g++ 4.7 or Visual C++ 2013 are sufficient).
61 // I haven't tested the code on big-endian systems or anything that smells like an apple.
62 //
63 // USE AT YOUR OWN RISK. Because you are a brave soul :-)
TooJpeg
Definition: roswrap/src/toojpeg/toojpeg.cpp:349
TooJpeg::writeJpeg
bool writeJpeg(WRITE_ONE_BYTE output, const void *pixels_, unsigned short width, unsigned short height, bool isRGB, unsigned char quality_, bool downsample, const char *comment)
Definition: roswrap/src/toojpeg/toojpeg.cpp:352
TooJpeg::WRITE_ONE_BYTE
void(* WRITE_ONE_BYTE)(unsigned char)
Definition: roswrap/src/toojpeg/toojpeg.h:24
sick_scan_base.h


sick_scan_xd
Author(s): Michael Lehning , Jochen Sprickerhof , Martin Günther
autogenerated on Fri Oct 25 2024 02:47:12