【发布时间】:2018-10-14 01:55:11
【问题描述】:
我正在寻找编写代码来打开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);
【问题讨论】:
-
请澄清:您要发送完整的 jpg 文件还是只发送文件的编码数据部分?请注意,在第二种情况下,如果没有文件其余部分中包含的信息,编码数据可能是无用的。
-
@user4581301 我想发送整个编码数据,包括标题。
-
在这种情况下,请考虑以下内容:
std::ifstream file(filename, std::ios::binary);std::vector<char>((std::istreambuf_iterator<char>(file)),std::istreambuf_iterator<char>());整个分块位于向量中,您可以通过data方法将其作为vector或char的数组访问。 -
@user4581301 需要我提前知道数据大小吗?
-
酷。或者您可以
seek()到最后,使用ftell()获取位置(即大小),然后seek()回到开头。无论哪种方式。
标签: c++ jpeg image-compression libjpeg