📄 tight.c
字号:
EncodeMonoRect32((CARD8 *)tightBeforeBuf, w, h); ((CARD32 *)tightAfterBuf)[0] = monoBackground; ((CARD32 *)tightAfterBuf)[1] = monoForeground; if (usePixelFormat24) { Pack24(tightAfterBuf, &cl->format, 2); paletteLen = 6; } else paletteLen = 8; memcpy(&updateBuf[ublen], tightAfterBuf, paletteLen); ublen += paletteLen; cl->rfbBytesSent[rfbEncodingTight] += 3 + paletteLen; break; case 16: EncodeMonoRect16((CARD8 *)tightBeforeBuf, w, h); ((CARD16 *)tightAfterBuf)[0] = (CARD16)monoBackground; ((CARD16 *)tightAfterBuf)[1] = (CARD16)monoForeground; memcpy(&updateBuf[ublen], tightAfterBuf, 4); ublen += 4; cl->rfbBytesSent[rfbEncodingTight] += 7; break; default: EncodeMonoRect8((CARD8 *)tightBeforeBuf, w, h); updateBuf[ublen++] = (char)monoBackground; updateBuf[ublen++] = (char)monoForeground; cl->rfbBytesSent[rfbEncodingTight] += 5; } return CompressData(cl, streamId, dataLen, tightConf[compressLevel].monoZlibLevel, Z_DEFAULT_STRATEGY);}static BoolSendIndexedRect(cl, w, h) rfbClientPtr cl; int w, h;{ int streamId = 2; int i, entryLen; if ( (ublen + TIGHT_MIN_TO_COMPRESS + 6 + paletteNumColors * cl->format.bitsPerPixel / 8) > UPDATE_BUF_SIZE ) { if (!rfbSendUpdateBuf(cl)) return FALSE; } /* Prepare tight encoding header. */ updateBuf[ublen++] = (streamId | rfbTightExplicitFilter) << 4; updateBuf[ublen++] = rfbTightFilterPalette; updateBuf[ublen++] = (char)(paletteNumColors - 1); /* Prepare palette, convert image. */ switch (cl->format.bitsPerPixel) { case 32: EncodeIndexedRect32((CARD8 *)tightBeforeBuf, w * h); for (i = 0; i < paletteNumColors; i++) { ((CARD32 *)tightAfterBuf)[i] = palette.entry[i].listNode->rgb; } if (usePixelFormat24) { Pack24(tightAfterBuf, &cl->format, paletteNumColors); entryLen = 3; } else entryLen = 4; memcpy(&updateBuf[ublen], tightAfterBuf, paletteNumColors * entryLen); ublen += paletteNumColors * entryLen; cl->rfbBytesSent[rfbEncodingTight] += 3 + paletteNumColors * entryLen; break; case 16: EncodeIndexedRect16((CARD8 *)tightBeforeBuf, w * h); for (i = 0; i < paletteNumColors; i++) { ((CARD16 *)tightAfterBuf)[i] = (CARD16)palette.entry[i].listNode->rgb; } memcpy(&updateBuf[ublen], tightAfterBuf, paletteNumColors * 2); ublen += paletteNumColors * 2; cl->rfbBytesSent[rfbEncodingTight] += 3 + paletteNumColors * 2; break; default: return FALSE; /* Should never happen. */ } return CompressData(cl, streamId, w * h, tightConf[compressLevel].idxZlibLevel, Z_DEFAULT_STRATEGY);}static BoolSendFullColorRect(cl, w, h) rfbClientPtr cl; int w, h;{ int streamId = 0; int len; if (ublen + TIGHT_MIN_TO_COMPRESS + 1 > UPDATE_BUF_SIZE) { if (!rfbSendUpdateBuf(cl)) return FALSE; } updateBuf[ublen++] = 0x00; /* stream id = 0, no flushing, no filter */ cl->rfbBytesSent[rfbEncodingTight]++; if (usePixelFormat24) { Pack24(tightBeforeBuf, &cl->format, w * h); len = 3; } else len = cl->format.bitsPerPixel / 8; return CompressData(cl, streamId, w * h * len, tightConf[compressLevel].rawZlibLevel, Z_DEFAULT_STRATEGY);}static BoolSendGradientRect(cl, w, h) rfbClientPtr cl; int w, h;{ int streamId = 3; int len; if (cl->format.bitsPerPixel == 8) return SendFullColorRect(cl, w, h); if (ublen + TIGHT_MIN_TO_COMPRESS + 2 > UPDATE_BUF_SIZE) { if (!rfbSendUpdateBuf(cl)) return FALSE; } if (prevRowBuf == NULL) prevRowBuf = (int *)xalloc(2048 * 3 * sizeof(int)); updateBuf[ublen++] = (streamId | rfbTightExplicitFilter) << 4; updateBuf[ublen++] = rfbTightFilterGradient; cl->rfbBytesSent[rfbEncodingTight] += 2; if (usePixelFormat24) { FilterGradient24(tightBeforeBuf, &cl->format, w, h); len = 3; } else if (cl->format.bitsPerPixel == 32) { FilterGradient32((CARD32 *)tightBeforeBuf, &cl->format, w, h); len = 4; } else { FilterGradient16((CARD16 *)tightBeforeBuf, &cl->format, w, h); len = 2; } return CompressData(cl, streamId, w * h * len, tightConf[compressLevel].gradientZlibLevel, Z_FILTERED);}static BoolCompressData(cl, streamId, dataLen, zlibLevel, zlibStrategy) rfbClientPtr cl; int streamId, dataLen, zlibLevel, zlibStrategy;{ z_streamp pz; int err; if (dataLen < TIGHT_MIN_TO_COMPRESS) { memcpy(&updateBuf[ublen], tightBeforeBuf, dataLen); ublen += dataLen; cl->rfbBytesSent[rfbEncodingTight] += dataLen; return TRUE; } pz = &cl->zsStruct[streamId]; /* Initialize compression stream if needed. */ if (!cl->zsActive[streamId]) { pz->zalloc = Z_NULL; pz->zfree = Z_NULL; pz->opaque = Z_NULL; err = deflateInit2 (pz, zlibLevel, Z_DEFLATED, MAX_WBITS, MAX_MEM_LEVEL, zlibStrategy); if (err != Z_OK) return FALSE; cl->zsActive[streamId] = TRUE; cl->zsLevel[streamId] = zlibLevel; } /* Prepare buffer pointers. */ pz->next_in = (Bytef *)tightBeforeBuf; pz->avail_in = dataLen; pz->next_out = (Bytef *)tightAfterBuf; pz->avail_out = tightAfterBufSize; /* Change compression parameters if needed. */ if (zlibLevel != cl->zsLevel[streamId]) { if (deflateParams (pz, zlibLevel, zlibStrategy) != Z_OK) { return FALSE; } cl->zsLevel[streamId] = zlibLevel; } /* Actual compression. */ if ( deflate (pz, Z_SYNC_FLUSH) != Z_OK || pz->avail_in != 0 || pz->avail_out == 0 ) { return FALSE; } return SendCompressedData(cl, tightAfterBufSize - pz->avail_out);}static Bool SendCompressedData(cl, compressedLen) rfbClientPtr cl; int compressedLen;{ int i, portionLen; updateBuf[ublen++] = compressedLen & 0x7F; cl->rfbBytesSent[rfbEncodingTight]++; if (compressedLen > 0x7F) { updateBuf[ublen-1] |= 0x80; updateBuf[ublen++] = compressedLen >> 7 & 0x7F; cl->rfbBytesSent[rfbEncodingTight]++; if (compressedLen > 0x3FFF) { updateBuf[ublen-1] |= 0x80; updateBuf[ublen++] = compressedLen >> 14 & 0xFF; cl->rfbBytesSent[rfbEncodingTight]++; } } portionLen = UPDATE_BUF_SIZE; for (i = 0; i < compressedLen; i += portionLen) { if (i + portionLen > compressedLen) { portionLen = compressedLen - i; } if (ublen + portionLen > UPDATE_BUF_SIZE) { if (!rfbSendUpdateBuf(cl)) return FALSE; } memcpy(&updateBuf[ublen], &tightAfterBuf[i], portionLen); ublen += portionLen; } cl->rfbBytesSent[rfbEncodingTight] += compressedLen; return TRUE;}/* * Code to determine how many different colors used in rectangle. */static voidFillPalette8(count) int count;{ CARD8 *data = (CARD8 *)tightBeforeBuf; CARD8 c0, c1; int i, n0, n1; paletteNumColors = 0; c0 = data[0]; for (i = 1; i < count && data[i] == c0; i++); if (i == count) { paletteNumColors = 1; return; /* Solid rectangle */ } if (paletteMaxColors < 2) return; n0 = i; c1 = data[i]; n1 = 0; for (i++; i < count; i++) { if (data[i] == c0) { n0++; } else if (data[i] == c1) { n1++; } else break; } if (i == count) { if (n0 > n1) { monoBackground = (CARD32)c0; monoForeground = (CARD32)c1; } else { monoBackground = (CARD32)c1; monoForeground = (CARD32)c0; } paletteNumColors = 2; /* Two colors */ }}#define DEFINE_FILL_PALETTE_FUNCTION(bpp) \ \static void \FillPalette##bpp(count) \ int count; \{ \ CARD##bpp *data = (CARD##bpp *)tightBeforeBuf; \ CARD##bpp c0, c1, ci; \ int i, n0, n1, ni; \ \ c0 = data[0]; \ for (i = 1; i < count && data[i] == c0; i++); \ if (i >= count) { \ paletteNumColors = 1; /* Solid rectangle */ \ return; \ } \ \ if (paletteMaxColors < 2) { \ paletteNumColors = 0; /* Full-color encoding preferred */ \ return; \ } \ \ n0 = i; \ c1 = data[i]; \ n1 = 0; \ for (i++; i < count; i++) { \ ci = data[i]; \ if (ci == c0) { \ n0++; \ } else if (ci == c1) { \ n1++; \ } else \ break; \ } \ if (i >= count) { \ if (n0 > n1) { \ monoBackground = (CARD32)c0; \ monoForeground = (CARD32)c1; \ } else { \ monoBackground = (CARD32)c1; \ monoForeground = (CARD32)c0; \ } \ paletteNumColors = 2; /* Two colors */ \ return; \
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -