View Full Version : DSS->standard format conversion
Hi, I'm looking for any conversion tool written in PHP to convert DDS files (clan/ali crests) to any standard image usable for website (bmp,jpg,gif,png...). Does anyone know about something?
tpuNIsher
19.06.2007, 11:44
I don't know any in php but if u want to view them, u can use nVidia dds viewer / DDS plugin and then convert through photoshop i guess.
humordwarf
19.06.2007, 11:48
I don't know any in php but if u want to view them, u can use nVidia dds viewer / DDS plugin and then convert through photoshop i guess.
how u think how your thing will looks for website that shows clan emblems in top-X clans online real-time ?
tpuNIsher
19.06.2007, 12:05
good question :P didn't think about that.
I don't have that script done cos' i don't know how it's done, yet. But i guess it's do-able.
Of course it is. :)
http://www.lineage2.es/info_clanes.php?server=2
Isnt perfect, have some problems with some colors in the last line... but it works... the original source of that code i get from here somewhere, i cant found the thread... , it was more bugged, i fixed it a little.
this one?
https://postpacific.mmorpg-game.online/forums/showthread.php?t=3555
this one?
https://postpacific.mmorpg-game.online/forums/showthread.php?t=3555
yeah, that is, lol, more 1 year ago, time is fast :(
Is almost the same... but have some small changes... if someone can fix it...
<?
####################
## humordwarf (c) ##
####################
$result = mssql_query(sprintf(SELECT_CLAN_LOGO,$id));
$clan_crest = mssql_fetch_array($result);
$rnd_file = tmpfile();
fwrite($rnd_file, $clan_crest['bitmap']);
fseek($rnd_file, 0);
function readInt($file)
{
$b4 = ord(fgetc($file));
$b3 = ord(fgetc($file));
$b2 = ord(fgetc($file));
$b1 = ord(fgetc($file));
return ($b1<<24)|($b2<<16)|($b3<<8)|$b4;
}
function readShort($file)
{
$b2 = ord(fgetc($file));
$b1 = ord(fgetc($file));
return ($b1<<8)|$b2;
}
$file = &$rnd_file; //fopen($filename,'rb');
$dds = fread($file,4);
if ($dds!=='DDS ') die("Error: no hay imagen DDS");
//DDS header
$hdrSize = readInt($file);
$hdrFlags = readInt($file);
$imgHeight = readInt($file)-4;
$imgWidth = readInt($file);
$imgPitch = readShort($file);
//DXT1 header
fseek($file, 84);
$dxt1 = fread($file,4);
if ($dxt1!=='DXT1') die("Error: no es formato DX1");
//here we go
fseek($file, 128);
header ("Content-type: image/png");
$img=imagecreatetruecolor($imgWidth,$imgHeight);
for ($y=-1; $y<$imgHeight/4; $y++)
{
for ($x=0; $x<$imgWidth/4; $x++)
{
$color0_16 = readShort($file);
$color1_16 = readShort($file);
$r0 = ($color0_16 >> 11) << 3;
$g0 = (($color0_16 >> 5) & 63) << 2;
$b0 = ($color0_16 & 31) << 3;
$r1 = ($color1_16 >> 11) << 3;
$g1 = (($color1_16 >> 5) & 63) << 2;
$b1 = ($color1_16 & 31) << 3;
$color0_32 = imagecolorallocate($img,$r0,$g0,$b0);
$color1_32 = imagecolorallocate($img,$r1,$g1,$b1);
$color01_32 = imagecolorallocate($img,$r0/2+$r1/2,$g0/2+$g1/2,$b0/2+$b1/2);
$black = imagecolorallocate($img,0,0,0);
$data = readInt($file);
for ($yy=0;$yy<4;$yy++)
{
for ($xx=0;$xx<4;$xx++)
{
$bb = $data & 3;
$data = $data >> 2;
switch ($bb)
{
case 0: $c = $color0_32; break;
case 1: $c = $color1_32; break;
case 2: $c = $color01_32; break;
default: $c = $black; break;
}
imagesetpixel($img,$x*4+$xx,$y*4+$yy,$c);
}
}
}
}
imagepng($img);
?>
###############
## humordwarf (c) ##
###############
Thanks its a great share but Fidow where's the define for SELECT_CLAN_LOGO?
Thanks, I'm going to implement it ASAP :)
humordwarf
19.06.2007, 23:59
lol... i forgot about that my thread %)
FidoW: and u forgot to place (c) HD into ur piece of code
:-P
lol... i forgot about that my thread %)
FidoW: and u forgot to place (c) HD into ur piece of code
:-P
Sorry about that, i simple forgot, but is 99,99999% your code... so.. its complety yours :rolleyes: isnt my work at all
Huh, i searched this for months! Thanks a lot!
Mine reworked version (without ugly file using):
function dds_to_image ( $dds_crest ) {
// base work, algorithm done by HD, changes by FidoW, reworked to image by mine
// Validate type
if ( substr ( $dds_crest, 0, 4 ) !== "DDS " ) {
return ( false );
}
// DDS header
list ( , $hdr_size ) = unpack ( "V", substr ( $dds_crest, 4, 4 ) );
list ( , $hdr_flags ) = unpack ( "V", substr ( $dds_crest, 8, 4 ) );
list ( , $img_height ) = unpack ( "V", substr ( $dds_crest, 12, 4 ) );
$img_height -= 4;
list ( , $img_width ) = unpack ( "V", substr ( $dds_crest, 16, 4 ) );
list ( , $img_pitch ) = unpack ( "v", substr ( $dds_crest, 20, 2 ) );
// Validate DXT1
if ( substr ( $dds_crest, 84, 4 ) !== "DXT1" ) {
return ( false );
}
// main code
$dds_ptr = 128;
$img = imagecreatetruecolor ( $img_width, $img_height );
for ( $y = -1; $y < $img_height / 4; $y++ ) {
for ( $x = 0; $x < $img_width / 4; $x++ ) {
list ( , $color0_16 ) = unpack ( "v", substr ( $dds_crest, $dds_ptr, 2 ) );
list ( , $color1_16 ) = unpack ( "v", substr ( $dds_crest, $dds_ptr + 2, 2 ) );
$r0 = ( $color0_16 >> 11 ) << 3;
$g0 = ( ( $color0_16 >> 5 ) & 0x3f ) << 2;
$b0 = ( $color0_16 & 31 ) << 3;
$r1 = ( $color1_16 >> 11 ) << 3;
$g1 = ( ( $color1_16 >> 5 ) & 0x3f ) << 2;
$b1 = ( $color1_16 & 31 ) << 3;
$color0_32 = imagecolorallocate ( $img, $r0, $g0, $b0 );
$color1_32 = imagecolorallocate ( $img, $r1, $g1, $b1 );
$color01_32 = imagecolorallocate ( $img, $r0 / 2 + $r1 / 2, $g0 / 2 + $g1 / 2, $b0 / 2 + $b1 / 2 );
$black = imagecolorallocate ( $img, 0, 0, 0 );
list ( , $data ) = @unpack ( "V", substr ( $dds_crest, $dds_ptr + 4, 4 ) );
for ( $yy = 0; $yy < 4; $yy++ ) {
for ( $xx = 0; $xx < 4; $xx++ ) {
$bb = $data & 3;
$data = $data >> 2;
switch ( $bb ) {
case 0: $c = $color0_32; break;
case 1: $c = $color1_32; break;
case 2: $c = $color01_32; break;
default: $c = $black; break;
}
imagesetpixel ( $img, $x *4 + $xx, $y * 4 + $yy, $c );
}
}
$dds_ptr += 8;
}
}
return ( $img );
}
could be used as:
$query = "select pc.bitmap from pledge p inner join pledge_crest pc on p.crest_id=pc.crest_id where p.pledge_id=" . str_replace ( "'", "''", $_GET [ "c" ] );
$res = mssql_query ( $query, $link_world );
$array_clan_crest = mssql_fetch_array ( $res );
mssql_free_result ( $res );
imagepng ( dds_to_image ( $array_clan_crest [ "bitmap" ] ) );
tpuNIsher
20.06.2007, 18:56
haha this rocks, thanks HD, FiDoW, kenjiro :P