Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted.*/
- #include <stdio.h>
- #include "jpeglib.h"
- int main(int argc, char **argv) {
- struct jpeg_decompress_struct srcinfo;
- struct jpeg_compress_struct dstinfo;
- struct jpeg_error_mgr srcerr, dsterr;
- jvirt_barray_ptr *coef;
- char *coef_filename;
- FILE *coef_file;
- JBLOCKARRAY coef_array;
- int c;
- JDIMENSION n_rows, n_cols, y, x;
- if (argc < 2) {
- fprintf(stderr, "Usage: replacedct coef_file < input.jpg > output.jpg\n");
- return 1;
- }
- coef_filename = argv[1];
- srcinfo.err = jpeg_std_error(&srcerr);
- jpeg_create_decompress(&srcinfo);
- jpeg_stdio_src(&srcinfo, stdin);
- jpeg_read_header(&srcinfo, TRUE);
- coef = jpeg_read_coefficients(&srcinfo);
- coef_file = fopen(coef_filename, "rb");
- if (coef_file == NULL) {
- fprintf(stderr, "Error opening file: %s\n", coef_filename);
- return 1;
- }
- for (c = 0; c < srcinfo.num_components; c++) {
- n_rows = srcinfo.comp_info[c].height_in_blocks;
- n_cols = srcinfo.comp_info[c].width_in_blocks;
- for (y = 0; y < n_rows; y++) {
- coef_array = (*srcinfo.mem->access_virt_barray)((j_common_ptr) &srcinfo, coef[c], y, 1, TRUE);
- for (x = 0; x < n_cols; x++) {
- if (fread(coef_array[0][x], sizeof(JCOEF), DCTSIZE2, coef_file) != DCTSIZE2) {
- fprintf(stderr, "Error reading coefficients\n");
- fclose(coef_file);
- return 1;
- }
- }
- }
- }
- dstinfo.err = jpeg_std_error(&dsterr);
- jpeg_create_compress(&dstinfo);
- jpeg_stdio_dest(&dstinfo, stdout);
- jpeg_copy_critical_parameters(&srcinfo, &dstinfo);
- jpeg_write_coefficients(&dstinfo, coef);
- jpeg_finish_compress(&dstinfo);
- jpeg_destroy_compress(&dstinfo);
- jpeg_finish_decompress(&srcinfo);
- jpeg_destroy_decompress(&srcinfo);
- fclose(coef_file);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement