<commit_before><?php
namespace Fsv\SimpleOcr;

/**
 * Class Bitmap
 * @package Fsv\SimpleOcr
 */
class Bitmap
{
    /**
     * @var int
     */
    protected $width;

    /**
     * @var int
     */
    protected $height;

    /**
     * @var resource
     */
    protected $image;

    /**
     * @var int
     */
    protected $backgroundColorIndex;

    /**
     * @var int
     */
    protected $foregroundColorIndex;

    /**
     * @param $width
     * @param $height
     * @throws Exception
     */
    public function __construct($width, $height)
    {
        $this->width = $width;
        $this->height = $height;
        $this->createImage();
        $this->createPalette();
    }

    /**
     * @throws Exception
     */
    protected function createImage()
    {
        if (false === ($this->image = @imagecreate($this->width, $this->height))) {
            throw new Exception();
        }
    }

    /**
     *
     */
    protected function createPalette()
    {
        $this->backgroundColorIndex = imagecolorallocate($this->image, 255, 255, 255);
        $this->foregroundColorIndex = imagecolorallocate($this->image, 0, 0, 0);
    }

    /**
     * @param $fileName
     * @param int $brightnessThreshold
     * @return Bitmap
     * @throws Exception
     */
    public static function createFromFile(
        $fileName,
        $brightnessThreshold = 128
    ) {
        if (false === ($imageData = @file_get_contents($fileName))) {
            throw new Exception();
        }

        if (false === ($sourceImage = @imagecreatefromstring($imageData))) {
            throw new Exception();
        }

        return self::createFromImage($sourceImage, $brightnessThreshold);
    }

    /**
     * @param resource $sourceImage
     * @param int $brightnessThreshold
     * @return Bitmap
     * @throws Exception
     */
    public static function createFromImage(
        $sourceImage,
        $brightnessThreshold = 128
    ) {
        if (!is_resource($sourceImage)) {
            throw new Exception();
        }

        $bitmap = new self(imagesx($sourceImage), imagesy($sourceImage));

        for ($x = 0; $x < $bitmap->getWidth(); $x++) {
            for ($y = 0; $y < $bitmap->getHeight(); $y++) {
                $rgba = imagecolorsforindex(
                    $sourceImage,
                    imagecolorat($sourceImage, $x, $y)
                );
                $brightness = 0.299 * $rgba['red'] + 0.587 * $rgba['green'] + 0.114 * $rgba['blue'];
                $bitmap->setPixel($x, $y, ($brightness < $brightnessThreshold) ? 1 : 0);
            }
        }

        return $bitmap;
    }

    /**
     * @param Bitmap $sourceBitmap
     * @param int $x
     * @param int $y
     * @param int $width
     * @param int $height
     * @return Bitmap
     * @throws Exception
     */
    public static function createFromBitmap(
        Bitmap $sourceBitmap,
        $x,
        $y,
        $width,
        $height
    ) {
        $bitmap = new Bitmap($width, $height);

        if (false ===
            (@imagecopy(
                $bitmap->getImage(),
                $sourceBitmap->getImage(),
                0,
                0,
                $x,
                $y,
                $width,
                $height
            ))
        ) {
            throw new Exception();
        }

        return $bitmap;
    }

    /**
     * @return int
     */
    public function getWidth()
    {
        return $this->width;
    }

    /**
     * @return int
     */
    public function getHeight()
    {
        return $this->height;
    }

    /**
     * @return resource
     */
    public function getImage()
    {
        return $this->image;
    }

    /**
     * @param $x
     * @param $y
     * @param $value<commit_msg>Move some methods to BitmapFactory class<commit_after><?php
namespace Fsv\SimpleOcr;

/**
 * Class Bitmap
 * @package Fsv\SimpleOcr
 */
class Bitmap
{
    /**
     * @var int
     */
    protected $width;

    /**
     * @var int
     */
    protected $height;

    /**
     * @var resource
     */
    protected $image;

    /**
     * @var int
     */
    protected $backgroundColorIndex;

    /**
     * @var int
     */
    protected $foregroundColorIndex;

    /**
     * @param $width
     * @param $height
     * @throws Exception
     */
    public function __construct($width, $height)
    {
        $this->width = $width;
        $this->height = $height;
        $this->createImage();
        $this->createPalette();
    }

    /**
     * @throws Exception
     */
    protected function createImage()
    {
        if (false === ($this->image = @imagecreate($this->width, $this->height))) {
            throw new Exception();
        }
    }

    /**
     *
     */
    protected function createPalette()
    {
        $this->backgroundColorIndex = imagecolorallocate($this->image, 255, 255, 255);
        $this->foregroundColorIndex = imagecolorallocate($this->image, 0, 0, 0);
    }

    /**
     * @return int
     */
    public function getWidth()
    {
        return $this->width;
    }

    /**
     * @return int
     */
    public function getHeight()
    {
        return $this->height;
    }

    /**
     * @return resource
     */
    public function getImage()
    {
        return $this->image;
    }

    /**
     * @param $x
     * @param $y
     * @param $value
{'fraction_non_alphanumeric': 0.14227111691217603, 'fraction_numerical': 0.008950214432220773, 'mean_word_length': 1.5677357587362375, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5646542', 'n_tokens_mistral': 1650, 'n_tokens_neox': 1552, 'n_words': 341}