Skip to content

Instantly share code, notes, and snippets.

@AesSedai101
Created May 26, 2014 08:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AesSedai101/af8634c812921a4d0716 to your computer and use it in GitHub Desktop.
Save AesSedai101/af8634c812921a4d0716 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <jpeglib.h>
#include <iostream>
using namespace std;
JBLOCKARRAY rowPtrs[MAX_COMPONENTS];
void read(jpeg_decompress_struct srcinfo, jvirt_barray_ptr * src_coef_arrays) {
//cout << "Started reading DCT" << endl;
for (JDIMENSION compNum=0; compNum < srcinfo.num_components; compNum++) {
size_t blockRowSize = (size_t) sizeof(JCOEF) * DCTSIZE2 * srcinfo.comp_info[compNum].width_in_blocks;
for (JDIMENSION rowNum=0; rowNum < srcinfo.comp_info[compNum].height_in_blocks; rowNum++) {
// A pointer to the virtual array of dct values
rowPtrs[compNum] = ((&srcinfo)->mem->access_virt_barray)((j_common_ptr) &srcinfo, src_coef_arrays[compNum],rowNum, (JDIMENSION) 1, FALSE);
// Loop through the blocks to get the dct values
for (JDIMENSION blockNum=0; blockNum < srcinfo.comp_info[compNum].width_in_blocks; blockNum++){
//...iterate over DCT coefficients
for (JDIMENSION i=0; i<DCTSIZE2; i++){
//and print them to standard out - one per line
cout << rowPtrs[compNum][0][blockNum][i] << endl;
}
}
}
}
}
int main() {
//TODO: change this to read a different file
const char* filename = "image4.jpg";
FILE * infile;
struct jpeg_decompress_struct srcinfo;
struct jpeg_error_mgr srcerr;
if ((infile = fopen(filename, "rb")) == NULL) {
fprintf(stderr, "can't open %s\n", filename);
return 0;
}
srcinfo.err = jpeg_std_error(&srcerr);
jpeg_create_decompress(&srcinfo);
jpeg_stdio_src(&srcinfo, infile);
(void) jpeg_read_header(&srcinfo, FALSE);
//coefficients
jvirt_barray_ptr * src_coef_arrays = jpeg_read_coefficients(&srcinfo);
read(srcinfo, src_coef_arrays);
jpeg_destroy_decompress(&srcinfo);
fclose(infile);
return 0;
}
@stephan735
Copy link

Do you happen to have an example of modifying DCT coefficients and writing a new JPG to disk ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment