You said it: if the functions you apply are not invertible (onto and one-to-one) then you'll need to keep more information than just the function used and its input parameters. This is a mathematical fact unfortunately, so you cannot get around it.
- Invert - Is a bijection, so you only need to store that you inverted the image on the stack
- Grayscale - Is onto, but not one-to-one, so you'll need to store extra information along with the function used.
Here are a few things you can do to conserve memory:
Store only two original colors of the three. The grayscale formula is
output=0.21 R + 0.71 G + 0.07 B
. Since you'll know the output, you only need to store the red and blue channels in memory, and ignore the green. This is an analog to the YCbCr colorspace. You'll have the grayscale, and only need to store the Cb and Cr images in memory.Compression. Lossless compression is possible with images if you store them in the
png
format orjpeg
format with 100% quality. Depending on the language, you should be able to write to a byte array in memory after conversion. Depending on the image, this could be significant savings.Saving to temporary disk space. People don't want their computer to crash by taking up memory, and loading images is fast from disk these days. All OS offer application-level temporary space. Just make sure you read from the disk into a large buffer instead of reading one byte at a time, and read the entire contents into memory before doing processing. This will make reading the images from the disk super fast.