Esempio n. 1
0
    def getpalette(self, entries=256):

        palette = []

        ix = 0
        x0, x1, xm, rgb0, rgb1, segment = self.gradient[ix]

        for i in range(entries):

            x = i / float(entries-1)

            while x1 < x:
                ix += 1
                x0, x1, xm, rgb0, rgb1, segment = self.gradient[ix]

            w = x1 - x0

            if w < EPSILON:
                scale = segment(0.5, 0.5)
            else:
                scale = segment((xm - x0) / w, (x - x0) / w)

            # expand to RGBA
            r = o8(int(255 * ((rgb1[0] - rgb0[0]) * scale + rgb0[0]) + 0.5))
            g = o8(int(255 * ((rgb1[1] - rgb0[1]) * scale + rgb0[1]) + 0.5))
            b = o8(int(255 * ((rgb1[2] - rgb0[2]) * scale + rgb0[2]) + 0.5))
            a = o8(int(255 * ((rgb1[3] - rgb0[3]) * scale + rgb0[3]) + 0.5))

            # add to palette
            palette.append(r + g + b + a)

        return b"".join(palette), "RGBA"
Esempio n. 2
0
    def __init__(self, fp):

        self.palette = [o8(i)*3 for i in range(256)]

        if fp.readline()[:12] != b"GIMP Palette":
            raise SyntaxError("not a GIMP palette file")

        i = 0

        while i <= 255:

            s = fp.readline()

            if not s:
                break
            # skip fields and comment lines
            if re.match(br"\w+:|#", s):
                continue
            if len(s) > 100:
                raise SyntaxError("bad palette file")

            v = tuple(map(int, s.split()[:3]))
            if len(v) != 3:
                raise ValueError("bad palette entry")

            if 0 <= i <= 255:
                self.palette[i] = o8(v[0]) + o8(v[1]) + o8(v[2])

            i += 1

        self.palette = b"".join(self.palette)
Esempio n. 3
0
    def __init__(self, fp):

        self.palette = [(i, i, i) for i in range(256)]

        while True:

            s = fp.readline()

            if not s:
                break
            if s[0:1] == b"#":
                continue
            if len(s) > 100:
                raise SyntaxError("bad palette file")

            v = [int(x) for x in s.split()]
            try:
                [i, r, g, b] = v
            except ValueError:
                [i, r] = v
                g = b = r

            if 0 <= i <= 255:
                self.palette[i] = o8(r) + o8(g) + o8(b)

        self.palette = b"".join(self.palette)
Esempio n. 4
0
    def __init__(self, fp):

        self.palette = [o8(i) * 3 for i in range(256)]

        if fp.readline()[:12] != b"GIMP Palette":
            raise SyntaxError("not a GIMP palette file")

        i = 0

        while i <= 255:

            s = fp.readline()

            if not s:
                break
            # skip fields and comment lines
            if re.match(b"\w+:|#", s):
                continue
            if len(s) > 100:
                raise SyntaxError("bad palette file")

            v = tuple(map(int, s.split()[:3]))
            if len(v) != 3:
                raise ValueError("bad palette entry")

            if 0 <= i <= 255:
                self.palette[i] = o8(v[0]) + o8(v[1]) + o8(v[2])

            i += 1

        self.palette = b"".join(self.palette)
Esempio n. 5
0
    def __init__(self, fp):

        self.palette = [(i, i, i) for i in range(256)]

        while True:

            s = fp.readline()

            if not s:
                break
            if s[0:1] == b"#":
                continue
            if len(s) > 100:
                raise SyntaxError("bad palette file")

            v = [int(x) for x in s.split()]
            try:
                [i, r, g, b] = v
            except ValueError:
                [i, r] = v
                g = b = r

            if 0 <= i <= 255:
                self.palette[i] = o8(r) + o8(g) + o8(b)

        self.palette = b"".join(self.palette)
Esempio n. 6
0
    def getpalette(self, entries=256):

        palette = []

        ix = 0
        x0, x1, xm, rgb0, rgb1, segment = self.gradient[ix]

        for i in range(entries):

            x = i / float(entries-1)

            while x1 < x:
                ix += 1
                x0, x1, xm, rgb0, rgb1, segment = self.gradient[ix]

            w = x1 - x0

            if w < EPSILON:
                scale = segment(0.5, 0.5)
            else:
                scale = segment((xm - x0) / w, (x - x0) / w)

            # expand to RGBA
            r = o8(int(255 * ((rgb1[0] - rgb0[0]) * scale + rgb0[0]) + 0.5))
            g = o8(int(255 * ((rgb1[1] - rgb0[1]) * scale + rgb0[1]) + 0.5))
            b = o8(int(255 * ((rgb1[2] - rgb0[2]) * scale + rgb0[2]) + 0.5))
            a = o8(int(255 * ((rgb1[3] - rgb0[3]) * scale + rgb0[3]) + 0.5))

            # add to palette
            palette.append(r + g + b + a)

        return b"".join(palette), "RGBA"
Esempio n. 7
0
 def write(self, fp):
     self.header.length = len(self.palette_entries) * 3
     self.header.write(fp)
     for entry in self.palette_entries:
         fp.write(b''  # start off with empty bytes...
                  + o8(entry[0])  # h
                  + o8(entry[1])  # s
                  + o8(entry[2])  # v
                  )
Esempio n. 8
0
 def write(self, fp):
     self.header.write(fp)
     fp.write(b''  # start off with empty bytes...
              + o8(self.format)  # format
              + o8(self.flags)  # flags
              + o8(self.compression)  # compression
              + o8(self.transparency_index)  # transparency index
              + o16(self.delay)  # delay
              )
