⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 compress.cpp

📁 pocket printer的一个打印机驱动程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	comm[4] = (BYTE)(length/256);

	BOOL bRes=port.Write(comm, 5);

	if (bRes)
		bRes=port.Write(Data, length);
	if (bRes)
		bRes=port.WriteString("\x0A\x0D");
	if (bRes)
		bRes=port.Flush();
	return bRes;
}

inline BOOL Printer::Reset()
{
#ifdef _WIN32_WCE_EMULATION
	OutputDebugString(_T("Printer_Reset\n"));
	return TRUE;
#else
	BYTE comm[] = { 0x1B, 0x40 };
	if (!port.Write(comm, 2))
		return FALSE;
	return port.Flush();
#endif
}

inline BOOL Printer::LineFeed()
{
#ifdef _WIN32_WCE_EMULATION
	OutputDebugString(_T("Printer_LineFeed\n"));
	return TRUE;
#else
	BYTE comm[] = { 0x0A, 0x0D };
	if (!port.Write(comm, 2))
		return FALSE;
	return port.Flush();
#endif
}

inline BOOL Printer::FormFeed()
{
#ifdef _WIN32_WCE_EMULATION
	OutputDebugString(_T("Printer_FormFeed\n"));
	return TRUE;
#else
	BYTE comm[] = { 0x0C };
	if (!port.Write(comm, 1))
		return FALSE;
	return port.Flush();
#endif
}

BOOL Printer::FeedSpace(DWORD dwSpace) // UNIT: pixel
{
#ifdef _WIN32_WCE_EMULATION
	TCHAR szMsg[MAX_PATH];
	wsprintf(szMsg, _T("Printer_FeedSpace(%d)\n"), dwSpace);
	OutputDebugString(szMsg);
	return TRUE;
#else
	BYTE comm[] = { 0x1B, 0x2B, 0xFF, 0x0A, 0x0D, 0x1B, 0x2B, 64 };

 	while (dwSpace)
	{
		if (dwSpace > 255)
			dwSpace -= 255;
		else
		{
			comm[2] = (BYTE)dwSpace;
			dwSpace = 0;
		}

		if (!port.Write(comm, 8))
			return FALSE;
		if (!port.Flush())
			return FALSE;
	}
	return TRUE;
#endif
}

BOOL Printer::HorzSpace(DWORD dwSpace) // UNIT: pixel
{
#ifdef _WIN32_WCE_EMULATION
	OutputDebugString(_T("Printer_HorzSpace\n"));
	return TRUE;
#else
	BYTE comm[] = { 0x1B, 0x7B, 0, 0 };

	if (dwSpace)
	{
		comm[2] = (BYTE)(dwSpace%256);
		comm[3] = (BYTE)(dwSpace/256);

		if (!port.Write(comm, 4))
			return FALSE;
		if (!port.Flush())
			return FALSE;
	}
	return TRUE;
#endif
}

BOOL Printer::SendCompressData(PBYTE pData, DWORD dwLen)
{
#ifdef _WIN32_WCE_EMULATION
	OutputDebugString(_T("Printer_SendCompressData\n"));
	return TRUE;
#else
	BYTE comm[] = { 0x1B, 0x2A, 0x33, 0, 0 };

	comm[3] = (BYTE)(dwLen%256);
	comm[4] = (BYTE)(dwLen/256);
	if (!port.Write(comm, 5))
		return FALSE;
	if (!port.Write(pData, dwLen))
		return FALSE;
	return port.Flush();
#endif
}

