reportlab/pdfgen/pdfimages.py
author aaron_watters
Tue, 24 Oct 2000 02:05:22 +0000
changeset 490 03c081d12447
child 491 6bf7da76b8fd
permissions -rw-r--r--
image functionality factored out of canvas.py initial checkin. tests pass
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
490
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
     1
###############################################################################
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
     2
#
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
     3
#	ReportLab Public License Version 1.0
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
     4
#
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
     5
#   Except for the change of names the spirit and intention of this
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
     6
#   license is the same as that of Python
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
     7
#
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
     8
#	(C) Copyright ReportLab Inc. 1998-2000.
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
     9
#
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    10
#
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    11
# All Rights Reserved
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    12
#
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    13
# Permission to use, copy, modify, and distribute this software and its
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    14
# documentation for any purpose and without fee is hereby granted, provided
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    15
# that the above copyright notice appear in all copies and that both that
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    16
# copyright notice and this permission notice appear in supporting
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    17
# documentation, and that the name of ReportLab not be used
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    18
# in advertising or publicity pertaining to distribution of the software
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    19
# without specific, written prior permission. 
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    20
# 
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    21
#
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    22
# Disclaimer
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    23
#
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    24
# ReportLab Inc. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    25
# SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    26
# IN NO EVENT SHALL ReportLab BE LIABLE FOR ANY SPECIAL, INDIRECT
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    27
# OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    28
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    29
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    30
# PERFORMANCE OF THIS SOFTWARE. 
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    31
#
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    32
###############################################################################
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    33
#	$Log: pdfimages.py,v $
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    34
#	Revision 1.1  2000/10/24 02:05:22  aaron_watters
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    35
#	image functionality factored out of canvas.py initial checkin. tests pass
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    36
#
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    37
#	
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    38
__version__=''' $Id: pdfimages.py,v 1.1 2000/10/24 02:05:22 aaron_watters Exp $ '''
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    39
__doc__="""
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    40
Image functionality sliced out of canvas.py for generalization
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    41
"""
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    42
# UNCOMMENT AS NEEDED
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    43
import os
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    44
#import sys
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    45
import string
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    46
#import time
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    47
#import tempfile
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    48
import cStringIO
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    49
from types import *
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    50
#from math import sin, cos, tan, pi, ceil
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    51
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    52
from reportlab.pdfbase import pdfutils
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    53
#from reportlab.pdfbase import pdfdoc
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    54
#from reportlab.pdfbase import pdfmetrics
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    55
#from reportlab.pdfgen  import pdfgeom, pathobject, textobject
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    56
#from reportlab.lib.colors import ColorType, toColor
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    57
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    58
from reportlab.lib.utils import fp_str
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    59
#_SeqTypes=(TupleType,ListType)
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    60
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    61
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    62
class PDFImage:
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    63
    def __init__(self, image, x,y, width=None, height=None):
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    64
        self.image = image
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    65
        self.point = (x,y)
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    66
        self.dimensions = (width, height)
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    67
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    68
    def jpg_imagedata(self):
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    69
        #directly process JPEG files
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    70
        #open file, needs some error handling!!
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    71
        imageFile = open(self.image, 'rb')
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    72
        info = pdfutils.readJPEGInfo(imageFile)
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    73
        imgwidth, imgheight = info[0], info[1]
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    74
        if info[2] == 1:
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    75
            colorSpace = 'DeviceGray'
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    76
        elif info[2] == 3:
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    77
            colorSpace = 'DeviceRGB'
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    78
        else: #maybe should generate an error, is this right for CMYK?
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    79
            colorSpace = 'DeviceCMYK'
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    80
        imageFile.seek(0) #reset file pointer
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    81
        imagedata = []
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    82
        #imagedata.append('BI /Width %d /Height /BitsPerComponent 8 /ColorSpace /%s /Filter [/Filter [ /ASCII85Decode /DCTDecode] ID' % (info[0], info[1], colorSpace))
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    83
        imagedata.append('BI /W %d /H %d /BPC 8 /CS /%s /F [/A85 /DCT] ID' % (imgwidth, imgheight, colorSpace))
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    84
        #write in blocks of (??) 60 characters per line to a list
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    85
        compressed = imageFile.read()
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    86
        encoded = pdfutils._AsciiBase85Encode(compressed)
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    87
        outstream = cStringIO.StringIO(encoded)
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    88
        dataline = outstream.read(60)
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    89
        while dataline <> "":
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    90
            imagedata.append(dataline)
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    91
            dataline = outstream.read(60)
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    92
        imagedata.append('EI')
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    93
        return (imagedata, imgwidth, imgheight)
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    94
    
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    95
    def cache_imagedata(self):
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    96
        image = self.image
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    97
        if not pdfutils.cachedImageExists(image):
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    98
            if not zlib:
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
    99
                print 'zlib not available'
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   100
                return
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   101
            try:
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   102
                import Image
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   103
            except ImportError:
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   104
                print 'Python Imaging Library not available'
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   105
                return
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   106
            pdfutils.cacheImageFile(image)
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   107
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   108
        #now we have one cached, slurp it in
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   109
        cachedname = os.path.splitext(image)[0] + '.a85'
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   110
        imagedata = open(cachedname,'rb').readlines()
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   111
        #trim off newlines...
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   112
        imagedata = map(string.strip, imagedata)
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   113
        return imagedata
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   114
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   115
    def PIL_imagedata(self):
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   116
        image = self.image
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   117
        if not zlib:
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   118
            print 'zlib not available'
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   119
            return
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   120
        myimage = image.convert('RGB')
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   121
        imgwidth, imgheight = myimage.size
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   122
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   123
        # this describes what is in the image itself
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   124
        # *NB* according to the spec you can only use the short form in inline images
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   125
        #imagedata.append('BI /Width %d /Height /BitsPerComponent 8 /ColorSpace /%s /Filter [/Filter [ /ASCII85Decode /FlateDecode] ID' % (imgwidth, imgheight,'RGB'))
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   126
        imagedata.append('BI /W %d /H %d /BPC 8 /CS /RGB /F [/A85 /Fl] ID' % (imgwidth, imgheight))
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   127
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   128
        #use a flate filter and Ascii Base 85 to compress
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   129
        raw = myimage.tostring()
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   130
        assert(len(raw) == imgwidth * imgheight, "Wrong amount of data for image")
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   131
        compressed = zlib.compress(raw)   #this bit is very fast...
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   132
        encoded = pdfutils._AsciiBase85Encode(compressed) #...sadly this isn't
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   133
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   134
        #write in blocks of (??) 60 characters per line to a list
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   135
        outstream = cStringIO.StringIO(encoded)
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   136
        dataline = outstream.read(60)
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   137
        while dataline <> "":
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   138
            imagedata.append(dataline)
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   139
            dataline = outstream.read(60)
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   140
        imagedata.append('EI')
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   141
        return (imagedata, imgwidth, imgheight) 
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   142
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   143
    def drawInlineImage(self, canvas): #, image, x,y, width=None,height=None):
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   144
        """Draw an Image into the specified rectangle.  If width and
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   145
        height are omitted, they are calculated from the image size.
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   146
        Also allow file names as well as images.  This allows a
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   147
        caching mechanism"""
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   148
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   149
        image = self.image 
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   150
        (x,y) = self.point
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   151
        (width, height) = self.dimensions
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   152
        
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   153
        if type(image) == StringType:
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   154
            if os.path.splitext(image)[1] in ['.jpg', '.JPG', '.jpeg', '.JPEG']:
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   155
                (imagedata, imgwidth, imgheight) = self.jpg_imagedata()
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   156
            else:
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   157
                if hasattr(self,'noImageCaching') and canvas.noImageCaching:
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   158
                    imagedata = pdfutils.cacheImageFile(image,returnInMemory=1)
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   159
                else:
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   160
                    imagedata = self.cache_imagedata()
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   161
                #parse line two for width, height
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   162
                words = string.split(imagedata[1])
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   163
                imgwidth = string.atoi(words[1])
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   164
                imgheight = string.atoi(words[3])
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   165
        else:
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   166
            (imagedata, imgwidth, imgheight) = self.PIL_imagedata()
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   167
        #now build the PDF for the image.
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   168
        if not width:
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   169
            width = imgwidth
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   170
        if not height:
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   171
            height = imgheight
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   172
        
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   173
        # this says where and how big to draw it
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   174
        if not canvas.bottomup: y = y+height
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   175
        canvas._code.append('q %s 0 0 %s cm' % (fp_str(width), fp_str(height, x, y)))
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   176
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   177
        # self._code.extend(imagedata) if >=python-1.5.2
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   178
        for line in imagedata:
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   179
            canvas._code.append(line)
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   180
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   181
        canvas._code.append('Q')
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   182
        #self._code.append('BT')
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   183
03c081d12447 image functionality factored out of canvas.py initial checkin. tests pass
aaron_watters
parents:
diff changeset
   184