# Path: pdfminer/pdftypes.py
# LITERALS_DCT_DECODE = (LIT('DCTDecode'), LIT('DCT'))
# 
# Path: pdfminer/pdfcolor.py
# LITERAL_DEVICE_GRAY = LIT('DeviceGray')
# 
# Path: pdfminer/pdfcolor.py
# LITERAL_DEVICE_RGB = LIT('DeviceRGB')
# 
# Path: pdfminer/pdfcolor.py
# LITERAL_DEVICE_CMYK = LIT('DeviceCMYK')


import struct
import os
import os.path
from io import BytesIO
from .pdftypes import LITERALS_DCT_DECODE
from .pdfcolor import LITERAL_DEVICE_GRAY
from .pdfcolor import LITERAL_DEVICE_RGB
from .pdfcolor import LITERAL_DEVICE_CMYK
                    from PIL import Image
                    from PIL import ImageChops
        self.fp = fp
        self.bits = bits
        self.width = width
        self.height = height
        if bits == 1:
            ncols = 2
        elif bits == 8:
            ncols = 256
        elif bits == 24:
            ncols = 0
        else:
            raise ValueError(bits)
        self.linesize = align32((self.width*self.bits+7)//8)
        self.datasize = self.linesize * self.height
        headersize = 14+40+ncols*4
        info = struct.pack('<IiiHHIIIIII', 40, self.width, self.height, 1, self.bits, 0, self.datasize, 0, 0, ncols, 0)
        assert len(info) == 40, len(info)
        header = struct.pack('<ccIHHI', b'B', b'M', headersize+self.datasize, 0, 0, headersize)
        assert len(header) == 14, len(header)
        self.fp.write(header)
        self.fp.write(info)
        if ncols == 2:
            # B&W color table
            for i in (0, 255):
                self.fp.write(struct.pack('BBBx', i, i, i))
        elif ncols == 256:
            # grayscale color table
            for i in range(256):
                self.fp.write(struct.pack('BBBx', i, i, i))
        self.pos0 = self.fp.tell()

# Path: pdfminer/pdftypes.py
# LITERALS_DCT_DECODE = (LIT('DCTDecode'), LIT('DCT'))
# 
# Path: pdfminer/pdfcolor.py
# LITERAL_DEVICE_GRAY = LIT('DeviceGray')
# 
# Path: pdfminer/pdfcolor.py
# LITERAL_DEVICE_RGB = LIT('DeviceRGB')
# 
# Path: pdfminer/pdfcolor.py
# LITERAL_DEVICE_CMYK = LIT('DeviceCMYK')


# Path: pdfminer/image.py
import struct
import os
import os.path
from io import BytesIO
from .pdftypes import LITERALS_DCT_DECODE
from .pdfcolor import LITERAL_DEVICE_GRAY
from .pdfcolor import LITERAL_DEVICE_RGB
from .pdfcolor import LITERAL_DEVICE_CMYK
                    from PIL import Image
                    from PIL import ImageChops

        self.fp = fp
        self.bits = bits
        self.width = width
        self.height = height
        if bits == 1:
            ncols = 2
        elif bits == 8:
            ncols = 256
        elif bits == 24:
            ncols = 0
        else:
            raise ValueError(bits)
        self.linesize = align32((self.width*self.bits+7)//8)
        self.datasize = self.linesize * self.height
        headersize = 14+40+ncols*4
        info = struct.pack('<IiiHHIIIIII', 40, self.width, self.height, 1, self.bits, 0, self.datasize, 0, 0, ncols, 0)
        assert len(info) == 40, len(info)
        header = struct.pack('<ccIHHI', b'B', b'M', headersize+self.datasize, 0, 0, headersize)
        assert len(header) == 14, len(header)
        self.fp.write(header)
        self.fp.write(info)
        if ncols == 2:
            # B&W color table
            for i in (0, 255):
                self.fp.write(struct.pack('BBBx', i, i, i))
        elif ncols == 256:
            # grayscale color table
            for i in range(256):
                self.fp.write(struct.pack('BBBx', i, i, i))
        self.pos0 = self.fp.tell()

        self.pos1 = self.pos0 + self.datasize