| 1 |
#!/usr/bin/env python
|
| 2 |
##
|
| 3 |
## pyvnc2swf - image.py
|
| 4 |
##
|
| 5 |
## $Id: image.py,v 1.10 2006/02/09 09:39:18 euske Exp $
|
| 6 |
##
|
| 7 |
## Copyright (C) 2005 by Yusuke Shinyama (yusuke at cs . nyu . edu)
|
| 8 |
## All Rights Reserved.
|
| 9 |
##
|
| 10 |
## This is free software; you can redistribute it and/or modify
|
| 11 |
## it under the terms of the GNU General Public License as published by
|
| 12 |
## the Free Software Foundation; either version 2 of the License, or
|
| 13 |
## (at your option) any later version.
|
| 14 |
##
|
| 15 |
## This software is distributed in the hope that it will be useful,
|
| 16 |
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 17 |
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 18 |
## GNU General Public License for more details.
|
| 19 |
##
|
| 20 |
## You should have received a copy of the GNU General Public License
|
| 21 |
## along with this software; if not, write to the Free Software
|
| 22 |
## Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
| 23 |
## USA.
|
| 24 |
##
|
| 25 |
|
| 26 |
import sys
|
| 27 |
lowerbound = max
|
| 28 |
upperbound = min
|
| 29 |
|
| 30 |
# format: 1: solid,
|
| 31 |
# 2: raw (uncompressed)
|
| 32 |
# 3: DefineBitLossless
|
| 33 |
# 4: SCREENVIDEOPACKET
|
| 34 |
IMG_SOLID = 1
|
| 35 |
IMG_RAW = 2
|
| 36 |
IMG_LOSSLESS = 3
|
| 37 |
IMG_VIDEOPACKET = 4
|
| 38 |
|
| 39 |
try:
|
| 40 |
# try to pygame 1.6 or newer.
|
| 41 |
import pygame
|
| 42 |
print >>sys.stderr, 'Using pygame', pygame.ver
|
| 43 |
pygame.init()
|
| 44 |
try:
|
| 45 |
pygame.mixer.quit()
|
| 46 |
except NotImplementedError:
|
| 47 |
pass
|
| 48 |
def imgsize(img):
|
| 49 |
return img.get_size()
|
| 50 |
def create_image(w, h):
|
| 51 |
return pygame.Surface((w, h), 0, 32)
|
| 52 |
def create_image_from_string_rgb(w, h, data):
|
| 53 |
return pygame.image.fromstring(data, (w, h), 'RGB')
|
| 54 |
def create_image_from_string_rgbx(w, h, data):
|
| 55 |
return pygame.image.fromstring(data, (w, h), 'RGBX')
|
| 56 |
def create_image_from_string_xrgb(w, h, data):
|
| 57 |
return pygame.image.fromstring(data[1:]+'x', (w, h), 'RGBX')
|
| 58 |
def create_image_from_string_bgr_flipped(w, h, data):
|
| 59 |
data = ''.join([ data[i+2]+data[i+1]+data[i] for i in xrange(0, len(data), 3) ])
|
| 60 |
return pygame.transform.flip(pygame.image.fromstring(data, (w, h), 'RGB'), 0, 1)
|
| 61 |
def crop_image(img, (x,y,w,h)):
|
| 62 |
(wm,hm) = img.get_size()
|
| 63 |
return img.subsurface((x,y,upperbound(wm-x,w),upperbound(hm-y,h)))
|
| 64 |
def paste_image(dest, src, (x0, y0)):
|
| 65 |
return dest.blit(src, (x0, y0))
|
| 66 |
def save_image(img, fname):
|
| 67 |
if not fname.endswith('.bmp'):
|
| 68 |
print >>sys.stderr, 'Warning: this format not supported by pygame, raw rgb is used instead.'
|
| 69 |
return pygame.image.save(img, fname)
|
| 70 |
def convert_image_to_string_rgb_flipped(img):
|
| 71 |
return pygame.image.tostring(img, 'RGB', 1)
|
| 72 |
def convert_image_to_string_rgb(img):
|
| 73 |
return pygame.image.tostring(img, 'RGB')
|
| 74 |
def convert_image_to_string_xrgb(img):
|
| 75 |
return pygame.image.tostring(img, 'ARGB')
|
| 76 |
def solid_fill(dest, rect, color):
|
| 77 |
return dest.fill(color, rect)
|
| 78 |
def scale_image(img, scaling):
|
| 79 |
# this might cause segmentation faults sometimes :(
|
| 80 |
# In that case, use the following instead:
|
| 81 |
# (w,h) = img.get_size()
|
| 82 |
# return pygame.transform.scale(img, (int(w*scaling), int(h*scaling)))
|
| 83 |
return pygame.transform.rotozoom(img, 0, scaling)
|
| 84 |
|
| 85 |
except ImportError:
|
| 86 |
# use PIL instead
|
| 87 |
pygame = None
|
| 88 |
try:
|
| 89 |
import Image
|
| 90 |
except ImportError:
|
| 91 |
print >>sys.stderr, 'Either Pygame or Python Imaging Library is required.'
|
| 92 |
sys.exit(1)
|
| 93 |
print >>sys.stderr, 'Using PIL', Image.VERSION
|
| 94 |
def imgsize(img):
|
| 95 |
return img.size
|
| 96 |
def create_image(w, h):
|
| 97 |
return Image.new('RGB', (w, h))
|
| 98 |
def create_image_from_string_rgb(w, h, data):
|
| 99 |
return Image.fromstring('RGB', (w, h), data, 'raw', 'RGB')
|
| 100 |
def create_image_from_string_rgbx(w, h, data):
|
| 101 |
return Image.fromstring('RGB', (w, h), data, 'raw', 'RGBX')
|
| 102 |
def create_image_from_string_xrgb(w, h, data):
|
| 103 |
return Image.fromstring('RGB', (w, h), data[1:]+'x', 'raw', 'RGBX')
|
| 104 |
def create_image_from_string_bgr_flipped(w, h, data):
|
| 105 |
return Image.fromstring('RGB', (w, h), data, 'raw', 'BGR').transpose(Image.FLIP_TOP_BOTTOM)
|
| 106 |
def crop_image(img, (x0,y0,w,h)):
|
| 107 |
(wm,hm) = img.size
|
| 108 |
return img.crop((x0, y0, upperbound(x0+w,wm), upperbound(y0+h,hm)))
|
| 109 |
def paste_image(dest, src, (x0, y0)):
|
| 110 |
return dest.paste(src, (x0, y0))
|
| 111 |
def save_image(img, fname):
|
| 112 |
return img.save(fname)
|
| 113 |
def convert_image_to_string_rgb_flipped(img):
|
| 114 |
return img.transpose(Image.FLIP_TOP_BOTTOM).tostring('raw', 'RGB')
|
| 115 |
def convert_image_to_string_rgb(img):
|
| 116 |
return img.tostring('raw', 'RGB')
|
| 117 |
def convert_image_to_string_xrgb(img):
|
| 118 |
return img.tostring('raw', 'XRGB')
|
| 119 |
def solid_fill(dest, (x0,y0,w,h), color):
|
| 120 |
return dest.paste(color, (x0, y0, x0+w, y0+h))
|
| 121 |
def scale_image(img, scaling):
|
| 122 |
img = img.copy()
|
| 123 |
(w,h) = img.size
|
| 124 |
img.thumbnail((int(w*scaling), int(h*scaling)), resample=1)
|
| 125 |
return img
|
| 126 |
|