Esempio n. 9
0
 def write(self, fp):
     self.header.write(fp)
     fp.write(b''  # start off with empty bytes...
              + o24(QGFGraphicsDescriptor.magic)  # magic
              + o8(self.version)  # version
              + o32(self.total_file_size)  # file size
              +
              o32((~self.total_file_size) & 0xFFFFFFFF)  # negated file size
              + o16(self.image_width)  # width
              + o16(self.image_height)  # height
              + o16(self.frame_count)  # frame count
              )
Esempio n. 10
0
 def write(self, fp):
     self.header.write(fp)
     fp.write(b''  # start off with empty bytes...
              + o24(QFFFontDescriptor.magic)  # magic
              + o8(self.version)  # version
              + o32(self.total_file_size)  # file size
              +
              o32((~self.total_file_size) & 0xFFFFFFFF)  # negated file size
              + o8(self.line_height)  # line height
              + o8(1 if self.has_ascii_table is True else 0
                   )  # whether or not we have an ascii table present
              + o16(self.unicode_glyph_count
                    & 0xFFFF)  # number of unicode glyphs present
              + o8(self.format)  # format
              + o8(self.flags)  # flags
              + o8(self.compression)  # compression
              + o8(self.transparency_index)  # transparency index
              )
Esempio n. 11
0
def test_standard():
    assert _binary.i8(b"*") == 42
    assert _binary.o8(42) == b"*"
Esempio n. 12
0
    def _open(self):

        if not _accept(self.fp.read(9)):
            raise SyntaxError("not an XPM file")

        # skip forward to next string
        while True:
            s = self.fp.readline()
            if not s:
                raise SyntaxError("broken XPM file")
            m = xpm_head.match(s)
            if m:
                break

        self.size = int(m.group(1)), int(m.group(2))

        pal = int(m.group(3))
        bpp = int(m.group(4))

        if pal > 256 or bpp != 1:
            raise ValueError("cannot read this XPM file")

        #
        # load palette description

        palette = [b"\0\0\0"] * 256

        for i in range(pal):

            s = self.fp.readline()
            if s[-2:] == b'\r\n':
                s = s[:-2]
            elif s[-1:] in b'\r\n':
                s = s[:-1]

            c = i8(s[1])
            s = s[2:-2].split()

            for i in range(0, len(s), 2):

                if s[i] == b"c":

                    # process colour key
                    rgb = s[i + 1]
                    if rgb == b"None":
                        self.info["transparency"] = c
                    elif rgb[0:1] == b"#":
                        # FIXME: handle colour names (see ImagePalette.py)
                        rgb = int(rgb[1:], 16)
                        palette[c] = (o8((rgb >> 16) & 255) +
                                      o8((rgb >> 8) & 255) + o8(rgb & 255))
                    else:
                        # unknown colour
                        raise ValueError("cannot read this XPM file")
                    break

            else:

                # missing colour key
                raise ValueError("cannot read this XPM file")

        self.mode = "P"
        self.palette = ImagePalette.raw("RGB", b"".join(palette))

        self.tile = [("raw", (0, 0) + self.size, self.fp.tell(), ("P", 0, 1))]
Esempio n. 13
0
    def _open(self):

        if not _accept(self.fp.read(9)):
            raise SyntaxError("not an XPM file")

        # skip forward to next string
        while True:
            s = self.fp.readline()
            if not s:
                raise SyntaxError("broken XPM file")
            m = xpm_head.match(s)
            if m:
                break

        self.size = int(m.group(1)), int(m.group(2))

        pal = int(m.group(3))
        bpp = int(m.group(4))

        if pal > 256 or bpp != 1:
            raise ValueError("cannot read this XPM file")

        #
        # load palette description

        palette = [b"\0\0\0"] * 256

        for i in range(pal):

            s = self.fp.readline()
            if s[-2:] == b'\r\n':
                s = s[:-2]
            elif s[-1:] in b'\r\n':
                s = s[:-1]

            c = i8(s[1])
            s = s[2:-2].split()

            for i in range(0, len(s), 2):

                if s[i] == b"c":

                    # process colour key
                    rgb = s[i+1]
                    if rgb == b"None":
                        self.info["transparency"] = c
                    elif rgb[0:1] == b"#":
                        # FIXME: handle colour names (see ImagePalette.py)
                        rgb = int(rgb[1:], 16)
                        palette[c] = o8((rgb >> 16) & 255) +\
                                     o8((rgb >> 8) & 255) +\
                                     o8(rgb & 255)
                    else:
                        # unknown colour
                        raise ValueError("cannot read this XPM file")
                    break

            else:

                # missing colour key
                raise ValueError("cannot read this XPM file")

        self.mode = "P"
        self.palette = ImagePalette.raw("RGB", b"".join(palette))

        self.tile = [("raw", (0, 0)+self.size, self.fp.tell(), ("P", 0, 1))]
Esempio n. 14
0
 def test_standard(self):
     self.assertEqual(_binary.i8(b'*'), 42)
     self.assertEqual(_binary.o8(42), b'*')
Esempio n. 15
0
def o24(i):
    return o16(i & 0xFFFF) + o8((i & 0xFF0000) >> 16)
Esempio n. 16
0
 def test_standard(self):
     self.assertEqual(_binary.i8(b"*"), 42)
     self.assertEqual(_binary.o8(42), b"*")
Esempio n. 17
0
 def write(self, fp):
     fp.write(b''  # start off with empty bytes...
              + o8(self.type_id)  # block type id
              + o8((~self.type_id) & 0xFF)  # negated block type id
              + o24(self.length)  # blob length
              )