î
nÐR\²*  ã               @   s¨   d  d l  m Z d d l m Z m Z d d „  Z d d „  Z d d	 „  Z Gd
 d „  d e ƒ Z d d „  Z	 d d „  Z
 Gd d „  d e ƒ Z Gd d „  d e ƒ Z d S)é   )Ú
BiffRecordé    )ÚpackÚunpackc             C   s   |  j  | ƒ S)N)Z	col_width)ÚsheetÚcol© r   ú//var/www/dbchiro/venv/build/xlwt/xlwt/Bitmap.pyÚ	_size_col   s    r
   c             C   s   |  j  | ƒ S)N)Z
row_height)r   Úrowr   r   r	   Ú	_size_row   s    r   c             C   s  x6 | t  |  | ƒ k r8 | t  |  | ƒ 8} | d 7} q Wx6 | t |  | ƒ k rq | t |  | ƒ 8} | d 7} q< W| } | } | | d } | | d } x6 | t  |  | ƒ k rÒ | t  |  | ƒ 8} | d 7} q Wx6 | t |  | ƒ k r| t |  | ƒ 8} | d 7} qÖ Wt  |  | ƒ d k s`t  |  | ƒ d k s`t |  | ƒ d k s`t |  | ƒ d k rdd St t | ƒ t  |  | ƒ d ƒ } t t | ƒ t |  | ƒ d ƒ } t t | ƒ t  |  | ƒ d ƒ }	 t t | ƒ t |  | ƒ d ƒ }
 | | | | | |	 | |
 f S)aú  Calculate the vertices that define the position of the image as required by
    the OBJ record.

             +------------+------------+
             |     A      |      B     |
       +-----+------------+------------+
       |     |(x1,y1)     |            |
       |  1  |(A1)._______|______      |
       |     |    |              |     |
       |     |    |              |     |
       +-----+----|    BITMAP    |-----+
       |     |    |              |     |
       |  2  |    |______________.     |
       |     |            |        (B2)|
       |     |            |     (x2,y2)|
       +---- +------------+------------+

    Example of a bitmap that covers some of the area from cell A1 to cell B2.

    Based on the width and height of the bitmap we need to calculate 8 vars:
        col_start, row_start, col_end, row_end, x1, y1, x2, y2.
    The width and height of the cells are also variable and have to be taken into
    account.
    The values of col_start and row_start are passed in from the calling
    function. The values of col_end and row_end are calculated by subtracting
    the width and height of the bitmap from the width and height of the
    underlying cells.
    The vertices are expressed as a percentage of the underlying cell width as
    follows (rhs values are in pixels):

           x1 = X / W *1024
           y1 = Y / H *256
           x2 = (X-1) / W *1024
           y2 = (Y-1) / H *256

           Where:  X is distance from the left side of the underlying cell
                   Y is distance from the top of the underlying cell
                   W is the width of the cell
                   H is the height of the cell

    Note: the SDK incorrectly states that the height should be expressed as a
    percentage of 1024.

    col_start  - Col containing upper left corner of object
    row_start  - Row containing top left corner of object
    x1  - Distance to left side of object
    y1  - Distance to top of object
    width  - Width of image frame
    height  - Height of image frame

    r   r   Ni   é   )r
   r   ÚintÚfloat)r   Ú	row_startÚ	col_startÚx1Úy1ÚwidthÚheightÚrow_endÚcol_endÚx2Úy2r   r   r	   Ú_position_image   s0    5**####r   c               @   s"   e  Z d  Z d Z d d „  Z d S)ÚObjBmpRecordé]   c	       3      C   sM  | j  | }	 | j | }
 t | | | | | |	 |
 ƒ } | \ } } } } } } } } d } d } d } d } | } | } | } | } | } | } | } | } d }  d }! d }" d }# d }$ d }% d }& d }' d }( d }) d }* d }+ d }, d }- d }. d }/ d }0 d }1 t d | ƒ }2 |2 t d | ƒ 7}2 |2 t d | ƒ 7}2 |2 t d | ƒ 7}2 |2 t d | ƒ 7}2 |2 t d | ƒ 7}2 |2 t d | ƒ 7}2 |2 t d | ƒ 7}2 |2 t d | ƒ 7}2 |2 t d | ƒ 7}2 |2 t d | ƒ 7}2 |2 t d | ƒ 7}2 |2 t d |  ƒ 7}2 |2 t d |! ƒ 7}2 |2 t d |" ƒ 7}2 |2 t d	 |# ƒ 7}2 |2 t d	 |$ ƒ 7}2 |2 t d	 |% ƒ 7}2 |2 t d	 |& ƒ 7}2 |2 t d	 |' ƒ 7}2 |2 t d	 |( ƒ 7}2 |2 t d	 |) ƒ 7}2 |2 t d	 |* ƒ 7}2 |2 t d |+ ƒ 7}2 |2 t d |, ƒ 7}2 |2 t d |- ƒ 7}2 |2 t d |. ƒ 7}2 |2 t d |/ ƒ 7}2 |2 t d |0 ƒ 7}2 |2 t d |1 ƒ 7}2 |2 |  _ d  S)
Nr   é   i  r   é	   éÿ   z<Lz<Hz<B)r   r   r   r   Ú	_rec_data)3Úselfr   r   r   Zim_data_bmpÚxÚyZscale_xZscale_yr   r   Zcoordinatesr   r   r   r   r   r   r   r   ZcObjZOTÚidZgrbitZcolLZdxLZrwTZdyTZcolRZdxRZrwBZdyBZcbMacroZ	Reserved1Z	Reserved2ZicvBackZicvForeZflsZfAutoZicvZlnsZlnwZfAutoBZfrsÚcfZ	Reserved3Z
cbPictFmlaZ	Reserved4Zgrbit2Z	Reserved5Údatar   r   r	   Ú__init__n   s‚    zObjBmpRecord.__init__N)Ú__name__Ú
__module__Ú__qualname__Ú_REC_IDr'   r   r   r   r	   r   k   s   r   c             C   s.   t  |  d ƒ  } | j ƒ  } Wd QXt | ƒ S)z±Convert a 24 bit bitmap into the modified internal format used by Windows.
    This is described in BITMAPCOREHEADER and BITMAPCOREINFO structures in the
    MSDN library.

    ÚrbN)ÚopenÚreadÚ_process_bitmap_data)ZbitmapÚfhr&   r   r   r	   Ú_process_bitmap¼   s    r1   c             C   sþ  t  |  ƒ d k r! t d ƒ ‚ n  |  d  d … d k rF t d ƒ ‚ n  |  d d  … }  t d |  d  d … ƒ d } | d 8} | d	 7} |  d d  … }  |  d	 d  … }  t d
 |  d  d … ƒ \ } } |  d d  … }  | d k rñ t d ƒ ‚ n  | d k rt d ƒ ‚ n  t d |  d  d … ƒ \ } } |  d d  … }  | d k rVt d ƒ ‚ n  | d k rqt d ƒ ‚ n  t d |  d  d … ƒ d } |  d d  … }  | d k r¹t d ƒ ‚ n  |  d d  … }  t d d	 | | d d ƒ } | |  }  | | | |  f S)Né6   z#bitmap doesn't contain enough data.é   s   BMz4bitmap doesn't appear to to be a valid bitmap image.z<Lé   r   é   z<LLr   iÿÿ  z-bitmap: largest image width supported is 65k.z.bitmap: largest image height supported is 65k.z<HHé   z'bitmap isn't a 24bit true color bitmap.r   z/bitmap: only 1 plane supported in bitmap image.z2bitmap: compression not supported in bitmap image.é   z<LHHHH)ÚlenÚ	Exceptionr   r   )r&   Úsizer   r   ZplanesZbitcountÚcompressionÚheaderr   r   r	   r/   È   s<    


r/   c               @   s.   e  Z d  Z d Z d d „  Z d d „  Z d S)ÚImRawDataBmpRecordé   c             C   s?   t  j |  ƒ t | ƒ \ |  _ |  _ |  _ } |  j | ƒ d S)z¡Insert a 24bit bitmap image in a worksheet. The main record required is
        IMDATA but it must be proceeded by a OBJ record to define its position.

        N)r   r'   r/   r   r   r:   Ú_write_imdata)r!   r&   r   r   r	   r'   ø   s    !zImRawDataBmpRecord.__init__c             C   s5   d } d } |  j  } t d | | | ƒ | |  _ d  S)Nr   r   z<HHL)r:   r   r    )r!   r&   r%   ÚenvZlcbr   r   r	   r?     s    	z ImRawDataBmpRecord._write_imdataN)r(   r)   r*   r+   r'   r?   r   r   r   r	   r=   õ   s   
r=   c               @   s   e  Z d  Z d d „  Z d S)ÚImDataBmpRecordc             C   s?   t  j |  ƒ t | ƒ \ |  _ |  _ |  _ } |  j | ƒ d S)z¡Insert a 24bit bitmap image in a worksheet. The main record required is
        IMDATA but it must be proceeded by a OBJ record to define its position.

        N)r   r'   r1   r   r   r:   r?   )r!   Úfilenamer&   r   r   r	   r'   
  s    !zImDataBmpRecord.__init__N)r(   r)   r*   r'   r   r   r   r	   rA   	  s   rA   N)ZBIFFRecordsr   Ústructr   r   r
   r   r   r   r1   r/   r=   rA   r   r   r   r	   Ú<module>   s   XQ-