Advertisement
Guest User

replacedct.c

a guest
Nov 15th, 2018
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.82 KB | None | 0 0
  1. /*Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.*/
  2.  
  3. #include <stdio.h>
  4. #include "jpeglib.h"
  5.  
  6. int main(int argc, char **argv) {
  7.     struct jpeg_decompress_struct srcinfo;
  8.     struct jpeg_compress_struct dstinfo;
  9.     struct jpeg_error_mgr srcerr, dsterr;
  10.     jvirt_barray_ptr *coef;
  11.     char *coef_filename;
  12.     FILE *coef_file;
  13.     JBLOCKARRAY coef_array;
  14.     int c;
  15.     JDIMENSION n_rows, n_cols, y, x;
  16.  
  17.     if (argc < 2) {
  18.         fprintf(stderr, "Usage: replacedct coef_file < input.jpg > output.jpg\n");
  19.         return 1;
  20.     }
  21.     coef_filename = argv[1];
  22.  
  23.     srcinfo.err = jpeg_std_error(&srcerr);
  24.     jpeg_create_decompress(&srcinfo);
  25.     jpeg_stdio_src(&srcinfo, stdin);
  26.     jpeg_read_header(&srcinfo, TRUE);
  27.     coef = jpeg_read_coefficients(&srcinfo);
  28.  
  29.     coef_file = fopen(coef_filename, "rb");
  30.     if (coef_file == NULL) {
  31.         fprintf(stderr, "Error opening file: %s\n", coef_filename);
  32.         return 1;
  33.     }
  34.  
  35.     for (c = 0; c < srcinfo.num_components; c++) {
  36.         n_rows = srcinfo.comp_info[c].height_in_blocks;
  37.         n_cols = srcinfo.comp_info[c].width_in_blocks;
  38.         for (y = 0; y < n_rows; y++) {
  39.             coef_array = (*srcinfo.mem->access_virt_barray)((j_common_ptr) &srcinfo, coef[c], y, 1, TRUE);
  40.             for (x = 0; x < n_cols; x++) {
  41.                 if (fread(coef_array[0][x], sizeof(JCOEF), DCTSIZE2, coef_file) != DCTSIZE2) {
  42.                     fprintf(stderr, "Error reading coefficients\n");
  43.                     fclose(coef_file);
  44.                     return 1;
  45.                 }
  46.             }
  47.         }
  48.     }
  49.  
  50.     dstinfo.err = jpeg_std_error(&dsterr);
  51.     jpeg_create_compress(&dstinfo);
  52.     jpeg_stdio_dest(&dstinfo, stdout);
  53.     jpeg_copy_critical_parameters(&srcinfo, &dstinfo);
  54.     jpeg_write_coefficients(&dstinfo, coef);
  55.  
  56.     jpeg_finish_compress(&dstinfo);
  57.     jpeg_destroy_compress(&dstinfo);
  58.     jpeg_finish_decompress(&srcinfo);
  59.     jpeg_destroy_decompress(&srcinfo);
  60.     fclose(coef_file);
  61.  
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement