# The Translation Kit: BBitmapStream

Derived from: public BPositionIO

Declared in: be/translation/BitmapStream.h

Library: libtranslation.so

## Overview

BBitmapStream allows for the easy conversion of a translator bitmap, the default Translation Kit bitmap format, to a BBitmap. It's a very limited subclass of BPositionIO capable only of reading and writing translator bitmaps; performing i/o on the object with other types of data can yield strange results. The main attraction of the class is the `DetachBitmap()` method, which returns the contents of the object as a BBitmap.

Most of the time, you won't need to use BBitmapStream directly; BTranslationUtils contains the functionality required to load images from files, resources, or general BPositionIOs into BBitmaps.

The following snippet illustrates typical usage of BBitmapStream sans proper error checking:

       BBitmap *FetchBitmap(char *filename)
       {
          BFile file(filename, B_READ_ONLY);
          BTranslatorRoster *roster = BTranslatorRoster::Default();
          BBitmapStream stream;
          BBitmap *result = NULL;
          if (roster->Translate(&file, NULL, NULL, &stream,
                B_TRANSLATOR_BITMAP) < B_OK)
             return NULL;
          stream.DetachBitmap(&result);
          return result;
       }

You can also initialize the class with a BBitmap and use it as input to `BTranslatorRoster::Translate()` to save it in a different format:

       void StoreTranslatorBitmap(BBitmap *bitmap, char *filename, uint32 type)
       {
          BTranslatorRoster *roster = BTranslatorRoster::Default();
          BBitmapStream stream(bitmap); // init with contents of bitmap
          BFile file(filename, B_CREATE_FILE | B_WRITE_ONLY);

          roster->Translate(&stream, NULL, NULL, &file, type);
       }

## Constructor and Destructor

### BBitmapStream()

          BBitmapStream(BBitmap *map = NULL)

Creates a new instance of BBitmapStream. If map is `NULL`, the stream is initialized to be empty. Otherwise, the BBitmap is converted to a translator bitmap and placed in the stream. The application shares the BBitmap with the BBitmapStream object. It therefore shouldn't `delete` the BBitmap without first calling `DetachBitmap()`.

### ~BBitmapStream()

          ~BBitmapStream()

Frees all memory allocated by the BTranslatorRoster.

## Member Functions

### DetachBitmap()

          status_t DetachBitmap(BBitmap **outMap)

Returns, in outMap, a BBitmap representing the image contained in the BBitmapStream. Once `DetachBitmap()` has been called, no further operations should be performed on the BBitmapStream.

RETURN CODES

``

- B_NO_ERROR. Success.``
- B_BAD_VALUE. outMap is NULL.
- `B_ERROR`. There is no BBitmap available.

### Position(), ReadAt(), Seek(), SetSize(), WriteAt()

          off_t Position() const

          ssize_t ReadAt(off_t pos, void *buffer, size_t *size)

          off_t Seek(off_t position, uint32 whence)

          status_t SetSize(off_t size) const

          ssize_t WriteAt(off_t pos, const void *data, size_t *size)

These methods provide the implementation for the BPositionIO. The class functions identically to BPositionIO with the exception of `ReadAt()` and `WriteAt()`, which read and write only translator bitmaps as described in the class introduction.

See also: `BPositionIO`

### Size()

          off_t Size() const

Returns the number of bytes in the translator bitmap in the stream.

The Be Book, in lovely HTML, for BeOS Release 3.

Copyright © 1998 Be, Inc. All rights reserved.

Last modified March 26, 1998.