|
17 | 17 | # You should have received a copy of the GNU General Public License |
18 | 18 | # along with this program. If not, see <https://www.gnu.org/licenses/>. |
19 | 19 |
|
20 | | -import struct |
21 | | - |
22 | 20 | from serial.tools.list_ports import comports |
23 | | -import numpy as np |
24 | 21 |
|
25 | 22 | from library.lcd.lcd_comm import * |
| 23 | +from library.lcd.serialize import image_to_RGB565, chunked |
26 | 24 | from library.log import logger |
27 | 25 |
|
28 | 26 |
|
@@ -195,38 +193,12 @@ def SetOrientation(self, orientation: Orientation = Orientation.PORTRAIT): |
195 | 193 | else: |
196 | 194 | self.SendCommand(Command.SET_ORIENTATION, payload=[OrientationValueRevB.ORIENTATION_LANDSCAPE]) |
197 | 195 |
|
198 | | - @staticmethod |
199 | | - def imageToRGB565BE(image: Image.Image): |
200 | | - if image.mode not in ["RGB", "RGBA"]: |
201 | | - # we need the first 3 channels to be R, G and B |
202 | | - image = image.convert("RGB") |
203 | | - |
204 | | - rgb = np.asarray(image) |
205 | | - |
206 | | - # flatten the first 2 dimensions (width and height) into a single stream |
207 | | - # of RGB pixels |
208 | | - rgb = rgb.reshape((image.size[1] * image.size[0], -1)) |
209 | | - |
210 | | - # extract R, G, B channels and promote them to 16 bits |
211 | | - r = rgb[:, 0].astype(np.uint16) |
212 | | - g = rgb[:, 1].astype(np.uint16) |
213 | | - b = rgb[:, 2].astype(np.uint16) |
214 | | - |
215 | | - # construct RGB565 |
216 | | - r = (r >> 3) |
217 | | - g = (g >> 2) |
218 | | - b = (b >> 3) |
219 | | - rgb565 = (r << 11) | (g << 5) | b |
220 | | - |
221 | | - # serialize to big-endian |
222 | | - return rgb565.astype('>u2').tobytes() |
223 | | - |
224 | 196 | def serialize_image(self, image: Image.Image, height: int, width: int) -> bytes: |
225 | 197 | if image.width != width or image.height != height: |
226 | 198 | image = image.crop((0, 0, width, height)) |
227 | 199 | if self.orientation == Orientation.REVERSE_PORTRAIT or self.orientation == Orientation.REVERSE_LANDSCAPE: |
228 | 200 | image = image.rotate(180) |
229 | | - return self.imageToRGB565BE(image) |
| 201 | + return image_to_RGB565(image, "big") |
230 | 202 |
|
231 | 203 | def DisplayPILImage( |
232 | 204 | self, |
@@ -271,12 +243,5 @@ def DisplayPILImage( |
271 | 243 | # Lock queue mutex then queue all the requests for the image data |
272 | 244 | with self.update_queue_mutex: |
273 | 245 | # Send image data by multiple of "display width" bytes |
274 | | - start = 0 |
275 | | - end = self.get_width() * 8 |
276 | | - while end <= len(rgb565be): |
277 | | - self.SendLine(rgb565be[start:end]) |
278 | | - start, end = end, end + self.get_width() * 8 |
279 | | - |
280 | | - # Write last line if needed |
281 | | - if start != len(rgb565be): |
282 | | - self.SendLine(rgb565be[start:]) |
| 246 | + for chunk in chunked(rgb565be, self.get_width() * 8): |
| 247 | + self.SendLine(chunk) |
0 commit comments