デモ用ソフト開発メモとか。
#include <stdio.h>
#include <stdlib.h>
#include <jpeglib.h>
int main(int argc, char *argv[]) {
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
FILE *infile;
FILE *outfile;
char filename[BUFSIZ];
int ***data;
unsigned char *line_buf;
int width, height, ncomp;
int c, i, j;
if (argc != 2) {
printf("Usage: test infile\n");
exit(1);
}
sprintf(filename, "%s", argv[1]);
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
if ((infile = fopen(filename, "rb")) == NULL) {
fprintf(stderr, "can't open %s\n", filename);
exit(1);
}
jpeg_stdio_src(&cinfo, infile);
jpeg_read_header(&cinfo, TRUE);
jpeg_start_decompress(&cinfo);
width = cinfo.output_width;
height = cinfo.output_height;
ncomp = cinfo.out_color_components;
printf("Width: %d\n", width);
printf("Height: %d\n", height);
printf("#comps: %d\n", ncomp);
data = malloc(ncomp * sizeof(int **));
for (c = 0; c < ncomp; ++c) {
data[c] = malloc(height * sizeof(int *));
for (j = 0; j < height; ++j)
data[c][j] = malloc(width * sizeof(int));
}
line_buf = malloc(ncomp * width * sizeof(unsigned char));
j = 0;
while (jpeg_read_scanlines(&cinfo, &line_buf, 1)) {
for (i = 0; i < width; ++i)
for (c = 0; c < ncomp; ++c)
data[c][j][i] = line_buf[ncomp * i + c];
++j;
}
jpeg_finish_decompress(&cinfo);
close(infile);
jpeg_destroy_decompress(&cinfo);
if ((outfile = fopen("hoge.ppm", "wb")) == NULL) {
fprintf(stderr, "can't open %s\n", filename);
exit(1);
}
fprintf(outfile, "P6\n# %s\n%d %d \n255\n", "ppm", width, height);
for (j = 0; j < height; ++j)
for (i = 0; i < width; ++i)
for (c = 0; c < ncomp; ++c)
fputc(data[c][j][i], outfile);
close(outfile);
}