Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bright-light-in-the-night/065eb7f640e2142cde9ff791635a0a7f to your computer and use it in GitHub Desktop.
Save bright-light-in-the-night/065eb7f640e2142cde9ff791635a0a7f to your computer and use it in GitHub Desktop.
Transform an 24 bits color depth image file into 16 bits color depth raw file and back again
<?php
// https://ee-programming-notepad.blogspot.com/2016/09/php-script-to-transform-24-bits-color.html
error_reporting(E_ALL);
ini_set('display_errors', 'On');
header('Content-type: text/html; charset=utf-8');
set_time_limit(0);
// DESCRIPTION:
// these functions will transform an 24 bits color depth image file into 16 bits color depth raw file and back again
// USEFULNESS:
// to be used on the ILI9340 IC based LCD screens (2.2" SPI TFT, 240x320)
// NOTES:
// - characters representation on 16 bits are made in 2 chars
define('IMAGE_WIDTH', 240);
define('IMAGE_HEIGHT', 320);
function jpeg_image_to_raw_file($filename)
{
$im = imagecreatefromjpeg($filename);
$output = '';
for ($y = 0; $y < IMAGE_HEIGHT; $y++)
{
for ($x = 0; $x < IMAGE_WIDTH; $x++)
{
$rgb = imagecolorat($im, $x, $y);
// split into color components (RGB)
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
if (1) {
// transform into procent of 8-8-8 bits
$r_procent = round(($r * 100) / 255);
$g_procent = round(($g * 100) / 255);
$b_procent = round(($b * 100) / 255);
// transform into procent of 5-6-5 bits
$r_5bits = round(($r_procent * 31) / 100);
$g_6bits = round(($g_procent * 63) / 100);
$b_5bits = round(($b_procent * 31) / 100);
} else {
// loose 3-2-3 bits of color depth
$r_5bits = $r >> 3;
$g_6bits = $g >> 2;
$b_5bits = $b >> 3;
}
// merge all color components into one
$rgb_16bits = ($r_5bits << 11) ^ ($g_6bits << 5) ^ $b_5bits;
// split into 2 chunks of 8 bits
$char1 = ($rgb_16bits >> 8) & 0xFF;
$char2 = $rgb_16bits & 0xFF;
// make them ASCII char
$rgb_16bits_char = chr($char1) . chr($char2);
// join them
$output .= $rgb_16bits_char;
}
}
return $output;
}
function raw_file_to_jpeg_image($filename)
{
$content = file_get_contents($filename);
$content_len = strlen($content);
$im = imagecreatetruecolor(IMAGE_WIDTH, IMAGE_HEIGHT);
$increm = 1;
$x = 0;
$y = 0;
for ($i = 0; $i < $content_len; $i += 2)
{
// get the first 2 chunks of 8 bits in reversed order and make them numbers again
$colors = 0;
$k = 1;
for ($j = 0; $j < 2; $j++)
{
$colors ^= ord($content[$i + $j]) << ($k * 8);
$k--;
}
// split into color components (RGB)
$r_5bits_back = ($colors >> 11) & 0x1F;
$g_6bits_back = ($colors >> 5) & 0x3F;
$b_5bits_back = $colors & 0x1F;
// convert from 5-6-5 bits to 8-8-8 bits
if (1) {
// transform into procent of 5-6-7 bits
$r_proc = round(($r_5bits_back * 100) / 31);
$g_proc = round(($g_6bits_back * 100) / 63);
$b_proc = round(($b_5bits_back * 100) / 31);
// transform into 8-8-8 bits of color from procent
$r = round(($r_proc * 255) / 100);
$g = round(($g_proc * 255) / 100);
$b = round(($b_proc * 255) / 100);
} else {
// add some zeros
$r = $r_5bits_back << 3;
$g = $g_6bits_back << 2;
$b = $b_5bits_back << 3;
}
// rebuild the image
$color = imagecolorallocate($im, $r, $g, $b);
imagesetpixel($im, $x, $y, $color);
// lets calculate some image coordinates
if ($increm % IMAGE_WIDTH == 0) {
$y++;
$x = 0;
} else {
$x++;
}
$increm++;
}
ob_start();
imagejpeg($im);
return ob_get_clean();
}
// run the script
{
$original_image = 'nature.jpg';
$raw_image = 'nature.raw';
$output_image = 'nature-back.jpg';
echo '<pre>';
if (!file_exists($original_image)) {
echo '<b>There is no '.$original_image.' image file</b>';
exit;
}
// IMAGE TO RAW
echo '<b>Transform to raw:</b> <br>';
echo 'input original_image: '.$original_image.'<br>';
echo 'input raw_image: '.$raw_image.'<br>';
file_put_contents($raw_image, jpeg_image_to_raw_file($original_image));
echo 'filesize raw_image: '.filesize($raw_image).'<br>';
// RAW TO IMAGE
echo '<br>';
echo '<b>Transform from raw:</b> <br>';
echo 'input raw_image: '.$raw_image.'<br>';
echo 'input output_image: '.$output_image.'<br>';
file_put_contents($output_image, raw_file_to_jpeg_image($raw_image));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment