Advertisement
Guest User

color depth transform from 24 bits to 16 bits

a guest
Sep 23rd, 2016
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.99 KB | None | 0 0
  1. <?php
  2.  
  3. error_reporting(E_ALL);
  4. ini_set('display_errors', 'On');
  5. header('Content-type: text/html; charset=utf-8');
  6. set_time_limit(0);
  7.  
  8. // DESCRIPTION:
  9. // these functions will transform an 24 bits color depth image file into 16 bits color depth raw file and back again
  10.  
  11. // USEFULNESS:
  12. // to be used on the ILI9340 IC based LCD screens (2.2" SPI TFT, 240x320)
  13.  
  14. // NOTES:
  15. // - characters representation on 16 bits are made in 2 chars
  16.  
  17. define('IMAGE_WIDTH', 240);
  18. define('IMAGE_HEIGHT', 320);
  19.  
  20. function jpeg_image_to_raw_file($filename)
  21. {
  22.     $im = imagecreatefromjpeg($filename);
  23.  
  24.     $output = '';
  25.  
  26.     for ($y = 0; $y < IMAGE_HEIGHT; $y++)
  27.     {
  28.         for ($x = 0; $x < IMAGE_WIDTH; $x++)
  29.         {
  30.             $rgb = imagecolorat($im, $x, $y);
  31.  
  32.             // split into color components (RGB)
  33.             $r = ($rgb >> 16) & 0xFF;
  34.             $g = ($rgb >> 8) & 0xFF;
  35.             $b = $rgb & 0xFF;
  36.  
  37.             if (1) {
  38.                 // transform into procent of 8-8-8 bits
  39.                 $r_procent = round(($r * 100) / 255);
  40.                 $g_procent = round(($g * 100) / 255);
  41.                 $b_procent = round(($b * 100) / 255);
  42.  
  43.                 // transform into procent of 5-6-5 bits
  44.                 $r_5bits = round(($r_procent * 31) / 100);
  45.                 $g_6bits = round(($g_procent * 63) / 100);
  46.                 $b_5bits = round(($b_procent * 31) / 100);
  47.             } else {
  48.                 // loose 3-2-3 bits of color depth
  49.                 $r_5bits = $r >> 3;
  50.                 $g_6bits = $g >> 2;
  51.                 $b_5bits = $b >> 3;
  52.             }
  53.  
  54.             // merge all color components into one
  55.             $rgb_16bits = ($r_5bits << 11) ^ ($g_6bits << 5) ^ $b_5bits;
  56.  
  57.             // split into 2 chunks of 8 bits
  58.             $char1 = ($rgb_16bits >> 8) & 0xFF;
  59.             $char2 = $rgb_16bits & 0xFF;
  60.  
  61.             // make them ASCII char
  62.             $rgb_16bits_char = chr($char1) . chr($char2);
  63.  
  64.             // join them
  65.             $output .= $rgb_16bits_char;
  66.         }
  67.     }
  68.  
  69.     return $output;
  70. }
  71.  
  72. function raw_file_to_jpeg_image($filename)
  73. {
  74.     $content = file_get_contents($filename);
  75.     $content_len = strlen($content);
  76.     $im = imagecreatetruecolor(IMAGE_WIDTH, IMAGE_HEIGHT);
  77.     $increm = 1;
  78.     $x = 0;
  79.     $y = 0;
  80.  
  81.     for ($i = 0; $i < $content_len; $i += 2)
  82.     {
  83.         // get the first 2 chunks of 8 bits in reversed order and make them numbers again
  84.         $colors = 0;
  85.         $k = 1;
  86.         for ($j = 0; $j < 2; $j++)
  87.         {
  88.             $colors ^= ord($content[$i + $j]) << ($k * 8);
  89.             $k--;
  90.         }
  91.  
  92.         // split into color components (RGB)
  93.         $r_5bits_back = ($colors >> 11) & 0x1F;
  94.         $g_6bits_back = ($colors >> 5) & 0x3F;
  95.         $b_5bits_back = $colors & 0x1F;
  96.  
  97.         // convert from 5-6-5 bits to 8-8-8 bits
  98.         if (1) {
  99.             // transform into procent of 5-6-7 bits
  100.             $r_proc = round(($r_5bits_back * 100) / 31);
  101.             $g_proc = round(($g_6bits_back * 100) / 63);
  102.             $b_proc = round(($b_5bits_back * 100) / 31);
  103.  
  104.             // transform into 8-8-8 bits of color from procent
  105.             $r = round(($r_proc * 255) / 100);
  106.             $g = round(($g_proc * 255) / 100);
  107.             $b = round(($b_proc * 255) / 100);
  108.         } else {
  109.             // add some zeros
  110.             $r = $r_5bits_back << 3;
  111.             $g = $g_6bits_back << 2;
  112.             $b = $b_5bits_back << 3;
  113.         }
  114.  
  115.         // rebuild the image
  116.         $color = imagecolorallocate($im, $r, $g, $b);
  117.         imagesetpixel($im, $x, $y, $color);
  118.  
  119.         // lets calculate some image coordinates
  120.         if ($increm % IMAGE_WIDTH == 0) {
  121.             $y++;
  122.             $x = 0;
  123.         } else {
  124.             $x++;
  125.         }
  126.  
  127.         $increm++;
  128.     }
  129.  
  130.     ob_start();
  131.     imagejpeg($im);
  132.  
  133.     return ob_get_clean();
  134. }
  135.  
  136. // run the script
  137. {
  138.     $original_image = 'nature.jpg';
  139.     $raw_image = 'nature.raw';
  140.     $output_image = 'nature-back.jpg';
  141.  
  142.     echo '<pre>';
  143.     if (!file_exists($original_image)) {
  144.         echo '<b>There is no '.$original_image.' image file</b>';
  145.         exit;
  146.     }
  147.  
  148.     // IMAGE TO RAW
  149.     echo '<b>Transform to raw:</b> <br>';
  150.     echo 'input original_image: '.$original_image.'<br>';
  151.     echo 'input raw_image: '.$raw_image.'<br>';
  152.     file_put_contents($raw_image, jpeg_image_to_raw_file($original_image));
  153.     echo 'filesize raw_image: '.filesize($raw_image).'<br>';
  154.  
  155.     // RAW TO IMAGE
  156.     echo '<br>';
  157.     echo '<b>Transform from raw:</b> <br>';
  158.     echo 'input raw_image: '.$raw_image.'<br>';
  159.     echo 'input output_image: '.$output_image.'<br>';
  160.     file_put_contents($output_image, raw_file_to_jpeg_image($raw_image));
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement