PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
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
17use pmmp\encoding\ByteBufferReader;
18use pmmp\encoding\ByteBufferWriter;
19use pmmp\encoding\DataDecodeException;
20use pmmp\encoding\VarInt;
24use function count;
25
26final class MapImage{
27 //these limits are enforced in the protocol in 1.20.0
28 public const MAX_HEIGHT = 128;
29 public const MAX_WIDTH = 128;
30
31 private int $width;
32 private int $height;
37 private array $pixels;
38 private ?string $encodedPixelCache = null;
39
44 public function __construct(array $pixels){
45 $rowLength = 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");
51 }
52 }
53 if($rowLength === null){
54 throw new \InvalidArgumentException("No pixels provided");
55 }
56 if($rowLength > self::MAX_WIDTH){
57 throw new \InvalidArgumentException("Image width must be at most " . self::MAX_WIDTH . " pixels wide");
58 }
59 if(count($pixels) > self::MAX_HEIGHT){
60 throw new \InvalidArgumentException("Image height must be at most " . self::MAX_HEIGHT . " pixels tall");
61 }
62 $this->height = count($pixels);
63 $this->width = $rowLength;
64 $this->pixels = $pixels;
65 }
66
67 public function getWidth() : int{ return $this->width; }
68
69 public function getHeight() : int{ return $this->height; }
70
75 public function getPixels() : array{ return $this->pixels; }
76
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){
82 //if mojang had any sense this would just be a regular LE int
83 VarInt::writeUnsignedInt($serializer, Binary::flipIntEndianness($this->pixels[$y][$x]->toRGBA()));
84 }
85 }
86 $this->encodedPixelCache = $serializer->getData();
87 }
88
89 $out->writeByteArray($this->encodedPixelCache);
90 }
91
96 public static function decode(ByteBufferReader $in, int $height, int $width) : self{
97 if($width > self::MAX_WIDTH){
98 throw new PacketDecodeException("Image width must be at most " . self::MAX_WIDTH . " pixels wide");
99 }
100 if($height > self::MAX_HEIGHT){
101 throw new PacketDecodeException("Image height must be at most " . self::MAX_HEIGHT . " pixels tall");
102 }
103 $pixels = [];
104
105 for($y = 0; $y < $height; ++$y){
106 $row = [];
107 for($x = 0; $x < $width; ++$x){
108 $row[] = Color::fromRGBA(Binary::flipIntEndianness(VarInt::readUnsignedInt($in)));
109 }
110 $pixels[] = $row;
111 }
112
113 return new self($pixels);
114 }
115}
static decode(ByteBufferReader $in, int $height, int $width)
Definition MapImage.php:96