Tôi đã tìm thấy chức năng này trong đó sử dụng libjpeg để viết thư cho một tập tin:Ghi vào bộ nhớ đệm thay vì tệp bằng libjpeg?
int write_jpeg_file(char *filename)
{
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
/* this is a pointer to one row of image data */
JSAMPROW row_pointer[1];
FILE *outfile = fopen(filename, "wb");
if (!outfile)
{
printf("Error opening output jpeg file %s\n!", filename);
return -1;
}
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
jpeg_stdio_dest(&cinfo, outfile);
/* Setting the parameters of the output file here */
cinfo.image_width = width;
cinfo.image_height = height;
cinfo.input_components = bytes_per_pixel;
cinfo.in_color_space = color_space;
/* default compression parameters, we shouldn't be worried about these */
jpeg_set_defaults(&cinfo);
/* Now do the compression .. */
jpeg_start_compress(&cinfo, TRUE);
/* like reading a file, this time write one row at a time */
while(cinfo.next_scanline < cinfo.image_height)
{
row_pointer[0] = &raw_image[ cinfo.next_scanline * cinfo.image_width * cinfo.input_components];
jpeg_write_scanlines(&cinfo, row_pointer, 1);
}
/* similar to read file, clean up after we're done compressing */
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
fclose(outfile);
/* success code is 1! */
return 1;
}
tôi sẽ thực sự cần phải viết hình ảnh jpeg nén chỉ vào bộ nhớ đệm, mà không lưu nó vào một tập tin, để tiết kiệm thời gian. Ai đó có thể cho tôi một ví dụ làm thế nào để làm điều đó?
Tôi đã tìm kiếm trên web trong một thời gian nhưng tài liệu rất hiếm khi có bất kỳ ví dụ nào cũng gặp khó khăn.
Tôi tưởng tượng rằng các con trỏ hàm này là những gì 'jpeg_stdio_dest' ảnh hưởng đến? –
@Ben Voigt, nhìn vào nguồn của 'jpeg_stdio_dest' chính xác là nó làm gì. Nó phân bổ một cấu trúc và đặt nó thành 'cinfo-> dest', sau đó đặt con trỏ.Tôi đang suy nghĩ mã mẫu của riêng tôi có thể là một chút không đầy đủ vì nó không tạo ra một cấu trúc 'jpeg_destination_mgr', nhưng tôi sẽ xem xét nó sau. –
Ồ, tất nhiên rồi. Bạn có thể [lưu trữ dữ liệu của riêng bạn ('std :: vector') ngay sau các con trỏ hàm và không cần biến toàn cục] (http://blogs.msdn.com/b/oldnewthing/archive/2010/12/20/ 10107027.aspx). –