/*
 * synergy -- mouse and keyboard sharing utility
 * Copyright (C) 2004 Chris Schoeneman
 * Copyright (C) 2006 Knut St. Osmundsen
 * 
 * This package is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * found in the file COPYING that should have accompanied this file.
 * 
 * This package is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include "CPMClipboardBitmapConverter.h"
#include "CLog.h"

//
// CPMClipboardBitmapConverter
//

CPMClipboardBitmapConverter::CPMClipboardBitmapConverter()
{
	// do nothing
}

CPMClipboardBitmapConverter::~CPMClipboardBitmapConverter()
{
	// do nothing
}

IClipboard::EFormat
CPMClipboardBitmapConverter::getFormat() const
{
	return IClipboard::kBitmap;
}

ULONG
CPMClipboardBitmapConverter::getPMFormat() const
{
	return CF_BITMAP;
}

ULONG
CPMClipboardBitmapConverter::getPMFormatInfo() const
{
	return CFI_HANDLE;
}

ULONG
CPMClipboardBitmapConverter::fromIClipboard(const CString& data) const
{
    /*
     * We're getting a windows DIB, create a PM bitmap from that.
     */
    /** @todo implement me */
    return 0;
}

void 
CPMClipboardBitmapConverter::freePMData(ULONG ulPMData) const
{
    HBITMAP hbmp = (HBITMAP)ulPMData;
    if (hbmp != NULLHANDLE) {
        /** @todo implement me */
    }
}

CString
CPMClipboardBitmapConverter::toIClipboard(ULONG data) const
{
#if 1
    /** @todo implement me */
    return CString();
#else
    HBITMAP hbmp = (HBITMAP)data;

    BITMAPINFOHEADER2 BmpHdr;

    ULONG	 cbFix;
    ULONG	 cx;
    ULONG	 cy;
    USHORT cPlanes;
    USHORT cBitCount;
    ULONG	 ulCompression;
    ULONG	 cbImage;
    ULONG	 cxResolution;
    ULONG	 cyResolution;
    ULONG	 cclrUsed;
    ULONG	 cclrImportant;
    USHORT usUnits;
    USHORT usReserved;
    USHORT usRecording;
    USHORT usRendering;
    ULONG	 cSize1;
    ULONG	 cSize2;
    ULONG	 ulColorEncoding;
    ULONG	 ulIdentifier;

    if (GpiQueryBitmapInfoHeader(hbmp, &BmpHdr)) {
        LOG((CLOG_INFO "bitmap: %dx%d %d", bmpData.biWidth, bitmap->bmiHeader.biHeight, (int)bitmap->bmiHeader.biBitCount));
        if (    BmpHdr.cPlanes == 1
            &&  (BmpHdr.cBitsCount == 32 || BmpHdr.cBitsCount == 24 /*???*/)
            &&   BmpHdr.ulCompression == ) {
            // already in canonical form
            char *abImage = new char[BmpHdr.cbImage];
            /* fun stuff starts here. */
            //if (GipQueryBitmapBits() {
            }
            CString image(abImage, srcSize);
            delete abImage;
            return image;
        }
        
    }
    return CString();

	// get datator
	const char* src = (const char*)GlobalLock(data);
	if (src == NULL) {
		return CString();
	}
	UInt32 srcSize = (UInt32)GlobalSize(data);

	// check image type
	const BITMAPINFO* bitmap = reinterpret_cast<const BITMAPINFO*>(src);
	LOG((CLOG_INFO "bitmap: %dx%d %d", bitmap->bmiHeader.biWidth, bitmap->bmiHeader.biHeight, (int)bitmap->bmiHeader.biBitCount));
	if (bitmap->bmiHeader.biPlanes == 1 &&
		(bitmap->bmiHeader.biBitCount == 24 ||
		bitmap->bmiHeader.biBitCount == 32) &&
		bitmap->bmiHeader.biCompression == BI_RGB) {
		// already in canonical form
		CString image(src, srcSize);
		GlobalUnlock(data);
		return image;
	}

	// create a destination DIB section
	LOG((CLOG_INFO "convert image from: depth=%d comp=%d", bitmap->bmiHeader.biBitCount, bitmap->bmiHeader.biCompression));
	void* raw;
	BITMAPINFOHEADER info;
	LONG w               = bitmap->bmiHeader.biWidth;
	LONG h               = bitmap->bmiHeader.biHeight;
	info.biSize          = sizeof(BITMAPINFOHEADER);
	info.biWidth         = w;
	info.biHeight        = h;
	info.biPlanes        = 1;
	info.biBitCount      = 32;
	info.biCompression   = BI_RGB;
	info.biSizeImage     = 0;
	info.biXPelsPerMeter = 1000;
	info.biYPelsPerMeter = 1000;
	info.biClrUsed       = 0;
	info.biClrImportant  = 0;
	HDC dc      = GetDC(NULL);
	HBITMAP dst = CreateDIBSection(dc, (BITMAPINFO*)&info,
							DIB_RGB_COLORS, &raw, NULL, 0);

	// find the start of the pixel data
	const char* srcBits = (const char*)bitmap + bitmap->bmiHeader.biSize;
	if (bitmap->bmiHeader.biBitCount >= 16) {
		if (bitmap->bmiHeader.biCompression == BI_BITFIELDS &&
			(bitmap->bmiHeader.biBitCount == 16 ||
			bitmap->bmiHeader.biBitCount == 32)) {
			srcBits += 3 * sizeof(ULONG);
		}
	}
	else if (bitmap->bmiHeader.biClrUsed != 0) {
		srcBits += bitmap->bmiHeader.biClrUsed * sizeof(RGBQUAD);
	}
	else {
		srcBits += (1 << bitmap->bmiHeader.biBitCount) * sizeof(RGBQUAD);
	}

	// copy source image to destination image
	HDC dstDC         = CreateCompatibleDC(dc);
	HGDIOBJ oldBitmap = SelectObject(dstDC, dst);
	SetDIBitsToDevice(dstDC, 0, 0, w, h, 0, 0, 0, h,
							srcBits, bitmap, DIB_RGB_COLORS);
	SelectObject(dstDC, oldBitmap);
	DeleteDC(dstDC);
	GdiFlush();

	// extract data
	CString image((const char*)&info, info.biSize);
	image.append((const char*)raw, 4 * w * h);

	// clean up GDI
	DeleteObject(dst);
	ReleaseDC(NULL, dc);

	// release handle
	GlobalUnlock(data);

	return image;
#endif
}

