Added ref to Misc/NEWS file; added hint to run regen script on Linux.
[python/dscho.git] / Mac / Demo / imgbrowse / imgbrowse.py
blobe498a0648722752a4fdd24964fbebe0a81fe70fd
1 """imgbrowse - Display pictures using img"""
3 import FrameWork
4 import EasyDialogs
5 import Res
6 import Qd
7 import QuickDraw
8 import Win
9 #import List
10 import sys
11 import struct
12 import img
13 import imgformat
14 import macfs
15 import struct
18 # Where is the picture window?
19 LEFT=200
20 TOP=64
21 MINWIDTH=64
22 MINHEIGHT=64
23 MAXWIDTH=320
24 MAXHEIGHT=320
26 def dumppixmap(data):
27 baseAddr, \
28 rowBytes, \
29 t, l, b, r, \
30 pmVersion, \
31 packType, packSize, \
32 hRes, vRes, \
33 pixelType, pixelSize, \
34 cmpCount, cmpSize, \
35 planeBytes, pmTable, pmReserved \
36 = struct.unpack("lhhhhhhhlllhhhhlll", data)
37 print 'Base: 0x%x'%baseAddr
38 print 'rowBytes: %d (0x%x)'%(rowBytes&0x3fff, rowBytes)
39 print 'rect: %d, %d, %d, %d'%(t, l, b, r)
40 print 'pmVersion: 0x%x'%pmVersion
41 print 'packing: %d %d'%(packType, packSize)
42 print 'resolution: %f x %f'%(float(hRes)/0x10000, float(vRes)/0x10000)
43 print 'pixeltype: %d, size %d'%(pixelType, pixelSize)
44 print 'components: %d, size %d'%(cmpCount, cmpSize)
45 print 'planeBytes: %d (0x%x)'%(planeBytes, planeBytes)
46 print 'pmTable: 0x%x'%pmTable
47 print 'pmReserved: 0x%x'%pmReserved
49 def mk16pixmap(w, h, data):
50 """kludge a pixmap together"""
51 rv = struct.pack("lhhhhhhhlllhhhhlll",
52 id(data),
53 w*2 + 0x8000,
54 0, 0, h, w,
56 0, 0, # XXXX?
57 72<<16, 72<<16,
58 16, 16, # XXXX
59 3, 5,
60 0, 0, 0)
61 print 'Our pixmap, size %d:'%len(rv)
62 dumppixmap(rv)
63 return Qd.RawBitMap(rv)
65 def main():
66 print 'hello world'
67 imgbrowse()
69 class imgbrowse(FrameWork.Application):
70 def __init__(self):
71 # First init menus, etc.
72 FrameWork.Application.__init__(self)
73 self.lastwin = None
74 # Finally, go into the event loop
75 self.mainloop()
77 def makeusermenus(self):
78 self.filemenu = m = FrameWork.Menu(self.menubar, "File")
79 self.openitem = FrameWork.MenuItem(m, "Open...", "O", self.opendoc)
80 self.infoitem = FrameWork.MenuItem(m, "Info", "I", self.info)
81 self.quititem = FrameWork.MenuItem(m, "Quit", "Q", self.quit)
83 def quit(self, *args):
84 raise self
86 def opendoc(self, *args):
87 fss, ok = macfs.StandardGetFile() # Any file type
88 if not ok:
89 return
90 bar = EasyDialogs.ProgressBar('Reading and converting...')
91 pathname = fss.as_pathname()
92 try:
93 rdr = img.reader(imgformat.macrgb16, pathname)
94 except img.error, arg:
95 EasyDialogs.Message(`arg`)
96 return
97 w, h = rdr.width, rdr.height
98 bar.set(10)
99 data = rdr.read()
100 del bar
101 pixmap = mk16pixmap(w, h, data)
102 self.showimg(w, h, pixmap)
104 def showimg(self, w, h, pixmap):
105 win = imgwindow(self)
106 win.open(w, h, pixmap)
107 self.lastwin = win
109 def info(self, *args):
110 if self.lastwin:
111 self.lastwin.info()
113 class imgwindow(FrameWork.Window):
114 def open(self, width, height, pixmap):
115 self.pixmap = pixmap
116 self.pictrect = (0, 0, width, height)
117 bounds = (LEFT, TOP, LEFT+width, TOP+height)
119 self.wid = Win.NewCWindow(bounds, "Picture", 1, 0, -1, 1, 0)
120 self.do_postopen()
122 def do_update(self, *args):
123 pass
124 currect = self.fitrect()
125 print 'PICT:', self.pictrect
126 print 'WIND:', currect
127 print 'ARGS:', (self.pixmap, self.wid.GetWindowPort().portBits, self.pictrect,
128 currect, QuickDraw.srcCopy, None)
129 self.info()
130 Qd.CopyBits(self.pixmap, self.wid.GetWindowPort().portBits, self.pictrect,
131 currect, QuickDraw.srcCopy, None)
132 ## Qd.DrawPicture(self.picture, currect)
134 def fitrect(self):
135 """Return self.pictrect scaled to fit in window"""
136 graf = self.wid.GetWindowPort()
137 screenrect = graf.portRect
138 picwidth = self.pictrect[2] - self.pictrect[0]
139 picheight = self.pictrect[3] - self.pictrect[1]
140 if picwidth > screenrect[2] - screenrect[0]:
141 factor = float(picwidth) / float(screenrect[2]-screenrect[0])
142 picwidth = picwidth / factor
143 picheight = picheight / factor
144 if picheight > screenrect[3] - screenrect[1]:
145 factor = float(picheight) / float(screenrect[3]-screenrect[1])
146 picwidth = picwidth / factor
147 picheight = picheight / factor
148 return (screenrect[0], screenrect[1], screenrect[0]+int(picwidth),
149 screenrect[1]+int(picheight))
151 def info(self):
152 graf = self.wid.GetWindowPort()
153 bits = graf.portBits
154 dumppixmap(bits.pixmap_data)
156 main()