inline BOOL Printer::RasterRow(BYTE * Row)//, BYTE * Seed)
{
//    TRACE(TRUE, (TEXT("A6PRN: RasterRow\r\n")));
    int WidthOfRowInBytes, j;
    BOOL f = TRUE;

    // Ignore trailing white.
    //
    WidthOfRowInBytes = WidthOfRasterAreaInBytes;
/*
    while (  WidthOfRowInBytes > 1 
          && Row[WidthOfRowInBytes - 1] == 0xFF
          )
    {
        WidthOfRowInBytes--;
    }
*/
	for (j=0; j<WidthOfRowInBytes; j++)
	{
		BYTE b = Row[j*3];
		BYTE g = Row[j*3+1];
		BYTE r = Row[j*3+2];
		BYTE gray = GrayScale(r, g, b);
		pixel[j*8+(iRow%8)] = gray;
	}

    // If the current plane is all zero, then just skip the plane.
    //
	/*
    if (Row[0] == 0 && WidthOfRowInBytes == 1)
    {
        // Bring Seed Row up to date.
        memset(Seed, 0, WidthOfRasterAreaInBytes);
    }
    else
    {
        // Update the Seed Row
        //
        memcpy(Seed, Row, WidthOfRasterAreaInBytes);
    }
	*/
	iRow++;
	if ((iRow%8) == 0)
	{
		PrepareImage(pixel, data, ((iRow-1)/8)%8, WidthOfRasterAreaInBytes);
		memset(pixel, 0xFF, 12000);
		if ((iRow%64) == 0)
		{
			DWORD dwLen = Compress(data, pData.data, 8*WidthOfRasterAreaInBytes);
			if (bSkip)
			{
				dwScroll += 64;
			}
			else
			{
				if (dwScroll)
				{
					FeedSpace(dwScroll);
					dwScroll = 0;
				}

				if (!SendCompressData(pData.data, dwLen))
					return FALSE;
				if (!LineFeed())
					return FALSE;
			}

			memset(data, 0x0, 12000);
		}
	}

    return f;
}

inline void RGB2HSV(int r, int g, int b, int * h, int * s, int * v)
{
    int m;
    int d;

    m  = min(min(r, g), b);

    *v = max(max(r, g), b);

    d  = *v - m;

    if (*v)
        *s = MAX_8 * d / *v;
    else 
        *s = 0;

    if (d) {
        if (r == *v)
            *h = MAX_8 * (g - b) / d;
        else if (g == *v)
            *h = MAX_8 * (b - r) / d + (MAX_8 + 1) * 2;
        else 
            *h = MAX_8 * (r - g) / d + (MAX_8 + 1) * 4;

        if (*h < 0)
            *h += (MAX_8 + 1) * 6;
    }
}

inline void HSV2RGB(int * r, int * g, int * b, int h, int s, int v)
{
    int i;
    int f;
    int p;
    int q;
    int t;

    if (s == 0) {
        *r = *g = *b = v;
    }
    else {
        i = h / (MAX_8 + 1);
        f = h - i * (MAX_8 + 1);
        p = v * (MAX_8 - s) / MAX_8;
        q = v * (MAX_8 * MAX_8 - (s * f)) / MAX_8 / MAX_8;
        t = v * (MAX_8 * MAX_8 - s * (MAX_8 - f)) / MAX_8 / MAX_8;
        switch (i) {
        case 0: *r = v; *g = t; *b = p; break;
        case 1: *r = q; *g = v; *b = p; break;
        case 2: *r = p; *g = v; *b = t; break;
        case 3: *r = p; *g = q; *b = v; break;
        case 4: *r = t; *g = p; *b = v; break;
        case 5: *r = v; *g = p; *b = q; break;
        }
    }
}

