Skip to content

Instantly share code, notes, and snippets.

@matwey
Created November 4, 2015 16:03
Show Gist options
  • Save matwey/ba1edb9894ed452f8084 to your computer and use it in GitHub Desktop.
Save matwey/ba1edb9894ed452f8084 to your computer and use it in GitHub Desktop.
libjpeg
#include <stdio.h>
#include <stdlib.h>
#include <jpeglib.h>
unsigned char buf[3][64] = {{
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0xff,
0xff, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0xff,
0xff, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0xff,
0x80, 0x00, 0x80, 0x80, 0x40, 0x40, 0x00, 0x40,
0x80, 0x00, 0x80, 0x80, 0x40, 0x40, 0x00, 0x40,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40,
0x80, 0x80, 0x80, 0x80, 0x40, 0x40, 0x40, 0x40
},{
0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff,
0x80, 0xff, 0xff, 0x80, 0x80, 0x80, 0x80, 0x80,
0x40, 0xff, 0xff, 0x40, 0x40, 0x40, 0x40, 0x40,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
/* Padding */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
},{
0xff, 0x80, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
0xff, 0x80, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
0xff, 0x80, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
0xff, 0x80, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
/* Padding */
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
}};
JSAMPARRAY jimage[3];
int main(int argc, char** argv) {
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
cinfo.image_width = 8;
cinfo.image_height = 8;
cinfo.input_components = 3;
jpeg_set_defaults(&cinfo);
jpeg_set_colorspace(&cinfo, JCS_YCbCr);
cinfo.raw_data_in = TRUE;
cinfo.comp_info[0].h_samp_factor = 2;
cinfo.comp_info[0].v_samp_factor = 2;
cinfo.comp_info[1].h_samp_factor = 1;
cinfo.comp_info[1].v_samp_factor = 1;
cinfo.comp_info[2].h_samp_factor = 1;
cinfo.comp_info[2].v_samp_factor = 1;
jpeg_stdio_dest(&cinfo, stdout);
jpeg_start_compress(&cinfo, TRUE);
for(int i=0;i<cinfo.input_components;i++){
jimage[i] = (JSAMPARRAY)calloc(cinfo.comp_info[i].height_in_blocks*DCTSIZE, sizeof(JSAMPROW));
for(int j=0;j<cinfo.comp_info[i].height_in_blocks*DCTSIZE;++j) {
jimage[i][j] = buf[i] + j * cinfo.comp_info[i].width_in_blocks*DCTSIZE;
}
}
jpeg_write_raw_data(&cinfo,jimage,cinfo.max_v_samp_factor*DCTSIZE);
for(int i=0;i<cinfo.input_components;i++){
free(jimage[i]);
}
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment