rem: if (rd == 0)
add: if (remaining > 0) return B_ERROR; else
context: translate_from_bits(BPositionIO *inSource, ssize_t amtread, uint8 *read,	TGATranslatorSettings &settings, uint32 outType,	BPositionIO *outDestination){	TranslatorBitmap bitsHeader;	bool bheaderonly, bdataonly, brle;	bheaderonly = settings.SetGetHeaderOnly();	bdataonly = settings.SetGetDataOnly();	brle = settings.SetGetRLE();			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_TGA_FORMAT	} else if (outType == B_TGA_FORMAT) {		// Set up TGA header		TGAFileHeader fileheader;		fileheader.idlength = 0;		fileheader.colormaptype = TGA_NO_COLORMAP;		fileheader.imagetype = 0;				TGAColorMapSpec mapspec;		mapspec.firstentry = 0;		mapspec.length = 0;		mapspec.entrysize = 0;				TGAImageSpec imagespec;		imagespec.xorigin = 0;		imagespec.yorigin = 0;		imagespec.width = static_cast<uint16> (bitsHeader.bounds.Width() + 1);		imagespec.height = static_cast<uint16> (bitsHeader.bounds.Height() + 1);		imagespec.depth = 0;		imagespec.descriptor = TGA_ORIGIN_VERT_BIT;				// determine fileSize / imagesize		switch (bitsHeader.colors) {					// Output to 32-bit True Color TGA (8 bits alpha)			case B_RGBA32:			case B_RGBA32_BIG:			case B_CMYA32:				if (brle)					fileheader.imagetype = TGA_RLE_TRUECOLOR;				else					fileheader.imagetype = TGA_NOCOMP_TRUECOLOR;				imagespec.depth = 32;				imagespec.descriptor |= 8;					// 8 bits of alpha				break;						// Output to 24-bit True Color TGA (no alpha)			case B_RGB32:			case B_RGB32_BIG:			case B_RGB24:			case B_RGB24_BIG:			case B_CMYK32:			case B_CMY32:			case B_CMY24:				if (brle)					fileheader.imagetype = TGA_RLE_TRUECOLOR;				else					fileheader.imagetype = TGA_NOCOMP_TRUECOLOR;				imagespec.depth = 24;				break;					// Output to 16-bit True Color TGA (no alpha)			// (TGA doesn't see 16 bit images as Be does			// so converting 16 bit Be Image to 16-bit TGA			// image would result in loss of quality)			case B_RGB16:			case B_RGB16_BIG:					if (brle)					fileheader.imagetype = TGA_RLE_TRUECOLOR;				else					fileheader.imagetype = TGA_NOCOMP_TRUECOLOR;				imagespec.depth = 24;				break;						// Output to 15-bit True Color TGA (1 bit alpha)			case B_RGB15:			case B_RGB15_BIG:				if (brle)					fileheader.imagetype = TGA_RLE_TRUECOLOR;				else					fileheader.imagetype = TGA_NOCOMP_TRUECOLOR;				imagespec.depth = 16;				imagespec.descriptor |= 1;					// 1 bit of alpha (always opaque)				break;							// Output to 16-bit True Color TGA (1 bit alpha)			case B_RGBA15:			case B_RGBA15_BIG:				if (brle)					fileheader.imagetype = TGA_RLE_TRUECOLOR;				else					fileheader.imagetype = TGA_NOCOMP_TRUECOLOR;				imagespec.depth = 16;				imagespec.descriptor |= 1;					// 1 bit of alpha				break;							// Output to 8-bit Color Mapped TGA 32 bits per color map entry			case B_CMAP8:				fileheader.colormaptype = TGA_COLORMAP;				if (brle)					fileheader.imagetype = TGA_RLE_COLORMAP;				else					fileheader.imagetype = TGA_NOCOMP_COLORMAP;				mapspec.firstentry = 0;				mapspec.length = 256;				mapspec.entrysize = 32;				imagespec.depth = 8;				imagespec.descriptor |= 8;					// the pixel values contain 8 bits of attribute data				break;							// Output to 8-bit Black and White TGA			case B_GRAY8:			case B_GRAY1:				if (brle)					fileheader.imagetype = TGA_RLE_BW;				else					fileheader.imagetype = TGA_NOCOMP_BW;				imagespec.depth = 8;				break;							default:				return B_NO_TRANSLATOR;		}				// write out the TGA headers		if (bheaderonly || (!bheaderonly && !bdataonly)) {			result = write_tga_headers(outDestination, fileheader,				mapspec, imagespec);			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 TGA 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:				result = translate_from_bits_to_tgatc(inSource, outDestination,					bitsHeader.colors, imagespec, brle);				break;								case B_CMAP8:			{				// write Be's system palette to the TGA file				uint8 pal[1024];				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;					palent[3] = c.alpha;				}				if (outDestination->Write(pal, 1024) != 1024)					return B_ERROR;				result = translate_from_bits_to_tgatc(inSource, outDestination,					B_GRAY8, imagespec, brle);				break;			}						case B_GRAY8:				result = translate_from_bits_to_tgatc(inSource, outDestination,					B_GRAY8, imagespec, brle);				break;							case B_GRAY1:				result = translate_from_bits1_to_tgabw(inSource, outDestination,					bitsHeader.rowBytes, imagespec, brle);				break;							default:				result = B_NO_TRANSLATOR;				break;		}				if (result == B_OK)			result = write_tga_footer(outDestination);					return result;	} else		return B_NO_TRANSLATOR;}