void BmpImage::toGrayscale() { if (bitsPerPixel != 8 && bitsPerPixel != 24) return; int w = rowBytes(); int avg; if (bitsPerPixel == 24) { ByteBuffer* bmpBuffer = new ByteBuffer(bitmapSize, bmpBits); BinaryReader* reader = new BinaryReader(bmpBuffer); byte blue, green, red; for (unsigned int i = 0; i < height; i++) { for (unsigned int j = 0; j < width; j++) { blue = reader->readByte(i*w+j*3); green = reader->readByte(i*w+j*3+1); red = reader->readByte(i*w+j*3+2); avg = (red + green + blue)/3; avg = (avg << 16) | (avg << 8) | (avg); bmpBuffer->seek(i*w+j*3); bmpBuffer->addRGB(avg); } } bmpBits = bmpBuffer->getBytes(); return; } int colorTableIndex; for (unsigned int i = 0; i < height; i++) { for (unsigned int j = 0; j < width; j++) { colorTableIndex = bmpBits[i*w+j]; avg = (colorPalette[colorTableIndex] & 0x00FF0000) >> 16; avg += (colorPalette[colorTableIndex] & 0x0000FF00) >> 8; avg += (colorPalette[colorTableIndex] & 0x000000FF); avg /= 3; bmpBits[i*w+j] = avg; } } for (int k = 0; k < colorPaletteSize; k++) { colorPalette[k] = (k << 16) | (k << 8) | (k); } }
uint Packet::getMessageLength(ushort lengthType, BinaryReader& reader) { uint length = 0; switch(lengthType) { case 1: length = static_cast<uint>(reader.readByte()); break; case 2: length = reader.readUShort(); break; case 3: length = static_cast<uint>( ((reader.readByte() & 255) << 16) + ((reader.readByte() & 255) << 8) + (reader.readByte() & 255)); break; default: break; } return length; }