BOOL Printer::ColorRow(BYTE *Row)
{
//    TRACE(TRUE, (TEXT("A6PRN: ColorRow\r\n")));

    int i;

    if (fColor) {
        int r;
        int g;
        int b;
        int h;
        int s;
        int v;


        for (i = 0; i < WidthPixels; i++) {
            b = Row[i * 3 + 0];
            g = Row[i * 3 + 1];
            r = Row[i * 3 + 2];

            if (!f3Pen && b == g && g == r) {
                pK[i] = pGK[MAX_8 - b];
                pY[i] = pM[i] = pC[i] = 0;
            }
            else {
                RGB2HSV(r, g, b, &h, &s, &v);

                v = MAX_8 - pGV[MAX_8 - v];

                if (b > r && b > g) {
                    if (h < (MAX_8 + 1) * 4) 
                        h = iHueB * (h - (MAX_8 + 1) * 3) / MAX_8 + (MAX_8 + 1) * 3;
                    else 
                        h = ((MAX_8 + 1) * 2 - iHueB) * (h - (MAX_8 + 1) * 5) / MAX_8 + (MAX_8 + 1) * 5;
                }
                else if (g > r && g > b) {
                    if (h < (MAX_8 + 1) * 2) 
                        h = iHueG * (h - (MAX_8 + 1)) / MAX_8 + (MAX_8 + 1);
                    else 
                        h = ((MAX_8 + 1) * 2 - iHueG) * (h - (MAX_8 + 1) * 3) / MAX_8 + (MAX_8 + 1) * 3;
                }

                HSV2RGB(&r, &g, &b, h, s, v);

                pY[i] = pGC[MAX_8 - b];
                pM[i] = pGC[MAX_8 - g];
                pC[i] = pGC[MAX_8 - r];

                if (!f3Pen) {
                    pK[i] = min(min(pY[i], pM[i]), pC[i]);

                    pY[i] -= pK[i];
                    pM[i] -= pK[i];
                    pC[i] -= pK[i];
                }
            }
        }       
    }
    else {
        for (i = 0; i < WidthPixels; i++) {
            pK[i] = pGK[MAX_8 -
                (Row[i * 3 + 0] * 256 * 11 / 100 +
                 Row[i * 3 + 1] * 256 * 59 / 100 +
                 Row[i * 3 + 2] * 256 * 30 / 100) / 256];
        }       
    }

    if (fColor) {
        if (f3Pen) {
            if (!Diffuse(pC, pEC, pScanRow, 0, iDotC, pSC))
                return FALSE;
            if (!Diffuse(pM, pEM, pScanRow, 0, iDotM, pSM))
                return FALSE;
            if (!Diffuse(pY, pEY, pScanRow, 0, iDotY, pSY))
                return FALSE;
        }
        else {
            if (!Diffuse(pK, pEK, pBlackRow, 0, iDotK, pSK))
                return FALSE;
            if (!Diffuse(pC, pEC, pScanRow, pBlackRow, iDotC, pSC))
                return FALSE;
            if (!Diffuse(pM, pEM, pScanRow, pBlackRow, iDotM, pSM))
                return FALSE;
            if (!Diffuse(pY, pEY, pScanRow, pBlackRow, iDotY, pSY))
                return FALSE;
        }
    }
    else if (!Diffuse(pK, pEK, pBlackRow, 0, iDotK, pSK))
        return FALSE;

    iRow++;

    return TRUE;
}

