Is there a way to convert a 16-bit (grayscale) color PNG to an RGBA4444 color format using PHP?
-OR-
Is there a way to load this 16-bit grayscale PNG using RGBA4444 format?
The PNG header says that it is using 16-bit color (Bit depth) and grayscale color (Color type) (http://www.fileformat.info/format/png/corion.htm, IHDR Image Header).
$rgb = imagecolorat($src, $x, $y);
var_dump("RGB - ".dechex($rgb));
$rgba = imagecolorsforindex($src, $rgb);
var_dump("RGBA - ".dechex($rgba));
The value of $rgb
(for example) is A7
while $rgba
is [A7, A7, A7, 0]
.
BTW, here is the header of the said file:
89 50 4E 47 0D 0A 1A 0A 00 00 00 0D 49 48 44 52 | .PNG........IHDR
00 00 03 FF 00 00 03 FF 10 00 00 00 00 E3 F9 FF | ................
C9 00 00 00 0D 74 45 58 74 44 46 4D 54 00 52 34 | .....tEXtDFMT.R4
47 34 42 34 41 34 E0 94 BA 92 00 00 20 00 49 44 | G4B4A4........ID
41 54 .. .. | AT
EDIT:
What I did first was follow this code by Charlie (https://stackoverflow.com/a/7293429/2205703). (Of course with some modification.) Then convert each 16-bit color format (based on tEXt
chunk) to RGBA8888 format.
Then, pack()
them to PNG file format. But I still got image error.
Up to 65,536 colors can be represented in the color palette. Most graphics formats provide 8-bit color or 24-bit color; however, graphics cards generally have an intermediate 16-bit color range that displays 65,536 colors. See color depth.
16-Bit is not ideal for printing as the printer cannot read all the available colors accurately. That's why 8-Bitit JPEG works just fine for most printing applications. 16-Bit is the best format for editing photos in software without losing image detail and color depth.
One of the easiest ways to know if you are working with an 8-bit image is to check the image's file format. If the image is a JPEG (with the extension “. jpg”), it will always be an 8-bit image. One of the advantages of working with 8-bit images is they are typically smaller in file size.
An 8-bit image can only contain a maximum of 256 shades of gray, while a 16-bit image can contain up to 65,536 shades of gray.
Brad's answer's not bad. Truncating to 8 bit could be good enough with grayscale images. RGBA would end in an transparent image that is probably not what you're really looking for. Did you allready tried to to convert to a full-color 24 bit image? The result would be still looking a like grayscale they you could simply reduce it to 8 bit with ease.
Using imagick would make this procedure much simpler:
convert source-image.png -evaluate multiply 16 -depth 8 destination-image.png
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With