{"id":1715,"date":"2019-03-21T19:11:07","date_gmt":"2019-03-21T10:11:07","guid":{"rendered":"http:\/\/rageworx.info\/?p=1715"},"modified":"2021-05-17T01:06:32","modified_gmt":"2021-05-16T16:06:32","slug":"writing-rgb-or-grey-buffer-into-a-jpeg-file","status":"publish","type":"post","link":"https:\/\/rageworx.info\/?p=1715","title":{"rendered":"Writing RGB or GREY buffer into a JPEG file"},"content":{"rendered":"<h3>Import issue:<\/h3>\n<blockquote><p>This example code is for Linux ( or possible for MacOSX ).<br \/>\nand .. Fixed some wrong code<\/p><\/blockquote>\n<h2>Makefile<\/h2>\n<p>It must be declared to link with jpeg library in system, or it should not be switched with turbo-jpeg.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">LFLAGS += -ljpeg<\/pre>\n<h2>Code for jpeglib.<\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"dracula\">#include &lt;jpeglib.h&gt;\r\n\r\nint writejpeg( const char* fname, unsigned char *src, size_t w, size_t h, size_t d, unsigned qual = 100 )\r\n{\r\n    FILE* outf = NULL;\r\n    struct jpeg_compress_struct jcs = {0};\r\n    struct jpeg_error_mgr jem = {0};\r\n\r\n    JSAMPROW row_pointer[1];\r\n    jcs.err = jpeg_std_error(&amp;jem);\r\n    jpeg_create_compress(&amp;jcs);\r\n\r\n    jcs.image_width = w;\r\n    jcs.image_height = h;\r\n    jcs.input_components = d;\r\n\r\n    switch( d )\r\n    {\r\n        case 1:\r\n            jcs.in_color_space = JCS_GRAYSCALE;\r\n            break;\r\n\r\n        case 3:\r\n            jcs.in_color_space = JCS_RGB;\r\n            break;\r\n        \r\n        default:\r\n            return -1;\r\n    }\r\n    \r\n    outf = fopen( fname,\"wb\" );\r\n    if ( outf == NULL )\r\n        return -2;\r\n\r\n    jpeg_stdio_dest(&amp;jcs, outf);\r\n    \r\n    if ( qual &gt; 100 ) qual = 100;\r\n    else\r\n    if ( qual == 0  ) qual = 10;\r\n    \r\n    jpeg_set_defaults(&amp;jcs);\r\n    jpeg_set_quality(&amp;jcs, qual, TRUE);\r\n    jpeg_start_compress(&amp;jcs, TRUE);\r\n\r\n    while( jcs.next_scanline &lt; jcs.image_height )\r\n    {\r\n        row_pointer[0] = &amp;src[ jcs.next_scanline *\r\n                               jcs.image_width *\r\n                               jcs.input_components ];\r\n        jpeg_write_scanlines( &amp;jcs, row_pointer, 1 );\r\n    }\r\n\r\n    jpeg_finish_compress(&amp;jcs);\r\n    jpeg_destroy_compress(&amp;jcs);\r\n    fclose(out);\r\n\r\n    return 0;\r\n}\r\n\r\nCode for TURBOJPEG\r\nLFLAGS += -lturbojpeg\r\n#include &lt;turbojpeg.h&gt;\r\n\r\nint writejpeg( const char* fname, unsigned char* src, size_t w, size_t h, size_t d, unsigned qual = 100 )\r\n{\r\n    unsigned long jsz = 0;\r\n    int jfmt = 0;\r\n    int jsmp = 0;\r\n    \r\n    switch( d )\r\n    {\r\n        case 1:\r\n            jfmt = TJPF_GRAY;\r\n            jsmp = TJSAMP_GRAY;\r\n            break;\r\n            \r\n        case 2:\r\n            return -1;\r\n\r\n        case 3:\r\n            jfmt = TJPF_RGB;\r\n            jsmp = TJSAMP_444;\r\n            break;\r\n\r\n        case 4:\r\n            jfmt = TJPF_RGBA;\r\n            jsmp = TJSAMP_444;\r\n            break;\r\n    }\r\n\r\n    if ( qual &gt; 100 ) qual = 100;\r\n    else\r\n    if ( qual == 0  ) qual = 10;\r\n        \r\n    uchar* outbuff = NULL;\r\n    \r\n    thandle jComp = tjInitCompress();\r\n    if ( jComp != NULL )\r\n    {\r\n        tjCompress2( jComp,\r\n                     src,\r\n                     w,\r\n                     0,\r\n                     h,\r\n                     jfmt,\r\n                     &amp;outbuff,\r\n                     &amp;jsz,\r\n                     TJSAMP_444,\r\n                     qual,\r\n                     TJFLAG_FASTDCT );\r\n         tjDestroy( jComp );\r\n         \r\n         if ( jsz &gt; 0 )\r\n         {\r\n             FILE* outf = fopen( fname, \"wb\" );\r\n             if ( outf != NULL )\r\n             {\r\n                 fwrite( outbuff, 1, jsz, outf );\r\n                 fclose( outf );\r\n                 \r\n                 delete[] outbuff;\r\n                 return 0;\r\n             }\r\n             else\r\n             {\r\n                 delete[] outbuff;\r\n             }\r\n         }\r\n    }\r\n    \r\n    return -1;\r\n}<\/pre>\n<h2>Using case for FLTK RGB image<\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"cpp\" data-enlighter-theme=\"dracula\">bool WriteImage2JPEG( Fl_RGB_Image* img, const char* fn ) \r\n{ \r\n    if ( ( img != NULL ) &amp;&amp; ( fn != NULL ) ) \r\n    { \r\n        uchar* pRef = (uchar*)pRef-&gt;data()[0]; \r\n        int ret = writejpeg( fn, pRef, img-&gt;w(), img-&gt;h(), img-&gt;d(), 81 ); \r\n        if ( ret == 0 ) \r\n            return true; \r\n    } \r\n    return false; \r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Import issue: This example code is for Linux ( or possible for MacOSX ). and .. Fixed some wrong code Makefile It must be declared to link with jpeg library in system, or it should not be switched with turbo-jpeg. LFLAGS += -ljpeg Code for jpeglib. #include &lt;jpeglib.h&gt; int writejpeg(&#8230; <a href=\"https:\/\/rageworx.info\/?p=1715\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,580,3],"tags":[10,11,616,102,71,468,617,153],"class_list":["post-1715","post","type-post","status-publish","format-standard","hentry","category-development","category-linux","category-raphs","tag-c","tag-code","tag-cpp","tag-example","tag-fltk","tag-fl_rgb_image","tag-jpeg","tag-linux"],"_links":{"self":[{"href":"https:\/\/rageworx.info\/index.php?rest_route=\/wp\/v2\/posts\/1715","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rageworx.info\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rageworx.info\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rageworx.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rageworx.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1715"}],"version-history":[{"count":0,"href":"https:\/\/rageworx.info\/index.php?rest_route=\/wp\/v2\/posts\/1715\/revisions"}],"wp:attachment":[{"href":"https:\/\/rageworx.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1715"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rageworx.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1715"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rageworx.info\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1715"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}