/* Dump data from stream into a BBitmap */ status_t GetBitmap(BPositionIO *in, BBitmap **out) { TranslatorBitmap header; status_t err = in->Read(&header, sizeof(header)); if (err != sizeof(header)) return B_IO_ERROR; header.magic = B_BENDIAN_TO_HOST_INT32(header.magic); header.bounds.left = B_BENDIAN_TO_HOST_FLOAT(header.bounds.left); header.bounds.top = B_BENDIAN_TO_HOST_FLOAT(header.bounds.top); header.bounds.right = B_BENDIAN_TO_HOST_FLOAT(header.bounds.right); header.bounds.bottom = B_BENDIAN_TO_HOST_FLOAT(header.bounds.bottom); header.rowBytes = B_BENDIAN_TO_HOST_INT32(header.rowBytes); header.colors = (color_space)B_BENDIAN_TO_HOST_INT32(header.colors); header.dataSize = B_BENDIAN_TO_HOST_INT32(header.dataSize); BBitmap *bitmap = new BBitmap(header.bounds, header.colors); *out = bitmap; if (bitmap == NULL) return B_NO_MEMORY; unsigned char *bits = (unsigned char *)bitmap->Bits(); if (bits == NULL) { delete bitmap; return B_NO_MEMORY; } err = in->Read(bits, header.dataSize); if (err == (status_t)header.dataSize) return B_OK; else { delete bitmap; return B_IO_ERROR; } }
status_t GetBitmap(BPositionIO* in, BBitmap** out) { TranslatorBitmap header; status_t result = in->Read(&header, sizeof(header)); if (result != sizeof(header)) return B_IO_ERROR; header.magic = B_BENDIAN_TO_HOST_INT32(header.magic); header.bounds.left = B_BENDIAN_TO_HOST_FLOAT(header.bounds.left); header.bounds.top = B_BENDIAN_TO_HOST_FLOAT(header.bounds.top); header.bounds.right = B_BENDIAN_TO_HOST_FLOAT(header.bounds.right); header.bounds.bottom = B_BENDIAN_TO_HOST_FLOAT(header.bounds.bottom); header.rowBytes = B_BENDIAN_TO_HOST_INT32(header.rowBytes); header.colors = (color_space)B_BENDIAN_TO_HOST_INT32(header.colors); header.dataSize = B_BENDIAN_TO_HOST_INT32(header.dataSize); // dump data from stream into a BBitmap *out = new(std::nothrow) BBitmap(header.bounds, header.colors); if (*out == NULL) return B_NO_MEMORY; if (!(*out)->IsValid()) { delete *out; return B_NO_MEMORY; } result = in->Read((*out)->Bits(), header.dataSize); if (result != (status_t)header.dataSize) { delete *out; return B_IO_ERROR; } return B_OK; }
Layer* Layer::readLayerOldStyle(BFile& file, ImageView* imageView, int32 new_id) { // // Layer has stored the following things: // // 1. Layer frame (i.e. the frame of bitmap) // // 2. Layer id // // 3. Layer type // // 4. Layer visibility // // 5. Bitmap data // // BRect layer_frame; if (file.Read(&layer_frame,sizeof(BRect)) != sizeof(BRect)) return NULL; // The layer id is written to the file, so it must be read also, but it will // not be used. Instead we use the id that is provided as a parameter int32 id; // This is not actually used. if (file.Read(&id,sizeof(uint32)) != sizeof(uint32)) return NULL; id = B_BENDIAN_TO_HOST_INT32(id); layer_type layerType; if ((file.Read(&layerType, sizeof(uint32)) != sizeof(uint32))) return NULL; bool visi; if (file.Read(&visi,sizeof(bool)) != sizeof(bool)) return NULL; // Old files project-files are all big-endian so we convert data here. layer_frame.left = B_BENDIAN_TO_HOST_FLOAT(layer_frame.left); layer_frame.right = B_BENDIAN_TO_HOST_FLOAT(layer_frame.right); layer_frame.top = B_BENDIAN_TO_HOST_FLOAT(layer_frame.top); layer_frame.bottom = B_BENDIAN_TO_HOST_FLOAT(layer_frame.bottom); layerType =layer_type(B_BENDIAN_TO_HOST_INT32(layerType)); if (layerType != HS_NORMAL_LAYER) return NULL; // Create the layer Layer* layer = new Layer(layer_frame, new_id, imageView, layerType); layer->SetVisibility(visi); int8* bits = (int8*)layer->Bitmap()->Bits(); // align the file pointer to four-byte boundary. int32 alignment_offset; alignment_offset = (4 - (file.Position() % 4)) % 4; if (file.Read(bits,alignment_offset) != alignment_offset) { delete layer; return NULL; } bits += alignment_offset; if (file.Read(bits,layer->Bitmap()->BitsLength()-alignment_offset) != (layer->Bitmap()->BitsLength()-alignment_offset)) { delete layer; return NULL; } // Before returning calculate the layer's miniature image. layer->calc_mini_image(); return layer; }
Layer* Layer::readLayer(BFile& file, ImageView* imageView, int32 new_id, bool is_little_endian, int32 compression_method) { // This is the new way of reading the layers. int32 marker; if (file.Read(&marker,sizeof(int32)) != sizeof(int32)) return NULL; if (is_little_endian) marker = B_LENDIAN_TO_HOST_INT32(marker); else marker = B_BENDIAN_TO_HOST_INT32(marker); if (marker != PROJECT_FILE_LAYER_START_MARKER) return NULL; int32 width; int32 height; layer_type layerType; int32 layer_visibility; int64 length; if (file.Read(&width,sizeof(int32)) != sizeof(int32)) return NULL; if (file.Read(&height,sizeof(int32)) != sizeof(int32)) return NULL; if (file.Read(&layerType,sizeof(int32)) != sizeof(int32)) return NULL; if (file.Read(&layer_visibility,sizeof(int32)) != sizeof(int32)) return NULL; if (file.Read(&length,sizeof(int64)) != sizeof(int64)) return NULL; if (is_little_endian) { width = B_LENDIAN_TO_HOST_INT32(width); height = B_LENDIAN_TO_HOST_INT32(height); layerType = layer_type(B_LENDIAN_TO_HOST_INT32(layerType)); length = B_LENDIAN_TO_HOST_INT64(length); } else { width = B_BENDIAN_TO_HOST_INT32(width); height = B_BENDIAN_TO_HOST_INT32(height); layerType = layer_type(B_BENDIAN_TO_HOST_INT32(layerType)); length = B_BENDIAN_TO_HOST_INT64(length); } Layer* layer = new Layer(BRect(0, 0, width - 1, height - 1), new_id, imageView, layerType); layer->SetVisibility((uint32(layer_visibility) == 0xFFFFFFFF)); int8* bits = (int8*)layer->Bitmap()->Bits(); if (file.Read(bits,length) != length) { delete layer; return NULL; } // Read the end-marker. if (file.Read(&marker,sizeof(int32)) != sizeof(int32)) { delete layer; return NULL; } if (is_little_endian) marker = B_LENDIAN_TO_HOST_INT32(marker); else marker = B_BENDIAN_TO_HOST_INT32(marker); if (marker != PROJECT_FILE_LAYER_END_MARKER) { delete layer; return NULL; } // Here try to read the extra-data block. if (file.Read(&marker,sizeof(int32)) == sizeof(int32)) { if (is_little_endian) marker = B_LENDIAN_TO_HOST_INT32(marker); else marker = B_BENDIAN_TO_HOST_INT32(marker); if (marker == PROJECT_FILE_LAYER_EXTRA_DATA_START_MARKER) { // Read the length of this section int32 length; if (file.Read(&length,sizeof(int32)) != sizeof(int32)) { delete layer; return NULL; } if (is_little_endian) length = B_LENDIAN_TO_HOST_INT32(length); else length = B_BENDIAN_TO_HOST_INT32(length); // Read the transparency coefficient float coeff; if (file.Read(&coeff,sizeof(float)) != sizeof(float)) { delete layer; return NULL; } if (is_little_endian) coeff = B_LENDIAN_TO_HOST_FLOAT(coeff); else coeff = B_BENDIAN_TO_HOST_FLOAT(coeff); layer->SetTransparency(coeff); length -= sizeof(float); // Skip the extra data that we do not recognize. file.Seek(length,SEEK_CUR); // Here we should get the end-marker for layer's extra data if (file.Read(&marker,sizeof(int32)) != sizeof(int32)) { delete layer; return NULL; } if (is_little_endian) marker = B_LENDIAN_TO_HOST_INT32(marker); else marker = B_BENDIAN_TO_HOST_INT32(marker); if (marker != PROJECT_FILE_LAYER_EXTRA_DATA_END_MARKER) { delete layer; return NULL; } } else { // Somehow -sizeof(int32) does not seem to work???? file.Seek(-4,SEEK_CUR); } } // Before returning calculate the layer's miniature image. layer->calc_mini_image(); return layer; }