将JPEG保存到ios照片库后,DCT系数会发生变化

将JPEG图像保存到iOS照片库时有一个奇怪的变化。 我不知道我是否做错了什么。 我使用libjpeg-turbo访问JPEG图像,然后修改图像的DCT系数。 修改后的图像(仅在DCT中,没有其他)被保存在照片库中。 但是在打开保存的图像之后,DCT系数与前一步中更改的不一样。

详细地说,让我解释一下如何为每个DCT添加+1。 我正在使用libjpeg库的“example.c”中的标准过程:

struct jpeg_decompress_struct cinfo; struct my_error_mgr jerr; FILE * infile; if ((infile = fopen(filename, "rb")) == NULL) { fprintf(stderr, "can't open %s\n", filename); return 0; } cinfo.err = jpeg_std_error(&jerr.pub); jerr.pub.error_exit = my_error_exit; if (setjmp(jerr.setjmp_buffer)) { jpeg_destroy_decompress(&cinfo); fclose(infile); return 0; } jpeg_create_decompress(&cinfo); jpeg_stdio_src(&cinfo, infile); (void) jpeg_read_header(&cinfo, TRUE); jvirt_barray_ptr* coeffs_array; coeffs_array = jpeg_read_coefficients(&cinfo); BOOL done = FALSE; for (int ci = 0; ci < 3; ci++) { JBLOCKARRAY buffer_one; JCOEFPTR blockptr_one; jpeg_component_info* compptr_one; compptr_one = cinfo.comp_info + ci; for (int by = 0; by < compptr_one->height_in_blocks; by++) { buffer_one = (cinfo.mem->access_virt_barray)((j_common_ptr)&cinfo, coeffs_array[ci], by, (JDIMENSION)1, FALSE); for (int bx = 0; bx < compptr_one->width_in_blocks; bx++) { blockptr_one = buffer_one[0][bx]; for (int bi = 0; bi < 64; bi++) { blockptr_one[bi]++; } } } } write_jpeg(output, &cinfo, coeffs_array); // saving modified JPEG to the output file jpeg_destroy_decompress(&cinfo); fclose(infile); 

之后,我有一个新的JPEG图像保存在文件,可以说“new.jpg”。 现在我想把这个“new.jpg”保存到照片库,所以我加载图像:

 imageToSave = [UIImage imageWithContentsOfFile:outputFile]; 

我也检查了DCT系数保持改变。 我有相同的图像后,我的修改的DCT检查,然后我要保存它:

 UIImageWriteToSavedPhotosAlbum(imageToSave, nil, nil, nil); 

图片“new.jpg”现在保存在照片库中。

到目前为止,一切工作都很好,DCT系数像libjpeg库一样工作。 当我再次加载保存的图像并查看DCT系数时,就会发生变化。 我发现DCT被改变了,我不知道为什么。 当您想要保存JPEG图像时,iOS是否有任何优化algorithm? 为什么DCT改变了。

我正在使用以下过程来读取保存的图像:

 NSData *jpegData = UIImageJPEGRepresentation([info objectForKey:UIImagePickerControllerOriginalImage], 1); [jpegData writeToFile:file atomically:YES]; // saved image is saved in file and I can use the procedure from above to check DCTs 

此外,这是一个原始DCT的例子,修改的DCT通过向所有添加+1以及在将其保存到照片库中的一个8×8块之后加载DCT:

 Original DCTs: -759 -24 -8 1 -1 0 0 1 56 -27 -10 1 0 1 0 0 8 0 0 0 0 -1 0 0 0 0 0 -1 0 -1 0 -1 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 0 0 0 0 Modified DCTs by libjpeg: -758 -23 -7 2 0 1 1 2 57 -26 -9 2 1 2 1 1 9 1 1 1 1 0 1 1 1 1 1 0 1 0 1 0 1 1 1 2 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 DCTs after saving JPEG to photo library: -758 -22 -7 2 0 0 0 0 58 -26 -8 3 0 0 0 0 8 2 0 0 -1 1 0 0 2 3 0 0 0 0 0 0 2 1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 

任何帮助,将不胜感激。 我真的不知道为什么在将图像保存到照片库之后,DCT被改变了。

当您使用UIImage及其方法时,iOS始终会重新压缩JPEG,因此会改变DCT。 相反,使用AssetsLibrary作为NSData存储和检索用户的图片。 这种行为没有logging,但它的工作原理。

我还build议将.jpeg存储在临时文件夹中,然后将其提供给libjpeg-turbo。

Interesting Posts