BOOL Printer::Diffuse(short * Value, short * Error, BYTE * Scan, BYTE * Mask, int Dot, BYTE * Seed)
{
//    TRACE(TRUE, (TEXT("A6PRN: Diffuse\r\n")));

    int i;
    int j;
    int k;
    int iByte;
    int iBit;
    int iE;
    int iE1;
    int iE3;
    int iE5;
    int iE7;
    int iR;
    short * p;
    

    memset(Scan, 0, WidthOfRasterAreaInBytes);
    memset(pError, 0, (WidthPixels + 2) * sizeof(short));
    p   = pError + 1;
    iE7 = 0;
    iR  = 0;

    if (!(iRow & 1)) {
        iByte = 0; 
        iBit  = 0;
        i     = 0;
        j     = 1;
    }
    else {
        iByte = WidthOfRasterAreaInBytes - 1; 
        iBit  = (WidthPixels - 1) % 8;
        i     = WidthPixels - 1;
        j     = -1;
    }
    // Loop
    //
    for (k = 0; k < WidthPixels; k++) {
        if ((Value[i] == 0) || 
            (Mask && (Mask[iByte] & BitMask[iBit]))) {
            iE = Value[i] + Error[i] + iE7;
        }
        else if (Value[i] == MAX_14) {
            Scan[iByte] |= BitMask[iBit];
            iE = Error[i] + iE7;
        }
        else {
            iE = rand() * (Value[i] - MAX_13) / RAND_MAX;
            if (((iR > 0) && (iE > 0)) ||
                ((iR < 0) && (iE < 0))) 
                iE = -iE;
            iR = iE;

            Value[i] += Error[i] + iE7;
            
            if (Value[i] + iR > MAX_13) {
                Scan[iByte] |= BitMask[iBit];
                iE = Value[i] - Dot;
            }
            else {
                iE = Value[i];
            }
        }

        if (iE) {
            iE1 = (iE)     / 16;
            iE3 = (iE * 3) / 16;
            iE5 = (iE * 5) / 16;
            iE7 = iE - iE1 - iE3 - iE5;

            p[i + j] += iE1;
            p[i]     += iE5;
            p[i - j] += iE3;
        }
        else
            iE7 = 0;

        iBit += j;
        if (iBit > 7) {
            iBit = 0;
            iByte++;
        }
        else if (iBit < 0) {
            iBit = 7;
            iByte--;
        }

        i += j;
    }

    memcpy(Error, p, WidthPixels * sizeof(short));
    
    return RasterRow(Scan);//, Seed);
}

BYTE
GrayScale(
	BYTE	r,
	BYTE	g,
	BYTE	b
	)
{
	BYTE gray;

	gray = (r*2 + g*7 + b*1) / 10;
	return gray;
}



void
PrepareImage(
	LPBYTE	pixel,	// gray scale bitmap data
	LPBYTE	dot,	// output black/white data
	DWORD	dwLine,	// output in 
	DWORD	dwWidth // bitmap width
	)
{
	DWORD	i, j;

	for (j=0; j<8; j++)
	{
		for (i=0; i<dwWidth; i++)
		{
			dot[i*8+dwLine] >>= 1;

			if (pixel[i*8+j] <= 127)
			{
				dot[i*8+dwLine] |= 0x80;
			}
		}
	}
}


// util.cpp
DWORD
Compress(
	LPBYTE	oData,
	LPBYTE	nData,
	DWORD dwDataLen
	)
{
	DWORD	dwIndex;
	DWORD	dwCount;
	DWORD	dwLength;

	dwIndex = 0;
	dwLength = 0;
	dwCount = 0;
	bSkip = TRUE;

	while (dwIndex < dwDataLen)
	{
		if (oData[dwIndex] == 0x0)
		{
			do
			{
				dwCount++;
				dwIndex++;
			} while ((oData[dwIndex]==0x0) && (dwIndex<dwDataLen));

			while (dwCount != 0)
			{
				nData[dwLength] = 0x0;
				dwLength++;

				if (dwCount > 254)
				{
					nData[dwLength] = 254;
					dwCount -= 254;
				}
				else
				{
					nData[dwLength] = (BYTE)dwCount;
					dwCount = 0;
				}

				dwLength++;
			}
		}
		else if (oData[dwIndex] == 0XFF)
		{
			bSkip = FALSE;

			do
			{
				dwCount++;
				dwIndex++;
			} while ((oData[dwIndex]==0xFF) && (dwIndex<dwDataLen));

			while (dwCount != 0)
			{
				nData[dwLength] = 0xFF;
				dwLength++;

				if (dwCount > 254)
				{
					nData[dwLength] = 254;
					dwCount -= 254;
				}
				else
				{
					nData[dwLength] = (BYTE)dwCount;
					dwCount = 0;
				}

				dwLength++;
			}
		}
		else
		{
			bSkip = FALSE;
			nData[dwLength] = oData[dwIndex];
			dwLength++;
			dwIndex++;
		}
	}

	return dwLength;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -