gpt4 book ai didi

php - 将 32 位 TGA 转换为 PNG

转载 作者:可可西里 更新时间:2023-10-31 23:20:12 25 4
gpt4 key购买 nike

我在 this code 工作并考虑到为 32 位“修复”它的注释,但它似乎仍然不起作用。我很确定它与 TGA descriptor 有关.这将使用 0-3 位作为 alpha channel 深度,对于 32 位,它始终为 8,并且代码没有考虑到这一点。

我试图了解如何使用 this C code 将其拼凑在一起作为指南,但运气不好。

一旦你考虑到像素长度为 4(根据评论中的补丁),他的 dwordize 似乎只占 4 个字节中的 3 个,第 4 个字节是 alpha我认为的位。

我试着改变函数从

function dwordize($str)
{
$a = ord($str[0]);
$b = ord($str[1]);
$c = ord($str[2]);
return $c*256*256 + $b*256 + $a;
}

function dwordize($str)
{
$a = ord($str[0]);
$b = ord($str[1]);
$c = ord($str[2]);
$d = ord($str[3]);
return $d*256*256*256 + $c*256*256 + $b*256 + $a;
}

这个不行,然后试了

function dwordize($str)
{
$a = ord($str[0]);
$b = ord($str[1]);
$c = ord($str[2]);
$d = ord($str[3]);
return $c*256*256 + $b*256 + $a + $d*256*256*256;
}

所有这些我都试图摆脱从 RBGA 到 BGRA 的 C 代码,然后索引都很奇怪。我真的不太了解 C 代码,无法将其应用于 PHP 代码。

我还找到了this site这可能会有所帮助,我正在阅读它,如果我有任何想法,我会更新。

最佳答案

我找到了您的解决方案。在 RLE 解码函数中缺少需要进行的调整以支持 32 位像素格式:即:AARRGGBB

因此,首先我们需要按照评论中的建议绕过您的库的颜色深度检查,因此我们在 99 行将检查更改为此

if ($header['pixel_size'] != 24 && $header['pixel_size'] != 32) {
die('Unsupported TGA color depth');
}

然后我们需要更改一些与颜色深度相关的变量。它们中的大多数将用于数据格式化,因此无论颜色深度如何,我们都需要它们具有正确的值。那将是:

104 : $bytes = $header['pixel_size'] / 8;

117 : $size = $header['width'] * $header['height'] * $bytes;

154 : $pixels = str_split($data, $bytes);

并删除行 153 : $num_bytes = $header['pixel_size']/8;我们不再需要它了。


然后我们需要 rle_decode()知道像素格式大小的函数,所以 int for当我们调用它时循环我们需要传递这个参数,所以我们还需要更改以下行:

141 : $data = rle_decode($data, $size, $bytes);

因此函数原型(prototype)变为:

9 : function rle_decode($data, $datalen, $pixel_size)

现在你可以看到这个函数有多个3 魔数(Magic Number)(即:for ($j = 0; $j<3*$value; $j++) 第 29 行)。所有这些都必须替换为像素大小参数。 (3 代表 24 bits4 代表 32 bits)

此外,有时在创建像素时它只会写入 3 字节而不是 4 因此我们必须调整也是如此。

所以最后这给了我们以下算法:

function rle_decode($data, $datalen, $pixel_size)
{
$len = strlen($data);

$out = '';

$i = 0;
$k = 0;
while ($i<$len)
{
dec_bits(ord($data[$i]), $type, $value);
if ($k >= $datalen)
{
break;
}

$i++;

if ($type == 0) //raw
{
for ($j=0; $j<$pixel_size*$value; $j++)
{
$out .= $data[$j+$i];
$k++;
}
$i += $value*$pixel_size;
}
else //rle
{
for ($j=0; $j<$value; $j++)
{
$out .= $data[$i] . $data[$i+1] . $data[$i+2];
if ($pixel_size == 4) $out .= $data[$i+3];
$k++;
}
$i += $pixel_size;
}
}
return $out;
}

就是这样,您现在将获得完美转换的 TGA 图像。我给你完整的 tga.php代码,所以你可以直接粘贴并自己测试:

<?php

// Author: de77
// Licence: MIT
// First-version: 9.02.2010
// Version: 24.08.2010
// http://de77.com

function rle_decode($data, $datalen, $pixel_size)
{
$len = strlen($data);

$out = '';

$i = 0;
$k = 0;
while ($i<$len)
{
dec_bits(ord($data[$i]), $type, $value);
if ($k >= $datalen)
{
break;
}

$i++;

if ($type == 0) //raw
{
for ($j=0; $j<$pixel_size*$value; $j++)
{
$out .= $data[$j+$i];
$k++;
}
$i += $value*$pixel_size;
}
else //rle
{
for ($j=0; $j<$value; $j++)
{
$out .= $data[$i] . $data[$i+1] . $data[$i+2];
if ($pixel_size == 4) $out .= $data[$i+3];
$k++;
}
$i += $pixel_size;
}
}
return $out;
}

function dec_bits($byte, &$type, &$value)
{
$type = ($byte & 0x80) >> 7;
$value = 1 + ($byte & 0x7F);
}

function getimagesizetga($filename)
{
$f = fopen($filename, 'rb');
$header = fread($f, 18);
$header = @unpack( "cimage_id_len/ccolor_map_type/cimage_type/vcolor_map_origin/vcolor_map_len/" .
"ccolor_map_entry_size/vx_origin/vy_origin/vwidth/vheight/" .
"cpixel_size/cdescriptor", $header);
fclose($f);

$types = array(0,1,2,3,9,10,11,32,33);
if (!in_array($header['image_type'], $types))
{
return array(0, 0, 0, 0, 0);
}

if ($header['pixel_size'] > 32)
{
return array(0, 0, 0, 0, 0);
}

return array($header['width'], $header['height'], 'tga', $header['pixel_size'], $header['image_type']);
}

function imagecreatefromtga($filename)
{
$f = fopen($filename, 'rb');
if (!$f)
{
return false;
}
$header = fread($f, 18);
$header = unpack( "cimage_id_len/ccolor_map_type/cimage_type/vcolor_map_origin/vcolor_map_len/" .
"ccolor_map_entry_size/vx_origin/vy_origin/vwidth/vheight/" .
"cpixel_size/cdescriptor", $header);

switch ($header['image_type'])
{
case 2: echo "Image is not compressed\n";//no palette, uncompressed
case 10: //no palette, rle
break;
default: die('Unsupported TGA format');
}

if ($header['pixel_size'] != 24 && $header['pixel_size'] != 32)
{
die('Unsupported TGA color depth');
}

$bytes = $header['pixel_size'] / 8;

if ($header['image_id_len'] > 0)
{
$header['image_id'] = fread($f, $header['image_id_len']);
}
else
{
$header['image_id'] = '';
}

$im = imagecreatetruecolor($header['width'], $header['height']);

$size = $header['width'] * $header['height'] * $bytes;

//-- check whether this is NEW TGA or not
$pos = ftell($f);
fseek($f, -26, SEEK_END);
$newtga = fread($f, 26);
if (substr($newtga, 8, 16) != 'TRUEVISION-XFILE')
{
$newtga = false;
}

fseek($f, 0, SEEK_END);
$datasize = ftell($f) - $pos;
if ($newtga)
{
$datasize -= 26;
}

fseek($f, $pos, SEEK_SET);

//-- end of check
$data = fread($f, $datasize);
if ($header['image_type'] == 10)
{
$data = rle_decode($data, $size, $bytes);
}
if (bit5($header['descriptor']) == 1)
{
$reverse = true;
}
else
{
$reverse = false;
}


$i = 0;
$pixels = str_split($data, $bytes);

//read pixels
if ($reverse)
{
for ($y=0; $y<$header['height']; $y++)
{
for ($x=0; $x<$header['width']; $x++)
{
imagesetpixel($im, $x, $y, dwordize($pixels[$i]));
$i++;
}
}
}
else
{
for ($y=$header['height']-1; $y>=0; $y--)
{
for ($x=0; $x<$header['width']; $x++)
{
imagesetpixel($im, $x, $y, dwordize($pixels[$i]));
$i++;
}
}
}
fclose($f);

return $im;
}

function dwordize($str)
{
$a = ord($str[0]);
$b = ord($str[1]);
$c = ord($str[2]);
return $c*256*256 + $b*256 + $a;
}

function bit5($x)
{
return ($x & 32) >> 5;
}

这是输出 PNG 直接从脚本创建的图像: Eyebrows PNG output

如果您更喜欢直接下载链接而不是粘贴固定代码,我会在此处为您提供完整的 php文件:https://www.sendspace.com/file/92uir9

在一天结束时,只需更改 tga.php 文件,一切都会自动运行。

关于php - 将 32 位 TGA 转换为 PNG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24709142/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com