哪位高人帮忙翻译一段代码.......

mdejtod 2010-04-15 04:49:38
RT。翻译成C++的,能翻译为DELPHI的就更好啦

class PhpPsdReader {var $infoArray;

var $fp;

var $fileName;

var $tempFileName;

var $colorBytesLength;


function PhpPsdReader($fileName)
{
set_time_limit(0);

$this->infoArray = array();

$this->fileName = $fileName;

$this->fp = fopen($this->fileName,'r');


if (fread($this->fp,4)=='8BPS')
{

$this->infoArray['version id'] = $this->_getInteger(2);

fseek($this->fp,6,SEEK_CUR); // 6 bytes of 0's

$this->infoArray['channels'] = $this->_getInteger(2);

$this->infoArray['rows'] = $this->_getInteger(4);

$this->infoArray['columns'] = $this->_getInteger(4);

$this->infoArray['colorDepth'] = $this->_getInteger(2);

$this->infoArray['colorMode'] = $this->_getInteger(2);



/* COLOR MODE DATA SECTION */
//4bytes Length The length of the following color data.

$this->infoArray['colorModeDataSectionLength'] = $this->_getInteger(4);

fseek($this->fp,$this->infoArray['colorModeDataSectionLength'],SEEK_CUR);
// ignore this snizzle


/* IMAGE RESOURCES */

$this->infoArray['imageResourcesSectionLength'] = $this->_getInteger(4);

fseek($this->fp,$this->infoArray['imageResourcesSectionLength'],SEEK_CUR);
$this->infoArray['layerMaskDataSectionLength'] = $this->_getInteger(4);

fseek($this->fp,$this->infoArray['layerMaskDataSectionLength'],SEEK_CUR);
$this->infoArray['compressionType'] = $this->_getInteger(2);

$this->infoArray['oneColorChannelPixelBytes'] = $this->infoArray['colorDepth']/8;
$this->colorBytesLength = $this->infoArray['rows']*$this->infoArray['columns']*$this->infoArray ['oneColorChannelPixelBytes'];
if ($this->infoArray['colorMode']==2)
{
$this->infoArray['error'] = 'images with indexed colours are not supported yet';
return false;
}
} else
{
$this->infoArray['error'] = 'invalid or unsupported psd';

return false;

}

}

function getImage()
{
switch($this->infoArray['compressionType'])
{
case 1:
$this->infoArray['scanLinesByteCounts'] = array();
for ($i=0; $i<($this->infoArray['rows']*$this->infoArray['channels']); $i++)
$this->infoArray['scanLinesByteCounts'][] = $this->_getInteger(2); $this->tempFileName = tempnam(realpath('/tmp'),'decompressedImageData');
$tfp = fopen($this->tempFileName,'wb');
foreach ($this->infoArray['scanLinesByteCounts'] as $scanLinesByteCount)
{
fwrite($tfp,$this->_getPackedBitsDecoded(fread($this->fp,$scanLinesByteCount)));
}
fclose($tfp);
fclose($this->fp);
$this->fp = fopen($this->tempFileName,'r');
default:
break;
}
$image = imagecreatetruecolor($this->infoArray['columns'],$this->infoArray['rows']);
for ($rowPointer = 0; ($rowPointer < $this->infoArray['rows']); $rowPointer++)
{
for ($columnPointer = 0; ($columnPointer < $this->infoArray['columns']); $columnPointer++)
{
switch ($this->infoArray['colorMode'])
{

case 2: exit;
break;

case 0:
if ($columnPointer == 0) $bitPointer = 0;
if ($bitPointer==0) $currentByteBits = str_pad(base_convert(bin2hex(fread($this->fp,1)), 16, 2),8,'0',STR_PAD_LEFT);
$r = $g = $b = (($currentByteBits[$bitPointer]=='1')?0:255);
$bitPointer++;
if ($bitPointer==8) $bitPointer = 0;
break;
case 1:
case 8: // 8 is indexed with 1 color..., so grayscale

$r = $g = $b = $this->_getInteger($this->infoArray['oneColorChannelPixelBytes']);
break;
case 4: // CMYK
$c = $this->_getInteger($this->infoArray['oneColorChannelPixelBytes']);
$currentPointerPos = ftell($this->fp)
fseek($this->fp,$this->colorBytesLength-1,SEEK_CUR);
$m = $this->_getInteger($this->infoArray['oneColorChannelPixelBytes']);
fseek($this->fp,$this->colorBytesLength-1,SEEK_CUR);
$y = $this->_getInteger($this->infoArray['oneColorChannelPixelBytes']);
fseek($this->fp,$this->colorBytesLength-1,SEEK_CUR);
$k = $this->_getInteger($this->infoArray['oneColorChannelPixelBytes']);
fseek($this->fp,$currentPointerPos);

$r = round(($c * $k) / (pow(2,$this->infoArray['colorDepth'])-1));
$g = round(($m * $k) / (pow(2,$this->infoArray['colorDepth'])-1));
$b = round(($y * $k) / (pow(2,$this->infoArray['colorDepth'])-1));

break;

case 9: // hunter Lab
// i still need an understandable lab2rgb convert algorithm... if you have one, please let me know!
$l = $this->_getInteger($this->infoArray['oneColorChannelPixelBytes']);
$currentPointerPos = ftell($this->fp);

fseek($this->fp,$this->colorBytesLength-1,SEEK_CUR);
$a = $this->_getInteger($this->infoArray['oneColorChannelPixelBytes']);
fseek($this->fp,$this->colorBytesLength-1,SEEK_CUR);
$b = $this->_getInteger($this->infoArray['oneColorChannelPixelBytes']);
fseek($this->fp,$currentPointerPos);


$r = $l;
$g = $a;

$b = $b;


break;

default:
$r = $this->_getInteger($this->infoArray['oneColorChannelPixelBytes']);
$currentPointerPos = ftell($this->fp);

fseek($this->fp,$this->colorBytesLength-1,SEEK_CUR);

$g = $this->_getInteger($this->infoArray['oneColorChannelPixelBytes']);
fseek($this->fp,$this->colorBytesLength-1,SEEK_CUR);

$b = $this->_getInteger($this->infoArray['oneColorChannelPixelBytes']);
fseek($this->fp,$currentPointerPos);

break;


}


if (($this->infoArray['oneColorChannelPixelBytes']==2))
{

$r = $r >> 8;

$g = $g >> 8;

$b = $b >> 8;

} else if (($this->infoArray['oneColorChannelPixelBytes']==4))
{

$r = $r >> 24;

$g = $g >> 24;

$b = $b >> 24;

}


$pixelColor = imagecolorallocate($image,$r,$g,$b);

imagesetpixel($image,$columnPointer,$rowPointer,$pixelColor);

}

}

fclose($this->fp);

if (isset($this->tempFileName))
unlink($this->tempFileName);

return $image;

}


/**
*
* PRIVATE FUNCTIONS
*
*/


function _getPackedBitsDecoded($string)
{
/*
The PackBits algorithm will precede a block of data with a one byte header n, where n is interpreted as follows:
n Meaning
0 to 127 Copy the next n + 1 symbols verbatim

-127 to -1 Repeat the next symbol 1 - n times

-128 Do nothing


Decoding:
Step 1. Read the block header (n).

Step 2. If the header is an EOF exit.

Step 3. If n is non-negative, copy the next n + 1 symbols to the output stream and go to step 1.
Step 4. If n is negative, write 1 - n copies of the next symbol to the output stream and go to step 1.

*/

$stringPointer = 0;

$returnString = '';


while (1)
{

if (isset($string[$stringPointer]))
$headerByteValue = $this->_unsignedToSigned(hexdec(bin2hex($string[$stringPointer])),1);
else
return $returnString;

$stringPointer++;


if ($headerByteValue >= 0)
{

for ($i=0; $i <= $headerByteValue; $i++)
{

$returnString .= $string[$stringPointer];
$stringPointer++;

}

}
else
{

if ($headerByteValue != -128)
{

$copyByte = $string[$stringPointer];

$stringPointer++;


for ($i=0; $i < (1-$headerByteValue); $i++)
{

$returnString .= $copyByte;

}

}

}

}

}


function _unsignedToSigned($int,$byteSize=1)
{

switch($byteSize)
{

case 1:

if ($int<128)
return $int;

else
return -256+$int;

break;


case 2:

if ($int<32768)
return $int;

else
return -65536+$int;


case 4:

if ($int<2147483648)
return $int;

else
return -4294967296+$int;


default:
return $int;

}

}


function _hexReverse($hex)
{

$output = '';

if (strlen($hex)%2) return false;

for ($pointer = strlen($hex);$pointer>=0;$pointer-=2)
$output .= substr($hex,$pointer,2);

return $output;

}


function _getInteger($byteCount=1)
{

switch ($byteCount)
{

case 4:
// for some strange reason this is still broken...
return @reset(unpack('N',fread($this->fp,4)));

break;


case 2:

return @reset(unpack('n',fread($this->fp,2)));

break;


default:eturn hexdec($this->_hexReverse(bin2hex(fread($this->fp,$byteCount))));

}

}
}

