使用 jpeglib 从 file.jpg 读取原始压缩缓冲区
Read raw compressed buffer from file.jpg using jpeglib
我正在寻找代码来打开 file.jpg
并将数据加载到缓冲区中,而无需实际解压缩数据。我需要按原样发送数据。
我发现了一个 code 可以读取图像并将其解压缩。我不知道如何修改代码以获取原始字节而不是解压缩版本。
struct jpeg_decompress_struct cinfo;
struct my_error_mgr jerr;
FILE * infile; /* source file */
JSAMPARRAY buffer; /* Output row buffer */
int row_stride; /* physical row width in output buffer */
if ((infile = fopen(filename, "rb")) == NULL) {
fprintf(stderr, "can't open %s\n", filename);
return 0;
}
/* Now we can initialize the JPEG decompression object. */
jpeg_create_decompress(&cinfo);
/* Step 2: specify data source (eg, a file) */
jpeg_stdio_src(&cinfo, infile);
(void) jpeg_read_header(&cinfo, TRUE);
// Here I want to only get raw bytes
(void) jpeg_start_decompress(&cinfo);
row_stride = cinfo.output_width * cinfo.output_components;
/* Make a one-row-high sample array that will go away when done with image */
buffer = (*cinfo.mem->alloc_sarray)
((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
while (cinfo.output_scanline < cinfo.output_height) {
(void) jpeg_read_scanlines(&cinfo, buffer, 1);
/* Assume put_scanline_someplace wants a pointer and sample count. */
// put_scanline_someplace(buffer[0], row_stride);
}
/* Step 7: Finish decompression */
(void) jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
fclose(infile);
在问题post下交流过后发现,如果我们不关心解压,简单的字节读取就足够了。这是解决问题的代码:
char* data = (char*)malloc(max_w*max_h*3 +100);
if (data == NULL) {
printf("Input data must not be NULL.\n");
return 0;
}
FILE *fp = fopen(filename, "rb"); /* b - binary mode */
if (fp == NULL) {
printf("Error opening file %s\n", filename);
return 0;
}
struct stat filestatus;
stat(filename, &filestatus);
size_t data_size = filestatus.st_size;
size_t len = fread(data, 1, data_size , fp);
if (len != data_size) {
printf("Error reading file %s\n", filename);
return 0;
}
fclose(fp);
return len;
// free(data) when you are done.
我正在寻找代码来打开 file.jpg
并将数据加载到缓冲区中,而无需实际解压缩数据。我需要按原样发送数据。
我发现了一个 code 可以读取图像并将其解压缩。我不知道如何修改代码以获取原始字节而不是解压缩版本。
struct jpeg_decompress_struct cinfo;
struct my_error_mgr jerr;
FILE * infile; /* source file */
JSAMPARRAY buffer; /* Output row buffer */
int row_stride; /* physical row width in output buffer */
if ((infile = fopen(filename, "rb")) == NULL) {
fprintf(stderr, "can't open %s\n", filename);
return 0;
}
/* Now we can initialize the JPEG decompression object. */
jpeg_create_decompress(&cinfo);
/* Step 2: specify data source (eg, a file) */
jpeg_stdio_src(&cinfo, infile);
(void) jpeg_read_header(&cinfo, TRUE);
// Here I want to only get raw bytes
(void) jpeg_start_decompress(&cinfo);
row_stride = cinfo.output_width * cinfo.output_components;
/* Make a one-row-high sample array that will go away when done with image */
buffer = (*cinfo.mem->alloc_sarray)
((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1);
while (cinfo.output_scanline < cinfo.output_height) {
(void) jpeg_read_scanlines(&cinfo, buffer, 1);
/* Assume put_scanline_someplace wants a pointer and sample count. */
// put_scanline_someplace(buffer[0], row_stride);
}
/* Step 7: Finish decompression */
(void) jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
fclose(infile);
在问题post下交流过后发现,如果我们不关心解压,简单的字节读取就足够了。这是解决问题的代码:
char* data = (char*)malloc(max_w*max_h*3 +100);
if (data == NULL) {
printf("Input data must not be NULL.\n");
return 0;
}
FILE *fp = fopen(filename, "rb"); /* b - binary mode */
if (fp == NULL) {
printf("Error opening file %s\n", filename);
return 0;
}
struct stat filestatus;
stat(filename, &filestatus);
size_t data_size = filestatus.st_size;
size_t len = fread(data, 1, data_size , fp);
if (len != data_size) {
printf("Error reading file %s\n", filename);
return 0;
}
fclose(fp);
return len;
// free(data) when you are done.