How to embed metadata (i.e. JSON/XML) into an image file? - c#

In .NET suppose I have an application that can create and open an image that has its metadata defined in a seperate file (i.e. '.json'/'.xml'). It would spit or open the metadata file of the same name as the image file.
Now to make distribution of the image + metadata files easier, I want to eliminate the extra metadata file so that users can open an image and has all the relevent info about the image. What are some of the good strategies for embedding the data into the image file itself?
Here are a few possibilities I found by googling:
Visual embedding:
Data encoded as 2D Barcode such as QR Code, Data Matrix or PDF417.
Data drawn as plain text on the image, retrieved through OCR
Format embedding:
Data inserted as JPEG comments/EXIF comments
Data inserted as PNG text chunks
The basic requirements are:
Data can be in JSON, XML or binary format, as long as it's machine-readable.
Data can be in the KB's, up to several dozens of KB's. The more the better.
For visual embedding:
Data should hold up well against a certain degree of lossy compressions.
The shape of the data 'zone' on the image's canvas should be as flexible as possible to better use the image's real estate.
Support all common image formats. (bmp, jpg, png, gif, etc.)
If possible, use open source .NET libraries.
Any advice welcomed. Thanks!

One way to do this is to store the information as a string of XML in the EXIF portion of the .jpg file, maybe in the UserComment field (9286H) of the EXIF IFD.
Here is some info on loading and saving metadata using gdi+.

Related

Is there a standard format for .sprite files?

I'm trying to build a small C# Windows Form, graphics program and I need to save/load sprite sheets. I've searched but can't find a standard file format for sprite sheets, I have however found the .sprite extension.
So my questions are:
Is there a standard format for a .sprite file (e.g. header 15 bytes and contains the length of etc...)
Is there a different file format I should be using for sprite sheets?
I have personally never heard of a .sprite file extension; Googling it seems it may be used for the Scratch language (https://en.scratch-wiki.info/wiki/Scratch_File_Format#Sprite_Files).
Typically though, spritesheets are just image files with multiple images on them. PNG files seem to be the most common, though there is no real reason you couldn't use basically any image file type (though keep in mind some may not have alpha channels, etc.). Most software that packs spritesheets will output in a PNG, and will either split it up based on a grid system or provide a catalog of information (often in JSON format) for splitting the sheet into the subimages you need.

c# Image size increases after saving it

I'm trying to save the image as follows:
var file = Image.FromFile(#"D:\front.png");
file.Save(#"D:\front_save.png");
Size of the saved imeage increases almost twice the size of original one. How do I save the image so it's completely the same as original image?
Image Link
Unfortunately, .NET doesn't expose any way to control the parameters that are applied as part of the PNG compression. Since PNG is a lossless format, the two images are "the same", they're just stored differently. What ever tool saved the original image must use different compression parameters than the default .NET PNG compression defaults, resulting in an optimized file size. When you save from .NET, the default compression parameters result in a larger, non-optimized, file size.
The answer to this question lists some third party libraries that you could use for PNG optimization. If you're not concerned about losing information, you could save the image as a jpeg.

Most efficient/performant way to store large images and metadata in tiff format

I have a requirement to save ~1-5 tiff images with a maximum size of roughly 10MB each. Each file will have it's own metadata file which contains serialized classes. I also need to house another XML file for quick access to important data about the file (potentially to search and sort the images and tell the software how to interact with the image).
What would be the most efficient and fastest to load/save all this data?
I was thinking possibly using a multi-paged tiff file where each page has it's own metadata file and xml file, the problem being that would be one huge tiff file and possibly very slow to load and save.
Would it be better to separate the metadata and xml from the file itself? If so then I could possibly combine the metadata and xml files. Or should I not use a multi-page tiff and create multiple tiff files in a folder structure?
Any help appreciated...

Edit TIFF image without changing header data

I have a program that takes in stacks of TIFF images and is very particular about the header data (it expects all headers to be the same), however I want to edit a couple of the images in the stack before sending it to this program.
Every program I've tried so far (Paint.net, MS Paint, ImageJ) has altered the header file or outright corrupted it when it saves the new images. I have access to C# and LibTiff.Net but even after reading the documents I'm confused as to how to simply replace the image data without changing the header information at all.
Currently the idea is simply to replace an image with a solid colour, so it isn't too complicated.
Here's how I would go about this.
Check TIFF documentation to find out where the actual bitmap data is stored. (I believe it is a structured format so it won't be at the same place every time, although it could be if all your headers are the same.)
Once you've identified the data, you can extract it or replace it with raw bitmap data of the same dimensions and format.
For example, you could extract the bitmap data from the TIFF file with the changed headers and overwrite the data in a file with a good header.
It's pretty low level, but it should work.
Alternatively, you could read-in the edited files and write back out a TIFF file with your own headers in the correct format. Might be more or less difficult.

Modifying JPEG headers in C#

I'm trying to open a file to bytes, convert it to a string, modify some data (think Steganography) and convert the file back to bytes and save it as a jpeg. So far, everything I've tried has corrupted the file in converting it to a string. I've tried converting it to a 64-bit string, but of course that's a bit hard to modify the data in :P
Any suggestions on how I can do this properly, without corrupting my file?
I don't have this in C# but in PHP, but you can take a look and adaptate to C#.
http://www.havenard.110mb.com/fotomagica/
This is my site where there's a tool to modify the EXIF data of a JPEG and build "magic pictures" which display something in the thumbnail which is not the real picture.
It opens the JPEG, slice its sectors, and build it back ignoring irrelevant sectors and placing my custom made EXIF header.
And this is the source of the PHP classes:
http://www.havenard.110mb.com/fotomagica/class.JpegMapper.php.txt (ExifMapper is incomplete)
http://www.havenard.110mb.com/fotomagica/class.DataMapper.php.txt
You can study it and rebuild in C#, it is really simple to slice a JPEG as you will see.
Usage of this PHP class (only JpegMapper):
$jpg = new JpegMapper('picture.jpg');
$jpg->save_filtered('filtere picture.jpg'); // save removing irrelevant sectors
It is great to get any JPEG even smaller (sometimes a lot smaller).

Categories

Resources