/**
* Returns an image identifier representing the image obtained from the given filename, using only GD, returns an empty string on failure
*
* @param string $fileName
* @return image identifier
*/


function imagecreatefrompsd($fileName)
{

$psdReader = new PhpPsdReader($fileName);

if (isset($psdReader->infoArray['error']))
return '';

else
return $psdReader->getImage();
}
?>
...全文
164 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
mdejtod 2010-04-17
  • 打赏
  • 举报
回复
不是PHP的 ? 那这到底是哪门子语言哪?
我根本没学过PHP
mdejtod 2010-04-16
  • 打赏
  • 举报
回复
读文件的可以不需要翻译,就是其它的,实在不知道怎么回事呢
yiwenyu168 2010-04-16
  • 打赏
  • 举报
回复
晕倒,这个不是C++和PHP代码,请楼主先学好基础再发。
ghostxyz0 2010-04-16
  • 打赏
  • 举报
回复
哥不寫C很久了...
mdejtod 2010-04-16
  • 打赏
  • 举报
回复
是的,我要的就是这个结果,但是有很多东西不知道在C里面怎么翻译。各位行行好吧。我另开贴子加分
helloyou0 2010-04-16
  • 打赏
  • 举报
回复
这里面用了gd库........
床上等您 2010-04-16
  • 打赏
  • 举报
回复
不会 C++ ,学过 delphi ,不过上课都在 CS
jumpheightway 2010-04-16
  • 打赏
  • 举报
回复
这个代码是将psd源文件改成大家都可以看到的图片格式
www_7di_net 2010-04-16
  • 打赏
  • 举报
回复
汗,太长了,本来打算帮你看看的,实在不行你就逐行输出看结果就知道是干什么的了
javaVenice 2010-04-16
  • 打赏
  • 举报
回复
...
C++会一点点、翻译不过来
......
qq369759459 2010-04-16
  • 打赏
  • 举报
回复
这段还挺长的
mdejtod 2010-04-15
  • 打赏
  • 举报
回复
可这个是PHP的代码呀。
recher1114 2010-04-15
  • 打赏
  • 举报
回复
真长啊代码
餅餅 2010-04-15
  • 打赏
  • 举报
回复
你应该去C++板块问
thinkinginAOCP 2010-04-15
  • 打赏
  • 举报
回复
还不如直接在网上找C++ 或是DELPHI的代码...
kingANDqueen2 2010-04-15
  • 打赏
  • 举报
回复
晕,那么长。。。。。
mdejtod 2010-04-15
  • 打赏
  • 举报
回复
代码排版不是很好,各位见谅

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