View Single Post
Old 20.06.2007, 08:56   #12
Kenjiro
Архивный участник
 
Join Date: Apr 2006
Posts: 120
Default

Huh, i searched this for months! Thanks a lot!
Mine reworked version (without ugly file using):
PHP Code:
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:
PHP Code:
$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" ] ) ); 
Kenjiro is offline