Advertisement
Guest User

Untitled

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