Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9037495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T09:15:34+00:00 2026-06-16T09:15:34+00:00

There is a strange change when saving JPEG image to the iOS photo library.

  • 0

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.

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);

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];

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);

Image “new.jpg” is saved in photo library now.

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

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 8×8 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 

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T09:15:36+00:00Added an answer on June 16, 2026 at 9:15 am

    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.

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is a strange behavior in my code and I really don't know how
I noticed something strange and there is a possibility I am wrong. I have
There is some strange behaviour in parsing KML produced by Google Earth using a
I know that it sound strange. printf shouldnt change anything,but without it sendTo failed.
Got a strange one and I know it is something silly but I can't
I'm using ASP.NET MVC 3 Beta and for some strange reason, if I change
I have noticed that there are strange requests to my website trying to find
i try to send my datas to php with ajax but there's strange mistake.
there is a strange white line in my screen. It's possible to see the
There is a strange bug in ListView/GridView in WPF when the SelectionMode is set

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.