[Zope-Checkins] CVS: Zope3/lib/python/Zope/App/OFS/Content/Image - Image.py:1.2 ImageFields.py:1.2 __init__.py:1.2 image.zcml:1.2
Jim Fulton
jim@zope.com
Mon, 10 Jun 2002 19:28:34 -0400
Update of /cvs-repository/Zope3/lib/python/Zope/App/OFS/Content/Image
In directory cvs.zope.org:/tmp/cvs-serv17445/lib/python/Zope/App/OFS/Content/Image
Added Files:
Image.py ImageFields.py __init__.py image.zcml
Log Message:
Merged Zope-3x-branch into newly forked Zope3 CVS Tree.
=== Zope3/lib/python/Zope/App/OFS/Content/Image/Image.py 1.1 => 1.2 ===
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""
+Revision Information:
+$Id$
+"""
+
+import struct
+
+from Zope.App.OFS.Content.File.File import IFile, File
+from Zope.App.OFS.Annotation.IAnnotatable import IAnnotatable
+from StringIO import StringIO
+
+
+class IImage(IFile):
+ """This interface defines an Image that can be displayed.
+ """
+
+ def getImageSize():
+ """Return a tuple (x, y) that describes the dimensions of
+ the object.
+ """
+
+
+class Image(File):
+ """ """
+
+ __implements__ = (
+ IImage,
+ IAnnotatable,
+ )
+
+ def __init__(self, data=None):
+ """ """
+ self._contentType, self._width, self._height = getImageInfo(data)
+ self.setData(data)
+
+
+ def setData(self, data):
+ """ """
+ super(Image, self).setData(data)
+
+ contentType = None
+ contentType, self._width, self._height = getImageInfo(self._data)
+ if contentType:
+ self._contentType = contentType
+
+
+
+
+ ############################################################
+ # Implementation methods for interface
+ # Zope.App.OFS.Image.IImage
+
+ def getImageSize(self):
+ '''See interface IImage'''
+
+ return (self._width, self._height)
+
+ #
+ ############################################################
+
+
+
+def getImageInfo(data):
+ """ """
+
+ data = str(data)
+ size = len(data)
+ height = -1
+ width = -1
+ content_type = ''
+
+ # handle GIFs
+ if (size >= 10) and data[:6] in ('GIF87a', 'GIF89a'):
+ # Check to see if content_type is correct
+ content_type = 'image/gif'
+ w, h = struct.unpack("<HH", data[6:10])
+ width = int(w)
+ height = int(h)
+
+ # See PNG v1.2 spec (http://www.cdrom.com/pub/png/spec/)
+ # Bytes 0-7 are below, 4-byte chunk length, then 'IHDR'
+ # and finally the 4-byte width, height
+ elif ((size >= 24) and data.startswith('\211PNG\r\n\032\n')
+ and (data[12:16] == 'IHDR')):
+ content_type = 'image/png'
+ w, h = struct.unpack(">LL", data[16:24])
+ width = int(w)
+ height = int(h)
+
+ # Maybe this is for an older PNG version.
+ elif (size >= 16) and data.startswith('\211PNG\r\n\032\n'):
+ # Check to see if we have the right content type
+ content_type = 'image/png'
+ w, h = struct.unpack(">LL", data[8:16])
+ width = int(w)
+ height = int(h)
+
+ # handle JPEGs
+ elif (size >= 2) and data.startswith('\377\330'):
+ content_type = 'image/jpeg'
+ jpeg = StringIO(data)
+ jpeg.read(2)
+ b = jpeg.read(1)
+ try:
+ while (b and ord(b) != 0xDA):
+ while (ord(b) != 0xFF): b = jpeg.read(1)
+ while (ord(b) == 0xFF): b = jpeg.read(1)
+ if (ord(b) >= 0xC0 and ord(b) <= 0xC3):
+ jpeg.read(3)
+ h, w = struct.unpack(">HH", jpeg.read(4))
+ break
+ else:
+ jpeg.read(int(struct.unpack(">H", jpeg.read(2))[0])-2)
+ b = jpeg.read(1)
+ width = int(w)
+ height = int(h)
+ except: pass
+
+ return content_type, width, height
=== Zope3/lib/python/Zope/App/OFS/Content/Image/ImageFields.py 1.1 => 1.2 ===
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""
+Revision information:
+$Id$
+"""
+
+from Zope.App.Formulator.FieldRegistry import getField
+from Zope.App.Formulator.ValidatorRegistry import getValidator
+
+
+ContentTypeField = getField('StringField')(
+ id = 'contentType',
+ title = 'Content Type',
+ description = 'The content type identifies the type of data.',
+ default = 'image/gif',
+ )
+
+
+DataField = getField('FileField')(
+ id = 'data',
+ title = 'Data',
+ description = 'The actual content of the object.'
+ )
=== Zope3/lib/python/Zope/App/OFS/Content/Image/__init__.py 1.1 => 1.2 ===
+#
+# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
=== Zope3/lib/python/Zope/App/OFS/Content/Image/image.zcml 1.1 => 1.2 ===
+ xmlns='http://namespaces.zope.org/zope'
+ xmlns:security='http://namespaces.zope.org/security'
+ xmlns:zmi='http://namespaces.zope.org/zmi'
+ xmlns:browser='http://namespaces.zope.org/browser'
+>
+ <security:permission id="Zope.AddImages" title="Add Images" />
+
+ <content class=".Image.">
+ <zmi:factory
+ id="Image"
+ permission="Zope.ManageContent"
+ title="Image"
+ description="An Image" />
+ <security:require
+ permission="Zope.View"
+ interface="Zope.App.OFS.Content.File.IFile.IReadFile" />
+ <security:require
+ permission="Zope.View"
+ interface=".Image.IImage" />
+ <security:require
+ permission="Zope.ManageContent"
+ interface="Zope.App.OFS.Content.File.IFile.IWriteFile" />
+ </content>
+
+ <adapter
+ factory="Zope.App.OFS.Annotation.AttributeAnnotations."
+ provides="Zope.App.OFS.Annotation.IAnnotations."
+ for=".Image." />
+
+ <!-- tabs for image -->
+
+ <zmi:tabs for=".Image.IImage.">
+ <zmi:tab label="Edit" action="editForm.html"/>
+ <zmi:tab label="View" action="."/>
+ <zmi:tab label="Role Permissions"
+ action="AllRolePermissions.html"/>
+ </zmi:tabs>
+
+
+ <!-- Further Directives -->
+
+ <include package=".Views" file="views.zcml" />
+
+</zopeConfigure>