I am using TeXlive 2016 with LuaTeX. My TeX documents include images, which must be 8-bit flattened grayscale (no transparency) or 1-bit monochrome, in PNG format. I am good with graphics programs, and correctly prepare the images with proper model and resolution, finished using Magick
mogrify -strip and verified with
\includegraphics knows how to size an image at 100% scale, based on its pixels and resolution. It read the metadata, I assume. Now for my question: can
\includegraphics (or some other command within LuaLaTeX, not shell) give a yes-no answer as to whether an image is really grayscale or monochrome? The images are always PNG.
My concern is that an unprocessed image (say in rgba model, using only gray shades) could accidentally be included. I do not expect TeX to convert the image or operate on it in any way. All I want is for it to be inspected. If not flattened gray or monochrome, then I'd write a warning message to the log. Then I'd know that I mistakenly included the wrong kind of image.
Possible, without reams of code? I have the PDF spec, but reading it is not for amateurs.
My question is broader than it might seem. Anyone preparing PDF for black and white print-on-demand would face similar issues. I realize that the images are not PNG within the PDF; but that is how they are input to TeX.
EDIT: After reviewing David's response, below, I looked at the image format in more detail. Something similar applies to JPG, but I will focus on PNG.
The PNG specification requires an
IHDR chunk early in the file. The tenth byte after the string
IHDR is a code for the image color model. A grayscale (or monochrome) image, without alpha channel, has hex code
00 there. Thus, if Lua has the ability to directly read the image file bytes, it should be able to scan for IHDR, count to ten, and report the byte.
Apparently that is NOT what Lua's
img.colorspace does, since it always returns
nil regardless of what PNG image (gray, color) I feed it. Either
img.colorspace is not what I expect, or it tries but fails to get the right code.