module.graphic.pcd.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /////////////////////////////////////////////////////////////////
  3. /// getID3() by James Heinrich <info@getid3.org> //
  4. // available at https://github.com/JamesHeinrich/getID3 //
  5. // or https://www.getid3.org //
  6. // or http://getid3.sourceforge.net //
  7. // see readme.txt for more details //
  8. /////////////////////////////////////////////////////////////////
  9. // //
  10. // module.graphic.pcd.php //
  11. // module for analyzing PhotoCD (PCD) Image files //
  12. // dependencies: NONE //
  13. // ///
  14. /////////////////////////////////////////////////////////////////
  15. if (!defined('GETID3_INCLUDEPATH')) { // prevent path-exposing attacks that access modules directly on public webservers
  16. exit;
  17. }
  18. class getid3_pcd extends getid3_handler
  19. {
  20. public $ExtractData = 0;
  21. /**
  22. * @return bool
  23. */
  24. public function Analyze() {
  25. $info = &$this->getid3->info;
  26. $info['fileformat'] = 'pcd';
  27. $info['video']['dataformat'] = 'pcd';
  28. $info['video']['lossless'] = false;
  29. $this->fseek($info['avdataoffset'] + 72);
  30. $PCDflags = $this->fread(1);
  31. $PCDisVertical = ((ord($PCDflags) & 0x01) ? true : false);
  32. if ($PCDisVertical) {
  33. $info['video']['resolution_x'] = 3072;
  34. $info['video']['resolution_y'] = 2048;
  35. } else {
  36. $info['video']['resolution_x'] = 2048;
  37. $info['video']['resolution_y'] = 3072;
  38. }
  39. if ($this->ExtractData > 3) {
  40. $this->error('Cannot extract PSD image data for detail levels above BASE (level-3) because encrypted with Kodak-proprietary compression/encryption.');
  41. return false;
  42. } elseif ($this->ExtractData > 0) {
  43. $PCD_levels = array();
  44. $PCD_levels[1] = array( 192, 128, 0x02000); // BASE/16
  45. $PCD_levels[2] = array( 384, 256, 0x0B800); // BASE/4
  46. $PCD_levels[3] = array( 768, 512, 0x30000); // BASE
  47. //$PCD_levels[4] = array(1536, 1024, ??); // BASE*4 - encrypted with Kodak-proprietary compression/encryption
  48. //$PCD_levels[5] = array(3072, 2048, ??); // BASE*16 - encrypted with Kodak-proprietary compression/encryption
  49. //$PCD_levels[6] = array(6144, 4096, ??); // BASE*64 - encrypted with Kodak-proprietary compression/encryption; PhotoCD-Pro only
  50. list($PCD_width, $PCD_height, $PCD_dataOffset) = $PCD_levels[3];
  51. $this->fseek($info['avdataoffset'] + $PCD_dataOffset);
  52. for ($y = 0; $y < $PCD_height; $y += 2) {
  53. // The image-data of these subtypes start at the respective offsets of 02000h, 0b800h and 30000h.
  54. // To decode the YcbYr to the more usual RGB-code, three lines of data have to be read, each
  55. // consisting of ‘w’ bytes, where ‘w’ is the width of the image-subtype. The first ‘w’ bytes and
  56. // the first half of the third ‘w’ bytes contain data for the first RGB-line, the second ‘w’ bytes
  57. // and the second half of the third ‘w’ bytes contain data for a second RGB-line.
  58. $PCD_data_Y1 = $this->fread($PCD_width);
  59. $PCD_data_Y2 = $this->fread($PCD_width);
  60. $PCD_data_Cb = $this->fread(intval(round($PCD_width / 2)));
  61. $PCD_data_Cr = $this->fread(intval(round($PCD_width / 2)));
  62. for ($x = 0; $x < $PCD_width; $x++) {
  63. if ($PCDisVertical) {
  64. $info['pcd']['data'][$PCD_width - $x][$y] = $this->YCbCr2RGB(ord($PCD_data_Y1[$x]), ord($PCD_data_Cb[(int) floor($x / 2)]), ord($PCD_data_Cr[(int) floor($x / 2)]));
  65. $info['pcd']['data'][$PCD_width - $x][$y + 1] = $this->YCbCr2RGB(ord($PCD_data_Y2[$x]), ord($PCD_data_Cb[(int) floor($x / 2)]), ord($PCD_data_Cr[(int) floor($x / 2)]));
  66. } else {
  67. $info['pcd']['data'][$y][$x] = $this->YCbCr2RGB(ord($PCD_data_Y1[$x]), ord($PCD_data_Cb[(int) floor($x / 2)]), ord($PCD_data_Cr[(int) floor($x / 2)]));
  68. $info['pcd']['data'][$y + 1][$x] = $this->YCbCr2RGB(ord($PCD_data_Y2[$x]), ord($PCD_data_Cb[(int) floor($x / 2)]), ord($PCD_data_Cr[(int) floor($x / 2)]));
  69. }
  70. }
  71. }
  72. // Example for plotting extracted data
  73. //getid3_lib::IncludeDependency(GETID3_INCLUDEPATH.'module.audio.ac3.php', __FILE__, true);
  74. //if ($PCDisVertical) {
  75. // $BMPinfo['resolution_x'] = $PCD_height;
  76. // $BMPinfo['resolution_y'] = $PCD_width;
  77. //} else {
  78. // $BMPinfo['resolution_x'] = $PCD_width;
  79. // $BMPinfo['resolution_y'] = $PCD_height;
  80. //}
  81. //$BMPinfo['bmp']['data'] = $info['pcd']['data'];
  82. //getid3_bmp::PlotBMP($BMPinfo);
  83. //exit;
  84. }
  85. return false;
  86. }
  87. /**
  88. * @param int $Y
  89. * @param int $Cb
  90. * @param int $Cr
  91. *
  92. * @return int
  93. */
  94. public function YCbCr2RGB($Y, $Cb, $Cr) {
  95. static $YCbCr_constants = array();
  96. if (empty($YCbCr_constants)) {
  97. $YCbCr_constants['red']['Y'] = 0.0054980 * 256;
  98. $YCbCr_constants['red']['Cb'] = 0.0000000 * 256;
  99. $YCbCr_constants['red']['Cr'] = 0.0051681 * 256;
  100. $YCbCr_constants['green']['Y'] = 0.0054980 * 256;
  101. $YCbCr_constants['green']['Cb'] = -0.0015446 * 256;
  102. $YCbCr_constants['green']['Cr'] = -0.0026325 * 256;
  103. $YCbCr_constants['blue']['Y'] = 0.0054980 * 256;
  104. $YCbCr_constants['blue']['Cb'] = 0.0079533 * 256;
  105. $YCbCr_constants['blue']['Cr'] = 0.0000000 * 256;
  106. }
  107. $RGBcolor = array('red'=>0, 'green'=>0, 'blue'=>0);
  108. foreach ($RGBcolor as $rgbname => $dummy) {
  109. $RGBcolor[$rgbname] = max(0,
  110. min(255,
  111. intval(
  112. round(
  113. ($YCbCr_constants[$rgbname]['Y'] * $Y) +
  114. ($YCbCr_constants[$rgbname]['Cb'] * ($Cb - 156)) +
  115. ($YCbCr_constants[$rgbname]['Cr'] * ($Cr - 137))
  116. )
  117. )
  118. )
  119. );
  120. }
  121. return (($RGBcolor['red'] * 65536) + ($RGBcolor['green'] * 256) + $RGBcolor['blue']);
  122. }
  123. }