2

For a specific task (replace an image inside a firmware without any data offset shifting), I need to create a GIF file of size 409x240 pixels of exactly 6449 bytes:

  • offset 0-5: GIF89a (magic string)
  • ...
  • offset 6447-6448: hex 00 3B

When I created such a GIF with, for example MSPaint, the output is ~ 2 KB, which is too small.

Question: is there a way to modify a GIF file to take a larger given size? Can we do null-byte (hex 00) padding near the end of the GIF file? Maybe with ImageMagick, or directly with a HEX editor (I already have one)?

11
  • Does the actual contents matter? Your question suggests not so you could simply use a tool to expand the file to the correct size superuser.com/questions/299329/resize-a-file-in-command-prompt and the remaining data would simply be zeroes.
    – Mokubai
    Commented Sep 19, 2023 at 7:48
  • Or add a comment: superuser.com/questions/556315/gif-image-exif-tags
    – Berend
    Commented Sep 19, 2023 at 8:24
  • @Mokubai, Yes the actual contents matter (a logo picture). I can easily expand a given 2 KB .gif file by adding zero-bytes at the end. But then it won't be a correct GIF file. For example a GIF has to end with 3B hex or 00 3B hex (I need to check). There are maybe other conditions to make a valid GIF file.
    – Basj
    Commented Sep 19, 2023 at 8:33
  • 1
    Ohhh you're right @Berend, I didn't think about this :) Sorry, my bad! I thought you were speaking about setting a "size" metadata to a bigger value :)
    – Basj
    Commented Sep 19, 2023 at 8:41
  • 1
    Okay, writing now... Commented Sep 19, 2023 at 10:30

1 Answer 1

2

The first thing I would try is to pad the GIF file with nulls. You can first try to simply append with a sufficient number of nulls. If that doesn't work, you can try adding the nulls immediately before the GIF file's final block terminator.

You can also try creating an XMP comment as recommended by Berend and discussed here: https://superuser.com/a/556337/380110

Now, if none of the above work, here's another possibility. We don't have your actual image file, so I can't guarantee this will work, but it's certainly worth a try.

GIF images support LZW compression at different compression levels. Thus, you can modify the level of compression of the file to try to get it to be the exact size you want.

I'm not sure what compression (if any) MS Paint is using to generate your 2KB file, but here's the procedure I would try:

  1. Upload your image to: https://www.freeconvert.com/gif-compressor
  2. Click on "GIF Options".
  3. Select "Undo Optimizations".
  4. Save the GIF file locally.
  5. See what size the file is to determine if you now need to increase or decrease its file size.
  6. Using that same web-app, adjust the LZW compression level to adjust the file size. You can also adjust the number of colors, if needed.

If you don't get quite the file size you need, you can tweak a few pixels in your image to yield the desired result. Remember, for compression, the LZW algorithm is largely looking for similar blocks of pixels. So if you intentionally make blocks of pixels dissimilar, it will increase the file size. This doesn't need to be a visible change. A slightly different color for a single pixel will likely be undetectable by anyone without a magnifying glass. You can make slightly different changes in numerous areas without any visible difference, but it will increase the file size.

You can also ensure your image uses a complete palette of 256 unique colours. This will make the file much larger. Again, you can do this creatively to hide that fact that you are using more unique colours than are actually needed to render your image.

2
  • 1
    I inserted null bytes just before the last byte, so that the last byte remains 3B (which marks the end of a GIF file). It works at least with various viewers. Now, finger crossed that it'll work in the firmware too!
    – Basj
    Commented Sep 19, 2023 at 12:10
  • Great! I hope it works! Let me know. Commented Sep 19, 2023 at 14:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .