PocketMine-MP 5.21.1 git-2ff647079265e7c600203af4fd902b15e99d49a4
MapImage.php
1<?php
2
3/*
4 * This file is part of BedrockProtocol.
5 * Copyright (C) 2014-2022 PocketMine Team <https://github.com/pmmp/BedrockProtocol>
6 *
7 * BedrockProtocol is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 */
12
13declare(strict_types=1);
14
15namespace pocketmine\network\mcpe\protocol\types;
16
22use function count;
23
24final class MapImage{
25 //these limits are enforced in the protocol in 1.20.0
26 public const MAX_HEIGHT = 128;
27 public const MAX_WIDTH = 128;
28
29 private int $width;
30 private int $height;
35 private array $pixels;
36 private ?string $encodedPixelCache = null;
37
42 public function __construct(array $pixels){
43 $rowLength = null;
44 foreach($pixels as $row){
45 if($rowLength === null){
46 $rowLength = count($row);
47 }elseif(count($row) !== $rowLength){
48 throw new \InvalidArgumentException("All rows must have the same number of pixels");
49 }
50 }
51 if($rowLength === null){
52 throw new \InvalidArgumentException("No pixels provided");
53 }
54 if($rowLength > self::MAX_WIDTH){
55 throw new \InvalidArgumentException("Image width must be at most " . self::MAX_WIDTH . " pixels wide");
56 }
57 if(count($pixels) > self::MAX_HEIGHT){
58 throw new \InvalidArgumentException("Image height must be at most " . self::MAX_HEIGHT . " pixels tall");
59 }
60 $this->height = count($pixels);
61 $this->width = $rowLength;
62 $this->pixels = $pixels;
63 }
64
65 public function getWidth() : int{ return $this->width; }
66
67 public function getHeight() : int{ return $this->height; }
68
73 public function getPixels() : array{ return $this->pixels; }
74
75 public function encode(BinaryStream $output) : void{
76 if($this->encodedPixelCache === null){
77 $serializer = new BinaryStream();
78 for($y = 0; $y < $this->height; ++$y){
79 for($x = 0; $x < $this->width; ++$x){
80 //if mojang had any sense this would just be a regular LE int
81 $serializer->putUnsignedVarInt(Binary::flipIntEndianness($this->pixels[$y][$x]->toRGBA()));
82 }
83 }
84 $this->encodedPixelCache = $serializer->getBuffer();
85 }
86
87 $output->put($this->encodedPixelCache);
88 }
89
94 public static function decode(BinaryStream $input, int $height, int $width) : self{
95 if($width > self::MAX_WIDTH){
96 throw new PacketDecodeException("Image width must be at most " . self::MAX_WIDTH . " pixels wide");
97 }
98 if($height > self::MAX_HEIGHT){
99 throw new PacketDecodeException("Image height must be at most " . self::MAX_HEIGHT . " pixels tall");
100 }
101 $pixels = [];
102
103 for($y = 0; $y < $height; ++$y){
104 for($x = 0; $x < $width; ++$x){
105 $pixels[$y][$x] = Color::fromRGBA(Binary::flipIntEndianness($input->getUnsignedVarInt()));
106 }
107 }
108
109 return new self($pixels);
110 }
111}
static decode(BinaryStream $input, int $height, int $width)
Definition: MapImage.php:94