~ubuntu-branches/ubuntu/quantal/enigmail/quantal-security

« back to all changes in this revision

Viewing changes to build/png2ico.py

  • Committer: Package Import Robot
  • Author(s): Chris Coulson
  • Date: 2013-09-13 16:02:15 UTC
  • mfrom: (0.12.16)
  • Revision ID: package-import@ubuntu.com-20130913160215-u3g8nmwa0pdwagwc
Tags: 2:1.5.2-0ubuntu0.12.10.1
* New upstream release v1.5.2 for Thunderbird 24

* Build enigmail using a stripped down Thunderbird 17 build system, as it's
  now quite difficult to build the way we were doing previously, with the
  latest Firefox build system
* Add debian/patches/no_libxpcom.patch - Don't link against libxpcom, as it
  doesn't exist anymore (but exists in the build system)
* Add debian/patches/use_sdk.patch - Use the SDK version of xpt.py and
  friends
* Drop debian/patches/ipc-pipe_rename.diff (not needed anymore)
* Drop debian/patches/makefile_depth.diff (not needed anymore)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
# This Source Code Form is subject to the terms of the Mozilla Public
 
3
# License, v. 2.0. If a copy of the MPL was not distributed with this
 
4
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
5
 
 
6
import png
 
7
import sys
 
8
import StringIO
 
9
import struct
 
10
import ctypes
 
11
 
 
12
# ref: http://msdn.microsoft.com/en-us/library/ms997538
 
13
class ICONDIR(ctypes.LittleEndianStructure):
 
14
    _pack_ = 1
 
15
    _fields_ = [("idReserved", ctypes.c_ushort),
 
16
                ("idType", ctypes.c_ushort),
 
17
                ("idCount", ctypes.c_ushort)]
 
18
 
 
19
class ICONDIRENTRY(ctypes.LittleEndianStructure):
 
20
    _pack_ = 1
 
21
    _fields_ = [("bWidth", ctypes.c_byte),
 
22
                ("bHeight", ctypes.c_byte),
 
23
                ("bColorCount", ctypes.c_byte),
 
24
                ("bReserved", ctypes.c_byte),
 
25
                ("wPlanes", ctypes.c_ushort),
 
26
                ("wBitCount", ctypes.c_ushort),
 
27
                ("dwBytesInRes", ctypes.c_ulong),
 
28
                ("dwImageOffset", ctypes.c_ulong)]
 
29
 
 
30
# R, G, B, A, so 4 columns per pixel
 
31
COLS_PP = 4
 
32
 
 
33
def main(infile, left, top, size, outfile):
 
34
    img = png.Reader(filename=infile)
 
35
    pixels = list(img.asRGBA()[2])
 
36
    # Take the subarray out. This is the ugliest but probably most efficient way
 
37
    # to do it
 
38
    outpixels = [[0] * (size * COLS_PP) for x in xrange(size)]
 
39
    for row in xrange(size):
 
40
        for col in xrange(size * COLS_PP):
 
41
            outpixels[row][col] = pixels[top + row][left * COLS_PP + col]
 
42
 
 
43
    # Set up a 32bpp RGBA PNG.
 
44
    writer = png.Writer(size=(size, size), bitdepth=8, alpha=True)
 
45
    # Write to a memory buffer
 
46
    outpng = StringIO.StringIO()
 
47
    writer.write(outpng, outpixels)
 
48
    outpngbuf = outpng.getvalue()
 
49
    outpng.close()
 
50
 
 
51
    # Set up an icon header
 
52
    icondir = ICONDIR()
 
53
    icondir.idReserved = 0
 
54
    # Icons are type 1
 
55
    icondir.idType = 1
 
56
    icondir.idCount = 1
 
57
 
 
58
    iconentry = ICONDIRENTRY()
 
59
    iconentry.bWidth = size
 
60
    iconentry.bHeight = size
 
61
    # Truecolor images have color count set to 0
 
62
    iconentry.bColorCount = 0
 
63
    iconentry.bReserved = 0
 
64
    # PNGs have 1 color plane
 
65
    iconentry.wPlanes = 1
 
66
    # We're RGBA, so 32 bits per pixel
 
67
    iconentry.wBitCount = 32
 
68
    # Length of the buffer
 
69
    iconentry.dwBytesInRes = len(outpngbuf)
 
70
    # The data will be right after the icondir and iconentry
 
71
    iconentry.dwImageOffset = ctypes.sizeof(icondir) + ctypes.sizeof(iconentry)
 
72
 
 
73
    # Time to write everything out
 
74
    out = open(outfile, "wb")
 
75
    out.write(icondir)
 
76
    out.write(iconentry)
 
77
    out.write(outpngbuf)
 
78
    out.close()
 
79
 
 
80
if __name__ == "__main__":
 
81
    # Convert left, top and size into integers
 
82
    main(*([sys.argv[1]] + [int(val) for val in sys.argv[2:5]] + [sys.argv[5]]))