Jump to content

GIF encoder is not optimal


Wes64
 Share

Recommended Posts

The paint.net GIF encoder does not optimize the size of the GIF global color table. The GIF color table is capable of holding 4, 8, 16, 32, 64, 128, and 256 colors as specified by the packed byte in the logical screen descriptor. Paint.net always pads the size of the global color table to 256, meaning that up to 756 bytes are wasted per GIF.

 

I have produced this image using paint.net and attached it as "pdn_encoded.gif" post-142812-0-12305900-1441645839.gif. Here is the image as hex:

47 49 46 38 39 61 10 00 10 00 f7 00 00 00 00 00 00 26 ff ff 00 00 ff ff ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 2c 00 00 00 00 10 00 10 00 00 08 4f 00 03 08 1c 48 b0 a0 c1 83 06 07 28 5c c8 50 21 c1 86 10 07 3c 1c 20 a0 a2 c5 8a 0e 07 2a bc 78 31 a3 c0 8d 02 00 88 04 80 51 a2 46 8a 21 47 96 9c 58 71 24 49 01 1e 03 80 74 b9 f2 24 47 8b 31 41 de cc 19 91 e1 c4 9e 31 11 0a 1d 3a 30 20 00 3b

 

The packed byte in the logical screen descriptor is f7, meaning the GCT size is going to be 2 << (f7 & 7) ==  256. However, the GIF only contains 4 colors: #000000, #0026ff, #ff0000, #ffffff.

 

A more optimal encoding is produced here using python. This is attached as "opt_encoded.gif" post-142812-0-57299300-1441645830.gif. Here is the hex:

47 49 46 38 39 61 10 00 10 00 f1 00 00 00 00 00 00 26 ff ff 00 00 ff ff ff 2c 00 00 00 00 10 00 10 00 00 08 4f 00 03 08 1c 48 b0 a0 c1 83 06 07 28 5c c8 50 21 c1 86 10 07 3c 1c 20 a0 a2 c5 8a 0e 07 2a bc 78 31 a3 c0 8d 02 00 88 04 80 51 a2 46 8a 21 47 96 9c 58 71 24 49 01 1e 03 80 74 b9 f2 24 47 8b 31 41 de cc 19 91 e1 c4 9e 31 11 0a 1d 3a 30 20 00 3b

 

The size of the global color table is now limited to 2 << (f1 & 7) == 4 and space is not wasted. This should be optimized because if one wants to amass just 100 GIF images (i.e. for distribution in a program or game), up to 75kb is wasted, which is a huge issue.

 

Hopefully uploading the images to the forum does not change the bytecode, which is why the hex is posted here. This is occurring with paint.net 4.0.6 on 64-bit Windows 7.

 

EDIT: This occurs both when you create a GIF in paint.net and when you open an existing GIF and save it again (even without making changes).

Edited by Wes64
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...