Lines Matching refs:self

52     def __str__(self):  argument
53 return self.__class__.__name__ + ': ' + ' '.join(self.args)
74 def __init__(self, ncolors=256, dither=True, exec_path="") -> None: argument
76 self.cmd = (f"{executable} {'--nofs' if not dither else ''} "
79 def convert(self, filename) -> bytes: argument
126 def bpp(self) -> int: argument
148 return cf_map[self] if self in cf_map else 0
151 def ncolors(self) -> int: argument
163 return cf_map.get(self, 0)
166 def is_indexed(self) -> bool: argument
170 return self.ncolors != 0
173 def is_alpha_only(self) -> bool: argument
174 return ColorFormat.A1.value <= self.value <= ColorFormat.A8.value
177 def has_alpha(self) -> bool: argument
178 return self.is_alpha_only or self.is_indexed or self in (
185 def is_colormap(self) -> bool: argument
186 return self in (ColorFormat.ARGB8888, ColorFormat.RGB888,
191 def is_luma_only(self) -> bool: argument
192 return self in (ColorFormat.L8, )
390 def __init__(self, argument
397 self.cf = cf
398 self.flags = flags
399 self.w = w & 0xffff
400 self.h = h & 0xffff
407 self.stride = self.stride_align(align) if stride == 0 else stride
409 def stride_align(self, align: int) -> int: argument
410 stride = self.stride_default
419 self.stride = stride
423 def stride_default(self) -> int: argument
424 return (self.w * self.cf.bpp + 7) // 8
427 def binary(self) -> bytearray: argument
430 binary += uint8_t(self.cf.value)
431 binary += uint16_t(self.flags) # 16bits flags
433 binary += uint16_t(self.w) # 16bits width
434 binary += uint16_t(self.h) # 16bits height
435 binary += uint16_t(self.stride) # 16bits stride
440 def from_binary(self, data: bytes): argument
445 self.cf = ColorFormat(data[1] & 0x1f) # color format
448 self.w = int.from_bytes(data[4:6], 'little')
449 self.h = int.from_bytes(data[6:8], 'little')
450 self.stride = int.from_bytes(data[8:10], 'little')
451 return self
456 def __init__(self, argument
460 self.blk_size = (cf.bpp + 7) // 8
461 self.compress = method
462 self.raw_data = raw_data
463 self.raw_data_len = len(raw_data)
464 self.compressed = self._compress(raw_data)
466 def _compress(self, raw_data: bytes) -> bytearray: argument
467 if self.compress == CompressMethod.NONE:
470 if self.compress == CompressMethod.RLE:
473 if self.raw_data_len % self.blk_size:
474 pad = b'\x00' * (self.blk_size - self.raw_data_len % self.blk_size)
475 compressed = RLEImage().rle_compress(raw_data + pad, self.blk_size)
476 elif self.compress == CompressMethod.LZ4:
481 self.compressed_len = len(compressed)
484 bin += uint32_t(self.compress.value)
485 bin += uint32_t(self.compressed_len)
486 bin += uint32_t(self.raw_data_len)
493 def __init__(self, argument
498 self.stride = 0 # default no valid stride value
499 self.premultiplied = False
500 self.rgb565_dither = False
501 self.set_data(cf, w, h, data)
503 def __repr__(self) -> str: argument
509 def adjust_stride(self, stride: int = 0, align: int = 1): argument
513 if self.stride == 0:
520 header = LVGLImageHeader(self.cf, self.w, self.h, align=align)
528 if self.stride == stride:
532 if self.data_len == 0:
533 self.stride = 0
536 current = LVGLImageHeader(self.cf, self.w, self.h, stride=self.stride)
558 palette_size = self.cf.ncolors * 4
559 data_out = [self.data[:palette_size]]
561 change_stride(self.data[palette_size:], self.h, current.stride,
565 if self.cf == ColorFormat.RGB565A8:
567 a8_stride = self.stride // 2
568 a8_map = self.data[-a8_stride * self.h:]
570 change_stride(a8_map, self.h, current.stride // 2,
573 self.stride = stride
574 self.data = bytearray(b''.join(data_out))
576 def premultiply(self): argument
580 if self.premultiplied:
583 if not self.cf.has_alpha:
586 if self.cf.is_indexed:
593 palette_size = self.cf.ncolors * 4
594 palette = self.data[:palette_size]
600 self.data = palette + self.data[palette_size:]
601 elif self.cf is ColorFormat.ARGB8888:
607 line_width = self.w * 4
608 for h in range(self.h):
609 offset = h * self.stride
610 map = self.data[offset:offset + self.stride]
616 self.data[offset:offset + line_width] = processed
617 elif self.cf is ColorFormat.RGB565A8:
627 line_width = self.w * 2
628 for h in range(self.h):
630 offset = self.h * self.stride + h * (self.stride // 2)
631 a = self.data[offset:offset + self.stride // 2]
634 offset = h * self.stride
635 rgb = self.data[offset:offset + self.stride]
641 self.data[offset:offset + line_width] = processed
642 elif self.cf is ColorFormat.ARGB8565:
652 line_width = self.w * 3
653 for h in range(self.h):
654 offset = h * self.stride
655 map = self.data[offset:offset + self.stride]
661 self.data[offset:offset + line_width] = processed
665 self.premultiplied = True
668 def data_len(self) -> int: argument
674 p = self.cf.ncolors * 4 if self.is_indexed and self.w * self.h else 0
675 p += self.stride * self.h
676 if self.cf is ColorFormat.RGB565A8:
677 a8_stride = self.stride // 2
678 p += a8_stride * self.h
682 def header(self) -> bytearray: argument
683 return LVGLImageHeader(self.cf, self.w, self.h)
686 def is_indexed(self): argument
687 return self.cf.is_indexed
689 def set_data(self, argument
702 self.cf = cf
703 self.w = w
704 self.h = h
708 self.stride = LVGLImageHeader(cf, w, h, stride, align=1).stride
710 if self.data_len != len(data):
714 self.data = data
716 return self
718 def from_data(self, data: bytes): argument
720 return self.set_data(header.cf, header.w, header.h,
723 def from_bin(self, filename: str): argument
733 return self.from_data(data)
735 def _check_ext(self, filename: str, ext): argument
739 def _check_dir(self, filename: str): argument
745 def to_bin(self, argument
751 self._check_ext(filename, ".bin")
752 self._check_dir(filename)
758 flags |= 0x01 if self.premultiplied else 0
760 header = LVGLImageHeader(self.cf,
761 self.w,
762 self.h,
763 self.stride,
766 compressed = LVGLCompressData(self.cf, compress, self.data)
771 return self
773 def to_c_array(self, argument
776 self._check_ext(filename, ".c")
777 self._check_dir(filename)
780 data = LVGLCompressData(self.cf, compress, self.data).compressed
782 data = self.data
783 write_c_array_file(self.w, self.h, self.stride, self.cf, filename,
784 self.premultiplied,
787 def to_png(self, filename: str): argument
788 self._check_ext(filename, ".png")
789 self._check_dir(filename)
791 old_stride = self.stride
792 self.adjust_stride(align=1)
793 if self.cf.is_indexed:
794 data = self.data
799 data[i * 4 + 3]) for i in range(self.cf.ncolors)]
801 data = data[self.cf.ncolors * 4:]
803 encoder = png.Writer(self.w,
804 self.h,
806 bitdepth=self.cf.bpp)
808 data = unpack_colors(data, self.cf, self.w)
809 elif self.cf.is_alpha_only:
811 transparency = unpack_colors(self.data, self.cf, self.w)
815 encoder = png.Writer(self.w, self.h, greyscale=False, alpha=True)
816 elif self.cf == ColorFormat.L8:
818 encoder = png.Writer(self.w,
819 self.h,
820 bitdepth=self.cf.bpp,
823 data = self.data
824 elif self.cf.is_colormap:
825 encoder = png.Writer(self.w,
826 self.h,
827 alpha=self.cf.has_alpha,
829 data = unpack_colors(self.data, self.cf, self.w)
837 self.adjust_stride(stride=old_stride)
839 def from_png(self, argument
849 self.background = background
850 self.rgb565_dither = rgb565_dither
862 self._png_to_indexed(cf, filename)
864 self._png_to_alpha_only(cf, filename)
866 self._png_to_luma_only(cf, filename)
868 self._png_to_colormap(cf, filename)
873 return self
875 def _png_to_indexed(self, cf: ColorFormat, filename: str): argument
926 self.set_data(cf, w, h, rawdata)
928 def _png_to_alpha_only(self, cf: ColorFormat, filename: str): argument
947 self.set_data(cf, w, h, rawdata)
949 def sRGB_to_linear(self, x): argument
954 def linear_to_sRGB(self, y): argument
959 def _png_to_luma_only(self, cf: ColorFormat, filename: str): argument
969 r, g, b, a = color_pre_multiply(r, g, b, a, self.background)
970 r = self.sRGB_to_linear(r / 255.0)
971 g = self.sRGB_to_linear(g / 255.0)
972 b = self.sRGB_to_linear(b / 255.0)
974 rawdata += uint8_t(int(self.linear_to_sRGB(luma) * 255))
976 self.set_data(ColorFormat.L8, w, h, rawdata)
978 def _png_to_colormap(self, cf, filename: str): argument
987 r, g, b, a = color_pre_multiply(r, g, b, a, self.background)
992 r, g, b, a = color_pre_multiply(r, g, b, a, self.background)
997 r, g, b, a = color_pre_multiply(r, g, b, a, self.background)
1034 self.rgb565_dither and
1048 self.set_data(cf, w, h, rawdata)
1087 def __init__(self, blksize: int, len: int): argument
1088 self.blksize = blksize
1089 self.len = len
1092 def binary(self): argument
1095 rle_header = self.blksize
1096 rle_header |= (self.len & 0xffffff) << 4
1106 def __init__(self, argument
1113 def to_rle(self, filename: str): argument
1117 self._check_ext(filename, ".rle")
1118 self._check_dir(filename)
1121 blksize = (self.cf.bpp + 7) // 8
1122 compressed = self.rle_compress(self.data, blksize)
1124 header = RLEHeader(blksize, len(self.data)).binary
1125 header.extend(self.header.binary)
1129 def rle_compress(self, data: bytearray, blksize: int, threshold=16): argument
1135 repeat_cnt = self.get_repeat_count(memview[index:], blksize)
1140 nonrepeat_cnt = self.get_nonrepeat_count(
1155 def get_repeat_count(self, data: bytearray, blksize: int): argument
1177 def get_nonrepeat_count(self, data: bytearray, blksize: int, threshold): argument
1222 def __init__(self, argument
1225 self.cf = cf
1226 self.data = data
1228 def to_c_array(self, argument
1232 write_c_array_file(0, 0, 0, self.cf, filename,
1233 False, CompressMethod.NONE, self.data)
1235 def from_file(self, argument
1242 self.data = f.read()
1243 self.cf = cf
1244 return self
1255 def __init__(self, argument
1266 self.files = files
1267 self.cf = cf
1268 self.ofmt = ofmt
1269 self.output = odir
1270 self.pngquant = None
1271 self.keep_folder = keep_folder
1272 self.align = align
1273 self.premultiply = premultiply
1274 self.compress = compress
1275 self.background = background
1276 self.rgb565_dither = rgb565_dither
1278 def _replace_ext(self, input, ext): argument
1279 if self.keep_folder:
1284 output = path.join(self.output, output)
1287 def convert(self): argument
1289 for f in self.files:
1290 if self.cf in (ColorFormat.RAW, ColorFormat.RAW_ALPHA):
1292 img = RAWImage().from_file(f, self.cf)
1293 img.to_c_array(self._replace_ext(f, ".c"))
1295 …img = LVGLImage().from_png(f, self.cf, background=self.background, rgb565_dither=self.rgb565_dithe…
1296 img.adjust_stride(align=self.align)
1298 if self.premultiply:
1301 if self.ofmt == OutputFormat.BIN_FILE:
1302 img.to_bin(self._replace_ext(f, ".bin"),
1303 compress=self.compress)
1304 elif self.ofmt == OutputFormat.C_ARRAY:
1305 img.to_c_array(self._replace_ext(f, ".c"),
1306 compress=self.compress)
1307 elif self.ofmt == OutputFormat.PNG_FILE:
1308 img.to_png(self._replace_ext(f, ".png"))