JPEG保存到ios照片库后,DCT系数发生了变化 [英] DCT coefficients changed after JPEG saved to ios photo library

查看:166
本文介绍了JPEG保存到ios照片库后,DCT系数发生了变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将JPEG图像保存到iOS照片库时有一个奇怪的变化。我不知道我做错了什么。
我正在使用libjpeg-turbo访问JPEG图像,然后修改图像的DCT系数。修改后的图像(仅在DCT中,仅此而已)保存在照片库中。但是,打开保存的图像后,DCT系数与上一步中更改的有所不同。

There is a strange change when saving JPEG image to the iOS photo library. I don't know if I am doing something wrong. I'm using libjpeg-turbo to access JPEG images and then I modify DCT coefficients of the image. Modified image (just in DCT, nothing else) is saved in photo library. But after I open the saved image, DCT coefficients are not the same that I changed in the previous step.

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

In detail, let me explain how I'm adding +1 to each DCT. I am using standard procedure from "example.c" of the libjpeg library:

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保存到照片库,以便通过以下方式加载图像:

After this I have a new JPEG image saved in the file, lets say "new.jpg". Now I want to save this "new.jpg" to the photo library so I load the image by:

imageToSave = [UIImage imageWithContentsOfFile:outputFile];

我还检查了DCT系数是否保持不变。在我拥有相同的图像并检查了修改后的DCT之后,我将保存它:

I also checked that the DCT coefficients remained changed. After I have the same image wuth my modified DCT checked then I'm going to save it:

UIImageWriteToSavedPhotosAlbum(imageToSave, nil, nil, nil);

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

Image "new.jpg" is saved in photo library now.

到现在为止,一切工作正常,DCT系数的工作方式与libjpeg库相同。当我再次加载保存的图像并查看DCT系数时,变化就来了。我发现DCT发生了变化,我不知道为什么。当您要保存JPEG图像时,iOS是否使用任何优化算法?为什么更改DCT。

Until now, everything works great, DCT coefficients works like it was meant with the libjpeg library. The change comes when I load the saved image again and look into DCT coefficients. I found out that DCTs are changed and I don't know why. Is there any optimization algorithm that iOS use when you want to save JPEG image? Why are DCTs changed.

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

I'm using the following procedure to read the saved image:

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)以及在将DCT保存到一个8x8块的图片库后加载的DCT的示例:

Furthermore, this is an example of original DCTs, modified DCTs by adding +1 to all, and loaded DCTs after saving it to photo library for one 8x8 block:

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会被更改。

Any help would be appreciated. I am really out of ideas why are DCTs after saving image to photo library altered.

推荐答案

iOS始终会在您重新压缩JPEG时使用UIImage及其方法,从而更改DCT。而是使用AssetsLibrary将用户的图片存储和检索为NSData。

iOS will always recompress your JPEG when you use UIImage and its methods, hence altering DCT. Instead, use AssetsLibrary to store and retrieve user's pictures as NSData. This behavior is not documented, but it works.

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

I'd also recommend storing the .jpeg in a temp folder, and then feeding it to libjpeg-turbo.

这篇关于JPEG保存到ios照片库后,DCT系数发生了变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