Example #1
0
function ReadIcon($filename, $id, &$Ikona)
{
    global $CurrentBit;
    $f = fopen($filename, "rb");
    fseek($f, 6 + $id * 16);
    $Width = freadbyte($f);
    $Height = freadbyte($f);
    fseek($f, 6 + $id * 16 + 12);
    $OffSet = freaddword($f);
    fseek($f, $OffSet);
    $p = $id;
    $Ikona[$p]["Info"]["HeaderSize"] = freadlngint($f);
    $Ikona[$p]["Info"]["ImageWidth"] = freadlngint($f);
    $Ikona[$p]["Info"]["ImageHeight"] = freadlngint($f);
    $Ikona[$p]["Info"]["NumberOfImagePlanes"] = freadword($f);
    $Ikona[$p]["Info"]["BitsPerPixel"] = freadword($f);
    $Ikona[$p]["Info"]["CompressionMethod"] = freadlngint($f);
    $Ikona[$p]["Info"]["SizeOfBitmap"] = freadlngint($f);
    $Ikona[$p]["Info"]["HorzResolution"] = freadlngint($f);
    $Ikona[$p]["Info"]["VertResolution"] = freadlngint($f);
    $Ikona[$p]["Info"]["NumColorUsed"] = freadlngint($f);
    $Ikona[$p]["Info"]["NumSignificantColors"] = freadlngint($f);
    $biBitCount = $Ikona[$p]["Info"]["BitsPerPixel"];
    if ($Ikona[$p]["Info"]["BitsPerPixel"] <= 8) {
        $barev = pow(2, $biBitCount);
        for ($b = 0; $b < $barev; $b++) {
            $Ikona[$p]["Paleta"][$b]["b"] = freadbyte($f);
            $Ikona[$p]["Paleta"][$b]["g"] = freadbyte($f);
            $Ikona[$p]["Paleta"][$b]["r"] = freadbyte($f);
            freadbyte($f);
        }
        $Zbytek = (4 - ceil($Width / (8 / $biBitCount)) % 4) % 4;
        for ($y = $Height - 1; $y >= 0; $y--) {
            $CurrentBit = 0;
            for ($x = 0; $x < $Width; $x++) {
                $C = freadbits($f, $biBitCount);
                $Ikona[$p]["Data"][$x][$y] = $C;
            }
            if ($CurrentBit != 0) {
                freadbyte($f);
            }
            for ($g = 0; $g < $Zbytek; $g++) {
                freadbyte($f);
            }
        }
    } elseif ($biBitCount == 24) {
        $Zbytek = $Width % 4;
        for ($y = $Height - 1; $y >= 0; $y--) {
            for ($x = 0; $x < $Width; $x++) {
                $B = freadbyte($f);
                $G = freadbyte($f);
                $R = freadbyte($f);
                $Ikona[$p]["Data"][$x][$y]["r"] = $R;
                $Ikona[$p]["Data"][$x][$y]["g"] = $G;
                $Ikona[$p]["Data"][$x][$y]["b"] = $B;
            }
            for ($z = 0; $z < $Zbytek; $z++) {
                freadbyte($f);
            }
        }
    } elseif ($biBitCount == 32) {
        $Zbytek = $Width % 4;
        for ($y = $Height - 1; $y >= 0; $y--) {
            for ($x = 0; $x < $Width; $x++) {
                $B = freadbyte($f);
                $G = freadbyte($f);
                $R = freadbyte($f);
                $Alpha = freadbyte($f);
                $Ikona[$p]["Data"][$x][$y]["r"] = $R;
                $Ikona[$p]["Data"][$x][$y]["g"] = $G;
                $Ikona[$p]["Data"][$x][$y]["b"] = $B;
                $Ikona[$p]["Data"][$x][$y]["alpha"] = $Alpha;
            }
            for ($z = 0; $z < $Zbytek; $z++) {
                freadbyte($f);
            }
        }
    }
    //Maska
    $Zbytek = (4 - ceil($Width / 8) % 4) % 4;
    for ($y = $Height - 1; $y >= 0; $y--) {
        $CurrentBit = 0;
        for ($x = 0; $x < $Width; $x++) {
            $C = freadbits($f, 1);
            $Ikona[$p]["Maska"][$x][$y] = $C;
        }
        if ($CurrentBit != 0) {
            freadbyte($f);
        }
        for ($g = 0; $g < $Zbytek; $g++) {
            freadbyte($f);
        }
    }
    //--------------
    fclose($f);
}
Example #2
0
function imagecreatefrombmp($file)
{
    global $CurrentBit, $echoMode;
    $f = fopen($file, "r");
    $Header = fread($f, 2);
    if ($Header == "BM") {
        $Size = freaddword($f);
        $Reserved1 = freadword($f);
        $Reserved2 = freadword($f);
        $FirstByteOfImage = freaddword($f);
        $SizeBITMAPINFOHEADER = freaddword($f);
        $Width = freaddword($f);
        $Height = freaddword($f);
        $biPlanes = freadword($f);
        $biBitCount = freadword($f);
        $RLECompression = freaddword($f);
        $WidthxHeight = freaddword($f);
        $biXPelsPerMeter = freaddword($f);
        $biYPelsPerMeter = freaddword($f);
        $NumberOfPalettesUsed = freaddword($f);
        $NumberOfImportantColors = freaddword($f);
        if ($biBitCount < 24) {
            $img = imagecreate($Width, $Height);
            $Colors = pow(2, $biBitCount);
            for ($p = 0; $p < $Colors; $p++) {
                $B = freadbyte($f);
                $G = freadbyte($f);
                $R = freadbyte($f);
                $Reserved = freadbyte($f);
                $Palette[] = imagecolorallocate($img, $R, $G, $B);
            }
            if ($RLECompression == 0) {
                $Zbytek = (4 - ceil($Width / (8 / $biBitCount)) % 4) % 4;
                for ($y = $Height - 1; $y >= 0; $y--) {
                    $CurrentBit = 0;
                    for ($x = 0; $x < $Width; $x++) {
                        $C = freadbits($f, $biBitCount);
                        imagesetpixel($img, $x, $y, $Palette[$C]);
                    }
                    if ($CurrentBit != 0) {
                        freadbyte($f);
                    }
                    for ($g = 0; $g < $Zbytek; $g++) {
                        freadbyte($f);
                    }
                }
            }
        }
        if ($RLECompression == 1) {
            $y = $Height;
            $pocetb = 0;
            while (true) {
                $y--;
                $prefix = freadbyte($f);
                $suffix = freadbyte($f);
                $pocetb += 2;
                $echoit = false;
                if ($echoit) {
                    echo "Prefix: {$prefix} Suffix: {$suffix}<BR>";
                }
                if ($prefix == 0 and $suffix == 1) {
                    break;
                }
                if (feof($f)) {
                    break;
                }
                while (!($prefix == 0 and $suffix == 0)) {
                    if ($prefix == 0) {
                        $pocet = $suffix;
                        $Data .= fread($f, $pocet);
                        $pocetb += $pocet;
                        if ($pocetb % 2 == 1) {
                            freadbyte($f);
                            $pocetb++;
                        }
                    }
                    if ($prefix > 0) {
                        $pocet = $prefix;
                        for ($r = 0; $r < $pocet; $r++) {
                            $Data .= chr($suffix);
                        }
                    }
                    $prefix = freadbyte($f);
                    $suffix = freadbyte($f);
                    $pocetb += 2;
                    if ($echoit) {
                        echo "Prefix: {$prefix} Suffix: {$suffix}<BR>";
                    }
                }
                for ($x = 0; $x < strlen($Data); $x++) {
                    imagesetpixel($img, $x, $y, $Palette[ord($Data[$x])]);
                }
                $Data = "";
            }
        }
        if ($RLECompression == 2) {
            $y = $Height;
            $pocetb = 0;
            /*while(!feof($f))
              echo freadbyte($f)."_".freadbyte($f)."<BR>";*/
            while (true) {
                //break;
                $y--;
                $prefix = freadbyte($f);
                $suffix = freadbyte($f);
                $pocetb += 2;
                $echoit = false;
                if ($echoit) {
                    echo "Prefix: {$prefix} Suffix: {$suffix}<BR>";
                }
                if ($prefix == 0 and $suffix == 1) {
                    break;
                }
                if (feof($f)) {
                    break;
                }
                while (!($prefix == 0 and $suffix == 0)) {
                    if ($prefix == 0) {
                        $pocet = $suffix;
                        $CurrentBit = 0;
                        for ($h = 0; $h < $pocet; $h++) {
                            $Data .= chr(freadbits($f, 4));
                        }
                        if ($CurrentBit != 0) {
                            freadbits($f, 4);
                        }
                        $pocetb += ceil($pocet / 2);
                        if ($pocetb % 2 == 1) {
                            freadbyte($f);
                            $pocetb++;
                        }
                    }
                    if ($prefix > 0) {
                        $pocet = $prefix;
                        $i = 0;
                        for ($r = 0; $r < $pocet; $r++) {
                            if ($i % 2 == 0) {
                                $Data .= chr($suffix % 16);
                            } else {
                                $Data .= chr(floor($suffix / 16));
                            }
                            $i++;
                        }
                    }
                    $prefix = freadbyte($f);
                    $suffix = freadbyte($f);
                    $pocetb += 2;
                    if ($echoit) {
                        echo "Prefix: {$prefix} Suffix: {$suffix}<BR>";
                    }
                }
                for ($x = 0; $x < strlen($Data); $x++) {
                    imagesetpixel($img, $x, $y, $Palette[ord($Data[$x])]);
                }
                $Data = "";
            }
        }
        if ($biBitCount == 24) {
            $img = imagecreatetruecolor($Width, $Height);
            $Zbytek = $Width % 4;
            for ($y = $Height - 1; $y >= 0; $y--) {
                for ($x = 0; $x < $Width; $x++) {
                    $B = freadbyte($f);
                    $G = freadbyte($f);
                    $R = freadbyte($f);
                    $color = imagecolorexact($img, $R, $G, $B);
                    if ($color == -1) {
                        $color = imagecolorallocate($img, $R, $G, $B);
                    }
                    imagesetpixel($img, $x, $y, $color);
                }
                for ($z = 0; $z < $Zbytek; $z++) {
                    freadbyte($f);
                }
            }
        }
        return $img;
    }
    fclose($f);
}
Example #3
0
function imagecreatefromani($filename, $imageid)
{
    $Info = ReadAniInfo($filename);
    $f = fopen($filename, "r");
    fseek($f, $Info["Icon"][$imageid]);
    $IconSize = freaddword($f);
    $Reserved = freadword($f);
    $Type = freadword($f);
    $Count = freadword($f);
    $Ikona["Width"] = freadbyte($f);
    $Ikona["Height"] = freadbyte($f);
    $Ikona["ColorCount"] = freadword($f);
    if ($Ikona["ColorCount"] == 0) {
        $Ikona["ColorCount"] = 256;
    }
    $Ikona["Planes"] = freadword($f);
    $Ikona["BitCount"] = freadword($f);
    $Ikona["BytesInRes"] = freaddword($f);
    $Ikona["ImageOffset"] = freaddword($f);
    $Ikona["Info"]["HeaderSize"] = freadlngint($f);
    $Ikona["Info"]["ImageWidth"] = freadlngint($f);
    $Ikona["Info"]["ImageHeight"] = freadlngint($f);
    $Ikona["Info"]["NumberOfImagePlanes"] = freadword($f);
    $Ikona["Info"]["BitsPerPixel"] = freadword($f);
    $Ikona["Info"]["CompressionMethod"] = freadlngint($f);
    $Ikona["Info"]["SizeOfBitmap"] = freadlngint($f);
    $Ikona["Info"]["HorzResolution"] = freadlngint($f);
    $Ikona["Info"]["VertResolution"] = freadlngint($f);
    $Ikona["Info"]["NumColorUsed"] = freadlngint($f);
    $Ikona["Info"]["NumSignificantColors"] = freadlngint($f);
    $biBitCount = $Ikona["Info"]["BitsPerPixel"];
    $Width = $Ikona["Width"];
    $Height = $Ikona["Height"];
    $img = imagecreatetruecolor($Ikona["Width"], $Ikona["Height"]);
    if ($biBitCount <= 8) {
        $barev = pow(2, $biBitCount);
        for ($b = 0; $b < $barev; $b++) {
            $B = freadbyte($f);
            $G = freadbyte($f);
            $R = freadbyte($f);
            $Palette[] = imagecolorallocate($img, $R, $G, $B);
            freadbyte($f);
        }
        $Zbytek = (4 - ceil($Width / (8 / $biBitCount)) % 4) % 4;
        for ($y = $Height - 1; $y >= 0; $y--) {
            $CurrentBit = 0;
            for ($x = 0; $x < $Width; $x++) {
                $C = freadbits($f, $biBitCount);
                imagesetpixel($img, $x, $y, $Palette[$C]);
            }
            if ($CurrentBit != 0) {
                freadbyte($f);
            }
            for ($g = 0; $g < $Zbytek; $g++) {
                freadbyte($f);
            }
        }
    } elseif ($biBitCount == 24) {
        $Zbytek = $Width % 4;
        for ($y = $Height - 1; $y >= 0; $y--) {
            for ($x = 0; $x < $Width; $x++) {
                $B = freadbyte($f);
                $G = freadbyte($f);
                $R = freadbyte($f);
                $color = imagecolorexact($img, $R, $G, $B);
                if ($color == -1) {
                    $color = imagecolorallocate($img, $R, $G, $B);
                }
                imagesetpixel($img, $x, $y, $color);
            }
            for ($z = 0; $z < $Zbytek; $z++) {
                freadbyte($f);
            }
        }
    } elseif ($biBitCount == 32) {
        $Zbytek = $Width % 4;
        for ($y = $Height - 1; $y >= 0; $y--) {
            for ($x = 0; $x < $Width; $x++) {
                $B = freadbyte($f);
                $G = freadbyte($f);
                $R = freadbyte($f);
                $Alpha = freadbyte($f);
                $color = imagecolorexactalpha($img, $R, $G, $B, $Alpha);
                if ($color == -1) {
                    $color = imagecolorallocatealpha($img, $R, $G, $B, $Alpha);
                }
                imagesetpixel($img, $x, $y, $color);
            }
            for ($z = 0; $z < $Zbytek; $z++) {
                freadbyte($f);
            }
        }
    }
    //Maska
    $Zbytek = (4 - ceil($Width / 8) % 4) % 4;
    for ($y = $Height - 1; $y >= 0; $y--) {
        $CurrentBit = 0;
        for ($x = 0; $x < $Width; $x++) {
            $C = freadbits($f, 1);
            if ($C == 1) {
                if (!$IsTransparent) {
                    if ($biBitCount >= 24 or imagecolorstotal($img) >= 256 or imagecolorstotal($img) == 0) {
                        $img2 = imagecreatetruecolor(imagesx($img), imagesy($img));
                        imagecopy($img2, $img, 0, 0, 0, 0, imagesx($img), imagesy($img));
                        imagedestroy($img);
                        $img = $img2;
                        imagetruecolortopalette($img, true, 255);
                    }
                    $Pruhledna = imagecolorallocate($img, 0, 0, 0);
                }
                $IsTransparent = true;
                imagesetpixel($img, $x, $y, $Pruhledna);
            }
        }
        if ($CurrentBit != 0) {
            freadbyte($f);
        }
        for ($g = 0; $g < $Zbytek; $g++) {
            freadbyte($f);
        }
    }
    if ($IsTransparent) {
        imagecolortransparent($img, $Pruhledna);
    }
    fclose($f);
    return $img;
}