module.graphic.pcd.php 5.3 KB

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