RaffaeL_RsK

JpegFile.cpp

Dec 23rd, 2018
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.67 KB | None | 0 0
  1. #include "StdAfx.h"
  2. #include "JpegFile.h"
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <memory.h>
  6.  
  7. #include <libjpeg-6b/jpeglib.h>
  8. #include <libjpeg-6b/jpegLibLink.h>
  9.  
  10. #define OUTBUFFER_SIZE 0x8000
  11.  
  12. static FILE*fi;
  13. static JOCTET * buffer;
  14. static unsigned char*dest;
  15. static int len;
  16. static int destlen;
  17. static unsigned char*data;
  18. static int pos;
  19. static int size;
  20.  
  21. static void file_init_destination(j_compress_ptr cinfo)
  22. {
  23.   struct jpeg_destination_mgr*dmgr =
  24.       (struct jpeg_destination_mgr*)(cinfo->dest);
  25.   buffer = (JOCTET*)malloc(OUTBUFFER_SIZE);
  26.   if(!buffer) {
  27.       perror("malloc");
  28.       printf("Out of memory!\n");
  29.       exit(1);
  30.   }
  31.   dmgr->next_output_byte = buffer;
  32.   dmgr->free_in_buffer = OUTBUFFER_SIZE;
  33. }
  34.  
  35. static boolean file_empty_output_buffer(j_compress_ptr cinfo)
  36. {
  37.   struct jpeg_destination_mgr*dmgr =
  38.       (struct jpeg_destination_mgr*)(cinfo->dest);
  39.   if(fi)
  40.     fwrite(buffer, OUTBUFFER_SIZE, 1, fi);
  41.   dmgr->next_output_byte = buffer;
  42.   dmgr->free_in_buffer = OUTBUFFER_SIZE;
  43.   return 1;
  44. }
  45.  
  46. static void file_term_destination(j_compress_ptr cinfo)
  47. { struct jpeg_destination_mgr*dmgr =
  48.       (struct jpeg_destination_mgr*)(cinfo->dest);
  49.   if(fi)
  50.     fwrite(buffer, OUTBUFFER_SIZE-dmgr->free_in_buffer, 1, fi);
  51.   free(buffer);
  52.   buffer = 0;
  53.   dmgr->free_in_buffer = 0;
  54. }
  55.  
  56. static void mem_init_destination(j_compress_ptr cinfo)
  57. {
  58.   struct jpeg_destination_mgr*dmgr =
  59.       (struct jpeg_destination_mgr*)(cinfo->dest);
  60.   dmgr->next_output_byte = dest;
  61.   dmgr->free_in_buffer = destlen;
  62. }
  63.  
  64. static boolean mem_empty_output_buffer(j_compress_ptr cinfo)
  65. {
  66.     printf("jpeg mem overflow!\n");
  67.     exit(1);
  68. }
  69.  
  70. static void mem_term_destination(j_compress_ptr cinfo)
  71. {
  72.   struct jpeg_destination_mgr*dmgr =
  73.       (struct jpeg_destination_mgr*)(cinfo->dest);
  74.   len = destlen - dmgr->free_in_buffer;
  75.   dmgr->free_in_buffer = 0;
  76. }
  77.  
  78. int jpeg_save(unsigned char*data, int width, int height, int quality, const char*filename)
  79. {
  80.   struct jpeg_destination_mgr mgr;
  81.   struct jpeg_compress_struct cinfo;
  82.   struct jpeg_error_mgr jerr;
  83.   int t;
  84.  
  85.   if(filename) {
  86.     fi = fopen(filename, "wb");
  87.     if(fi == NULL)
  88.         return 0;
  89.   } else
  90.     fi = NULL;
  91.  
  92.   memset(&cinfo, 0, sizeof(cinfo));
  93.   memset(&jerr, 0, sizeof(jerr));
  94.   memset(&mgr, 0, sizeof(mgr));
  95.   cinfo.err = jpeg_std_error(&jerr);
  96.   jpeg_create_compress(&cinfo);
  97.  
  98.   mgr.init_destination = file_init_destination;
  99.   mgr.empty_output_buffer = file_empty_output_buffer;
  100.   mgr.term_destination = file_term_destination;
  101.   cinfo.dest = &mgr;
  102.  
  103.   // init compression
  104.  
  105.   cinfo.image_width  = width;
  106.   cinfo.image_height = height;
  107.   cinfo.input_components = 3;
  108.   cinfo.in_color_space = JCS_RGB;
  109.   jpeg_set_defaults(&cinfo);
  110.   jpeg_set_quality(&cinfo,quality,TRUE);
  111.  
  112.   //jpeg_write_tables(&cinfo);
  113.   //jpeg_suppress_tables(&cinfo, TRUE);
  114.   jpeg_start_compress(&cinfo, FALSE);
  115.  
  116.   for(t=0;t<height;t++) {
  117.     unsigned char*data2 = &data[width*3*t];
  118.     jpeg_write_scanlines(&cinfo, &data2, 1);
  119.   }
  120.   jpeg_finish_compress(&cinfo);
  121.  
  122.   if(fi)
  123.     fclose(fi);
  124.   jpeg_destroy_compress(&cinfo);
  125.   return 1;
  126. }
  127.  
  128. int jpeg_save_to_file(unsigned char*data, int width, int height, int quality, FILE*_fi)
  129. {
  130.   struct jpeg_destination_mgr mgr;
  131.   struct jpeg_compress_struct cinfo;
  132.   struct jpeg_error_mgr jerr;
  133.   int t;
  134.  
  135.   fi = _fi;
  136.  
  137.   memset(&cinfo, 0, sizeof(cinfo));
  138.   memset(&jerr, 0, sizeof(jerr));
  139.   memset(&mgr, 0, sizeof(mgr));
  140.   cinfo.err = jpeg_std_error(&jerr);
  141.   jpeg_create_compress(&cinfo);
  142.  
  143.   mgr.init_destination = file_init_destination;
  144.   mgr.empty_output_buffer = file_empty_output_buffer;
  145.   mgr.term_destination = file_term_destination;
  146.   cinfo.dest = &mgr;
  147.  
  148.   // init compression
  149.  
  150.   cinfo.image_width  = width;
  151.   cinfo.image_height = height;
  152.   cinfo.input_components = 3;
  153.   cinfo.in_color_space = JCS_RGB;
  154.   jpeg_set_defaults(&cinfo);
  155.   cinfo.dct_method = JDCT_IFAST;
  156.   jpeg_set_quality(&cinfo,quality,TRUE);
  157.  
  158.   //jpeg_write_tables(&cinfo);
  159.   //jpeg_suppress_tables(&cinfo, TRUE);
  160.   jpeg_start_compress(&cinfo, FALSE);
  161.  
  162.   for(t=0;t<height;t++) {
  163.     unsigned char*data2 = &data[width*3*t];
  164.     jpeg_write_scanlines(&cinfo, &data2, 1);
  165.   }
  166.   jpeg_finish_compress(&cinfo);
  167.   jpeg_destroy_compress(&cinfo);
  168.   return 1;
  169. }
  170.  
  171. int jpeg_save_to_mem(unsigned char*data, int width, int height, int quality, unsigned char*_dest, int _destlen)
  172. {
  173.   struct jpeg_destination_mgr mgr;
  174.   struct jpeg_compress_struct cinfo;
  175.   struct jpeg_error_mgr jerr;
  176.   int t;
  177.  
  178.   memset(&cinfo, 0, sizeof(cinfo));
  179.   memset(&jerr, 0, sizeof(jerr));
  180.   memset(&mgr, 0, sizeof(mgr));
  181.   cinfo.err = jpeg_std_error(&jerr);
  182.   jpeg_create_compress(&cinfo);
  183.  
  184.   dest = _dest;
  185.   len = 0;
  186.   destlen = _destlen;
  187.  
  188.   mgr.init_destination = mem_init_destination;
  189.   mgr.empty_output_buffer = mem_empty_output_buffer;
  190.   mgr.term_destination = mem_term_destination;
  191.   cinfo.dest = &mgr;
  192.  
  193.   // init compression
  194.  
  195.   cinfo.image_width  = width;
  196.   cinfo.image_height = height;
  197.   cinfo.input_components = 3;
  198.   cinfo.in_color_space = JCS_RGB;
  199.   jpeg_set_defaults(&cinfo);
  200.   cinfo.dct_method = JDCT_IFAST;
  201.   jpeg_set_quality(&cinfo,quality,TRUE);
  202.  
  203.   jpeg_start_compress(&cinfo, FALSE);
  204.   for(t=0;t<height;t++) {
  205.     unsigned char*data2 = &data[width*3*t];
  206.     jpeg_write_scanlines(&cinfo, &data2, 1);
  207.   }
  208.   jpeg_finish_compress(&cinfo);
  209.   jpeg_destroy_compress(&cinfo);
  210.   return len;
  211. }
  212.  
  213. void mem_init_source (j_decompress_ptr cinfo)
  214. {
  215.     struct jpeg_source_mgr* mgr = cinfo->src;
  216.     mgr->next_input_byte = data;
  217.     mgr->bytes_in_buffer = size;
  218.     //printf("init %d\n", size - mgr->bytes_in_buffer);
  219. }
  220.  
  221. boolean mem_fill_input_buffer (j_decompress_ptr cinfo)
  222. {
  223.     struct jpeg_source_mgr* mgr = cinfo->src;
  224.     printf("fill %d\n", size - mgr->bytes_in_buffer);
  225.     return 0;
  226. }
  227.  
  228. void mem_skip_input_data (j_decompress_ptr cinfo, long num_bytes)
  229. {
  230.     struct jpeg_source_mgr* mgr = cinfo->src;
  231.     printf("skip %d +%d\n", size - mgr->bytes_in_buffer, num_bytes);
  232.     if(num_bytes<=0)
  233.     return;
  234.     mgr->next_input_byte += num_bytes;
  235.     mgr->bytes_in_buffer -= num_bytes;
  236. }
  237.  
  238. boolean mem_resync_to_restart (j_decompress_ptr cinfo, int desired)
  239. {
  240.     struct jpeg_source_mgr* mgr = cinfo->src;
  241.     printf("resync %d\n", size - mgr->bytes_in_buffer);
  242.     mgr->next_input_byte = data;
  243.     mgr->bytes_in_buffer = size;
  244.     return 1;
  245. }
  246.  
  247. void mem_term_source (j_decompress_ptr cinfo)
  248. {
  249.     struct jpeg_source_mgr* mgr = cinfo->src;
  250.     //printf("term %d\n", size - mgr->bytes_in_buffer);
  251. }
  252.  
  253. int jpeg_load_from_mem(unsigned char*_data, int _size, unsigned char*dest, int width, int height)
  254. {
  255.     struct jpeg_decompress_struct cinfo;
  256.     struct jpeg_error_mgr jerr;
  257.     struct jpeg_source_mgr mgr;
  258.     int y;
  259.     //int x;
  260.  
  261.     data = _data;
  262.     size = _size;
  263.  
  264.     jpeg_create_decompress(&cinfo);
  265.  
  266.     mgr.next_input_byte = data;
  267.     mgr.bytes_in_buffer = size;
  268.     mgr.init_source        =mem_init_source ;
  269.     mgr.fill_input_buffer  =mem_fill_input_buffer ;
  270.     mgr.skip_input_data    =mem_skip_input_data ;
  271.     mgr.resync_to_restart  =mem_resync_to_restart ;
  272.     mgr.term_source        =mem_term_source ;
  273.  
  274.     cinfo.err = jpeg_std_error(&jerr);
  275.     cinfo.src = &mgr;
  276.  
  277.     jpeg_read_header(&cinfo, TRUE);
  278.     jpeg_start_decompress(&cinfo);
  279.  
  280.     for(y=0;y<height;y++) {
  281.     unsigned char*j = &dest[width*y*3];
  282.     jpeg_read_scanlines(&cinfo,&j,1);
  283.     }
  284.  
  285.     jpeg_finish_decompress(&cinfo);
  286.     jpeg_destroy_decompress(&cinfo);
  287.     return 1;
  288. }
  289.  
  290. typedef struct _RGBA {
  291.     unsigned char a,r,g,b;
  292. } RGBA;
  293.  
  294. typedef unsigned char U8;
  295.  
  296. int jpeg_load(const char*filename, unsigned char**dest, int*_width, int*_height)
  297. {
  298.     struct jpeg_decompress_struct cinfo;
  299.     struct jpeg_error_mgr jerr;
  300.     //struct jpeg_source_mgr mgr;
  301.  
  302.     FILE*fi = fopen(filename, "rb");
  303.     if(!fi) {
  304.         fprintf(stderr, "Couldn't open file %s\n", filename);
  305.     return 0;
  306.     }
  307.  
  308.     cinfo.err = jpeg_std_error(&jerr);
  309.     jpeg_create_decompress(&cinfo);
  310.     jpeg_stdio_src(&cinfo, fi);
  311.     jpeg_read_header(&cinfo, TRUE);
  312.     jpeg_start_decompress(&cinfo);
  313.    
  314.     U8*scanline = (U8 *)malloc(4 * cinfo.output_width);
  315.  
  316.     int width = *_width = cinfo.output_width;
  317.     int height = *_height = cinfo.output_height;
  318.     *dest = (unsigned char*)malloc(width*height*4);
  319.  
  320.     int y;
  321.     for (y=0;y<height;y++) {
  322.     int x;
  323.     U8 *js = scanline;
  324.         RGBA*line = &((RGBA*)(*dest))[y*width];
  325.  
  326.     jpeg_read_scanlines(&cinfo, &js, 1);
  327.     if (cinfo.out_color_space == JCS_GRAYSCALE) {
  328.         for (x = 0; x < width; x++) {
  329.         line[x].a = 255;
  330.         line[x].r = line[x].g = line[x].b = js[x];
  331.         }
  332.     } else if (cinfo.out_color_space == JCS_RGB) {
  333.         for (x = width - 1; x >= 0; x--) {
  334.         line[x].a = 255;
  335.         line[x].r = js[x*3+0];
  336.         line[x].g = js[x*3+1];
  337.         line[x].b = js[x*3+2];
  338.         }
  339.     } else if (cinfo.out_color_space == JCS_YCCK) {
  340.         fprintf(stderr, "Error: Can't convert YCCK to RGB.\n");
  341.         return 0;
  342.     } else if (cinfo.out_color_space == JCS_YCbCr) {
  343.         for (x = 0; x < width; x++) {
  344.         int y = js[x * 3 + 0];
  345.         int u = js[x * 3 + 1];
  346.         int v = js[x * 3 + 1];
  347.         line[x].a = 255;
  348.         line[x].r = y + ((360 * (v - 128)) >> 8);
  349.         line[x].g = y - ((88 * (u - 128) + 183 * (v - 128)) >> 8);
  350.         line[x].b = y + ((455 * (u - 128)) >> 8);
  351.         }
  352.     } else if (cinfo.out_color_space == JCS_CMYK) {
  353.         for (x = 0; x < width; x++) {
  354.         int white = 255 - js[x * 4 + 3];
  355.         line[x].a = 255;
  356.         line[x].r = white - ((js[x * 4] * white) >> 8);
  357.         line[x].g = white - ((js[x * 4 + 1] * white) >> 8);
  358.         line[x].b = white - ((js[x * 4 + 2] * white) >> 8);
  359.         }
  360.     }
  361.     }
  362.  
  363.     free(scanline);
  364.  
  365.     jpeg_finish_decompress(&cinfo);
  366.     jpeg_destroy_decompress(&cinfo);
  367.     fclose(fi);
  368.     return 1;
  369. }
Add Comment
Please, Sign In to add comment