U
    >Ke*                     @   sx   d dl mZ ddlmZmZ dd Zdd Zdd	 ZG d
d deZdd Z	dd Z
G dd deZG dd deZdS )   )
BiffRecord    )packunpackc                 C   s
   |  |S N)Z	col_width)sheetcol r	   //tmp/pip-unpacked-wheel-x1drl5t4/xlwt/Bitmap.py	_size_col   s    r   c                 C   s
   |  |S r   )Z
row_height)r   rowr	   r	   r
   	_size_row   s    r   c                 C   sp  |t | |kr&|t | |8 }|d7 }q |t| |krL|t| |8 }|d7 }q&|}|}|| d }|| d }|t | |kr|t | |8 }|d7 }ql|t| |kr|t| |8 }|d7 }qt | |dkst | |dkst| |dkst| |dkrdS t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   s6    5



r   c                   @   s   e Zd ZdZdd ZdS )ObjBmpRecord]   c	           3      C   s^  |j | }	|j| }
t||||||	|
}|\}}}}}}}}d}d}d}d}|}|}|}|}|}|}|}|}d} d}!d}"d}#d}$d}%d}&d}'d}(d})d}*d}+d},d}-d}.d}/d}0d}1td|}2|2td|7 }2|2td|7 }2|2td|7 }2|2td|7 }2|2td|7 }2|2td|7 }2|2td|7 }2|2td|7 }2|2td|7 }2|2td|7 }2|2td|7 }2|2td| 7 }2|2td|!7 }2|2td|"7 }2|2td	|#7 }2|2td	|$7 }2|2td	|%7 }2|2td	|&7 }2|2td	|'7 }2|2td	|(7 }2|2td	|)7 }2|2td	|*7 }2|2td|+7 }2|2td|,7 }2|2td|-7 }2|2td|.7 }2|2td|/7 }2|2td|07 }2|2td|17 }2|2| _d S )
Nr      i  r   	      <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}| }W 5 Q R X t|S )zConvert a 24 bit bitmap into the modified internal format used by Windows.
    This is described in BITMAPCOREHEADER and BITMAPCOREINFO structures in the
    MSDN library.

    rb)openread_process_bitmap_data)Zbitmapfhr(   r	   r	   r
   _process_bitmap   s    r3   c                 C   s`  t | dkrtd| d d dkr,td| dd  } td| d d d }|d8 }|d	7 }| dd  } | d	d  } td
| d d \}}| dd  } |dkrtd|dkrtdtd| d d \}}| dd  } |dkrtd|dkrtdtd| d d d }| dd  } |dkr.td| dd  } tdd	||dd}||  } |||| fS )N6   z#bitmap doesn't contain enough data.   s   BMz4bitmap doesn't appear to to be a valid bitmap image.r!      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
   r1      s<    
r1   c                   @   s    e Zd ZdZdd Zdd ZdS )ImRawDataBmpRecord   c                 C   s.   t |  t|\| _| _| _}| | dS zInsert 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<   _write_imdata)r#   r(   r	   r	   r
   r)      s    
zImRawDataBmpRecord.__init__c                 C   s&   d}d}| j }td|||| | _d S )Nr   r   z<HHL)r<   r   r"   )r#   r(   r'   envZlcbr	   r	   r
   rB     s    z ImRawDataBmpRecord._write_imdataN)r*   r+   r,   r-   r)   rB   r	   r	   r	   r
   r?      s   
r?   c                   @   s   e Zd Zdd ZdS )ImDataBmpRecordc                 C   s.   t |  t|\| _| _| _}| | dS rA   )r   r)   r3   r   r   r<   rB   )r#   filenamer(   r	   r	   r
   r)   
  s    
zImDataBmpRecord.__init__N)r*   r+   r,   r)   r	   r	   r	   r
   rD   	  s   rD   N)ZBIFFRecordsr   structr   r   r   r   r   r   r3   r1   r?   rD   r	   r	   r	   r
   <module>   s   XQ-