| 1 | """imgbrowse - Display pictures using img"""
|
|---|
| 2 |
|
|---|
| 3 | import FrameWork
|
|---|
| 4 | import EasyDialogs
|
|---|
| 5 | from Carbon import Res
|
|---|
| 6 | from Carbon import Qd
|
|---|
| 7 | from Carbon import QuickDraw
|
|---|
| 8 | from Carbon import Win
|
|---|
| 9 | #ifrom Carbon mport List
|
|---|
| 10 | import sys
|
|---|
| 11 | import struct
|
|---|
| 12 | import img
|
|---|
| 13 | import imgformat
|
|---|
| 14 | import struct
|
|---|
| 15 | import mac_image
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 | # Where is the picture window?
|
|---|
| 19 | LEFT=200
|
|---|
| 20 | TOP=64
|
|---|
| 21 | MINWIDTH=64
|
|---|
| 22 | MINHEIGHT=64
|
|---|
| 23 | MAXWIDTH=320
|
|---|
| 24 | MAXHEIGHT=320
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 | def main():
|
|---|
| 28 | print 'hello world'
|
|---|
| 29 | imgbrowse()
|
|---|
| 30 |
|
|---|
| 31 | class imgbrowse(FrameWork.Application):
|
|---|
| 32 | def __init__(self):
|
|---|
| 33 | # First init menus, etc.
|
|---|
| 34 | FrameWork.Application.__init__(self)
|
|---|
| 35 | self.lastwin = None
|
|---|
| 36 | # Finally, go into the event loop
|
|---|
| 37 | self.mainloop()
|
|---|
| 38 |
|
|---|
| 39 | def makeusermenus(self):
|
|---|
| 40 | self.filemenu = m = FrameWork.Menu(self.menubar, "File")
|
|---|
| 41 | self.openitem = FrameWork.MenuItem(m, "Open...", "O", self.opendoc)
|
|---|
| 42 | self.infoitem = FrameWork.MenuItem(m, "Info", "I", self.info)
|
|---|
| 43 | self.quititem = FrameWork.MenuItem(m, "Quit", "Q", self.quit)
|
|---|
| 44 |
|
|---|
| 45 | def quit(self, *args):
|
|---|
| 46 | self._quit()
|
|---|
| 47 |
|
|---|
| 48 | def opendoc(self, *args):
|
|---|
| 49 | pathname = EasyDialogs.AskFileForOpen() # Any file type
|
|---|
| 50 | if not pathname:
|
|---|
| 51 | return
|
|---|
| 52 | bar = EasyDialogs.ProgressBar('Reading and converting...')
|
|---|
| 53 | try:
|
|---|
| 54 | rdr = img.reader(imgformat.macrgb16, pathname)
|
|---|
| 55 | except img.error, arg:
|
|---|
| 56 | EasyDialogs.Message(repr(arg))
|
|---|
| 57 | return
|
|---|
| 58 | w, h = rdr.width, rdr.height
|
|---|
| 59 | bar.set(10)
|
|---|
| 60 | data = rdr.read()
|
|---|
| 61 | del bar
|
|---|
| 62 | pixmap = mac_image.mkpixmap(w, h, imgformat.macrgb16, data)
|
|---|
| 63 | self.showimg(w, h, pixmap, data)
|
|---|
| 64 |
|
|---|
| 65 | def showimg(self, w, h, pixmap, data):
|
|---|
| 66 | win = imgwindow(self)
|
|---|
| 67 | win.open(w, h, pixmap, data)
|
|---|
| 68 | self.lastwin = win
|
|---|
| 69 |
|
|---|
| 70 | def info(self, *args):
|
|---|
| 71 | if self.lastwin:
|
|---|
| 72 | self.lastwin.info()
|
|---|
| 73 |
|
|---|
| 74 | class imgwindow(FrameWork.Window):
|
|---|
| 75 | def open(self, width, height, pixmap, data):
|
|---|
| 76 | self.pixmap = pixmap
|
|---|
| 77 | self.data = data
|
|---|
| 78 | self.pictrect = (0, 0, width, height)
|
|---|
| 79 | bounds = (LEFT, TOP, LEFT+width, TOP+height)
|
|---|
| 80 |
|
|---|
| 81 | self.wid = Win.NewCWindow(bounds, "Picture", 1, 0, -1, 1, 0)
|
|---|
| 82 | self.do_postopen()
|
|---|
| 83 |
|
|---|
| 84 | def do_update(self, *args):
|
|---|
| 85 | pass
|
|---|
| 86 | currect = self.fitrect()
|
|---|
| 87 | print 'PICT:', self.pictrect
|
|---|
| 88 | print 'WIND:', currect
|
|---|
| 89 | print 'ARGS:', (self.pixmap, self.wid.GetWindowPort().GetPortBitMapForCopyBits(), self.pictrect,
|
|---|
| 90 | currect, QuickDraw.srcCopy, None)
|
|---|
| 91 | self.info()
|
|---|
| 92 | Qd.CopyBits(self.pixmap, self.wid.GetWindowPort().GetPortBitMapForCopyBits(), self.pictrect,
|
|---|
| 93 | currect, QuickDraw.srcCopy, None)
|
|---|
| 94 |
|
|---|
| 95 | def fitrect(self):
|
|---|
| 96 | """Return self.pictrect scaled to fit in window"""
|
|---|
| 97 | graf = self.wid.GetWindowPort()
|
|---|
| 98 | screenrect = graf.GetPortBounds()
|
|---|
| 99 | picwidth = self.pictrect[2] - self.pictrect[0]
|
|---|
| 100 | picheight = self.pictrect[3] - self.pictrect[1]
|
|---|
| 101 | if picwidth > screenrect[2] - screenrect[0]:
|
|---|
| 102 | factor = float(picwidth) / float(screenrect[2]-screenrect[0])
|
|---|
| 103 | picwidth = picwidth / factor
|
|---|
| 104 | picheight = picheight / factor
|
|---|
| 105 | if picheight > screenrect[3] - screenrect[1]:
|
|---|
| 106 | factor = float(picheight) / float(screenrect[3]-screenrect[1])
|
|---|
| 107 | picwidth = picwidth / factor
|
|---|
| 108 | picheight = picheight / factor
|
|---|
| 109 | return (screenrect[0], screenrect[1], screenrect[0]+int(picwidth),
|
|---|
| 110 | screenrect[1]+int(picheight))
|
|---|
| 111 |
|
|---|
| 112 | def info(self):
|
|---|
| 113 | graf = self.wid.GetWindowPort()
|
|---|
| 114 | bits = graf.GetPortBitMapForCopyBits()
|
|---|
| 115 | mac_image.dumppixmap(bits.pixmap_data)
|
|---|
| 116 |
|
|---|
| 117 | main()
|
|---|