rem: outDestination->Write(buf, rd); rd = inSource->Read(buf, 1024); }  if (rd == 0)
add: writ = outDestination->Write(buf, rd); if (writ < 0) break; remaining -= static_cast<uint32>(writ); rd = inSource->Read(buf, min(1024, remaining)); }  if (remaining > 0) return B_ERROR; else
context: translate_from_bits(BPositionIO *inSource, ssize_t amtread, uint8 *read,	bool bheaderonly, bool bdataonly, uint32 outType,	BPositionIO *outDestination){	TranslatorBitmap bitsHeader;			status_t result;	result = identify_bits_header(inSource, NULL, amtread, read, &bitsHeader);	if (result != B_OK)		return result;		// Translate B_TRANSLATOR_BITMAP to B_TRANSLATOR_BITMAP, easy enough :)		if (outType == B_TRANSLATOR_BITMAP) {		// write out bitsHeader (only if configured to)		if (bheaderonly || (!bheaderonly && !bdataonly)) {			if (swap_data(B_UINT32_TYPE, &bitsHeader,				sizeof(TranslatorBitmap), B_SWAP_HOST_TO_BENDIAN) != B_OK)				return B_ERROR;			if (outDestination->Write(&bitsHeader,				sizeof(TranslatorBitmap)) != sizeof(TranslatorBitmap))				return B_ERROR;		}				// write out the data (only if configured to)		if (bdataonly || (!bheaderonly && !bdataonly)) {				uint8 buf[1024];			ssize_t rd = inSource->Read(buf, 1024);			while (rd > 0) {				outDestination->Write(buf, rd);				rd = inSource->Read(buf, 1024);			}					if (rd == 0)				return B_OK;			else				return B_ERROR;		} else			return B_OK;			// Translate B_TRANSLATOR_BITMAP to B_BMP_FORMAT	} else if (outType == B_BMP_FORMAT) {		// Set up BMP header		BMPFileHeader fileHeader;		fileHeader.magic = 'MB';		fileHeader.reserved = 0;				MSInfoHeader msheader;		msheader.size = 40;		msheader.width = 			static_cast<uint32> (bitsHeader.bounds.Width() + 1);		msheader.height = 			static_cast<uint32> (bitsHeader.bounds.Height() + 1);		msheader.planes = 1;		msheader.xpixperm = 2835; // 72 dpi horizontal		msheader.ypixperm = 2835; // 72 dpi vertical		msheader.colorsused = 0;		msheader.colorsimportant = 0;				// determine fileSize / imagesize		switch (bitsHeader.colors) {			case B_RGB32:			case B_RGB32_BIG:			case B_RGBA32:			case B_RGBA32_BIG:			case B_RGB24:			case B_RGB24_BIG:			case B_RGB16:			case B_RGB16_BIG:			case B_RGB15:			case B_RGB15_BIG:			case B_RGBA15:			case B_RGBA15_BIG:			case B_CMYK32:			case B_CMY32:			case B_CMYA32:			case B_CMY24:					fileHeader.dataOffset = 54;				msheader.bitsperpixel = 24;				msheader.compression = BMP_NO_COMPRESS;				msheader.imagesize = get_rowbytes(msheader.width, 24) *					msheader.height;				fileHeader.fileSize = fileHeader.dataOffset +					msheader.imagesize;							break;							case B_CMAP8:			case B_GRAY8:							msheader.colorsused = 256;				msheader.colorsimportant = 256;				fileHeader.dataOffset = 54 + (4 * 256);				msheader.bitsperpixel = 8;				msheader.compression = BMP_NO_COMPRESS;				msheader.imagesize = get_rowbytes(msheader.width,					msheader.bitsperpixel) * msheader.height;				fileHeader.fileSize = fileHeader.dataOffset +					msheader.imagesize;									break;							case B_GRAY1:							msheader.colorsused = 2;				msheader.colorsimportant = 2;				fileHeader.dataOffset = 62;				msheader.bitsperpixel = 1;				msheader.compression = BMP_NO_COMPRESS;				msheader.imagesize = get_rowbytes(msheader.width,					msheader.bitsperpixel) * msheader.height;				fileHeader.fileSize = fileHeader.dataOffset +					msheader.imagesize;									break;							default:				return B_NO_TRANSLATOR;		}				// write out the BMP headers		if (bheaderonly || (!bheaderonly && !bdataonly)) {			result = write_bmp_headers(outDestination, fileHeader, msheader);			if (result != B_OK)				return result;		}		if (bheaderonly)			// if user only wants the header, bail out			// before the data is written			return result;		// write out the BMP pixel data		switch (bitsHeader.colors) {			case B_RGB32:			case B_RGB32_BIG:			case B_RGBA32:			case B_RGBA32_BIG:			case B_RGB24:			case B_RGB24_BIG:			case B_RGB16:			case B_RGB16_BIG:			case B_RGB15:			case B_RGB15_BIG:			case B_RGBA15:			case B_RGBA15_BIG:			case B_CMYK32:			case B_CMY32:			case B_CMYA32:			case B_CMY24:				return translate_from_bits_to_bmp24(inSource, outDestination,					bitsHeader.colors, msheader);								case B_CMAP8:			{				// write Be's system palette to the BMP file				uint8 pal[1024] = { 0 };				const color_map *pmap = system_colors();				if (!pmap)					return B_ERROR;				for (int32 i = 0; i < 256; i++) {					uint8 *palent = pal + (i * 4);					rgb_color c = pmap->color_list[i];					palent[0] = c.blue;					palent[1] = c.green;					palent[2] = c.red;				}				if (outDestination->Write(pal, 1024) != 1024)					return B_ERROR;				return translate_from_bits8_to_bmp8(inSource, outDestination,					bitsHeader.rowBytes, msheader);			}						case B_GRAY8:			{				// write out a grayscale palette to the BMP file				uint8 pal[1024] = { 0 };				for (int32 i = 0; i < 256; i++) {					uint8 *palent = pal + (i * 4);					palent[0] = i;					palent[1] = i;					palent[2] = i;				}				if (outDestination->Write(pal, 1024) != 1024)					return B_ERROR;				return translate_from_bits8_to_bmp8(inSource, outDestination,					bitsHeader.rowBytes, msheader);			}							case B_GRAY1:			{				// write monochrome palette to the BMP file				const uint32 monopal[] = { 0x00ffffff, 0x00000000 };				if (outDestination->Write(monopal, 8) != 8)					return B_ERROR;								return translate_from_bits1_to_bmp1(inSource, outDestination,					bitsHeader.rowBytes, msheader);			}							default:				return B_NO_TRANSLATOR;		}	} else		return B_NO_TRANSLATOR;}