28    public const MAX_HEIGHT = 128;
 
   29    public const MAX_WIDTH = 128;
 
   37    private array $pixels;
 
   38    private ?
string $encodedPixelCache = 
null;
 
   46        foreach($pixels as $row){
 
   47            if($rowLength === 
null){
 
   48                $rowLength = count($row);
 
   49            }elseif(count($row) !== $rowLength){
 
   50                throw new \InvalidArgumentException(
"All rows must have the same number of pixels");
 
   53        if($rowLength === 
null){
 
   54            throw new \InvalidArgumentException(
"No pixels provided");
 
   56        if($rowLength > self::MAX_WIDTH){
 
   57            throw new \InvalidArgumentException(
"Image width must be at most " . self::MAX_WIDTH . 
" pixels wide");
 
   59        if(count($pixels) > self::MAX_HEIGHT){
 
   60            throw new \InvalidArgumentException(
"Image height must be at most " . self::MAX_HEIGHT . 
" pixels tall");
 
   62        $this->height = count($pixels);
 
   63        $this->width = $rowLength;
 
   64        $this->pixels = $pixels;
 
 
   67    public function getWidth() : int{ return $this->width; }
 
   69    public function getHeight() : int{ return $this->height; }
 
   75    public function getPixels() : array{ return $this->pixels; }
 
   77    public function encode(ByteBufferWriter $out) : void{
 
   78        if($this->encodedPixelCache === null){
 
   79            $serializer = 
new ByteBufferWriter();
 
   80            for($y = 0; $y < $this->height; ++$y){
 
   81                for($x = 0; $x < $this->width; ++$x){
 
   83                    VarInt::writeUnsignedInt($serializer, Binary::flipIntEndianness($this->pixels[$y][$x]->toRGBA()));
 
   86            $this->encodedPixelCache = $serializer->getData();
 
   89        $out->writeByteArray($this->encodedPixelCache);
 
   96    public static function decode(ByteBufferReader $in, 
int $height, 
int $width) : self{
 
   97        if($width > self::MAX_WIDTH){
 
  100        if($height > self::MAX_HEIGHT){
 
  101            throw new PacketDecodeException(
"Image height must be at most " . self::MAX_HEIGHT . 
" pixels tall");
 
  105        for($y = 0; $y < $height; ++$y){
 
  107            for($x = 0; $x < $width; ++$x){
 
  108                $row[] = Color::fromRGBA(Binary::flipIntEndianness(VarInt::readUnsignedInt($in)));
 
  113        return new self($pixels);