PageRenderTime 42ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/getid3/module.graphic.pcd.php

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