bool CIwGameImage::Init(void* memory_file, int memory_file_size) { CIwGameFile file; if (file.Open(memory_file, memory_file_size)) { // Load the image CIwImage image; image.ReadFile((s3eFile*)file.getFileHandle()); // if required format is different then convert it if (FormatSet && image.GetFormat() != Format) { CIwImage* new_image = ConvertToFormat(image, Format); Image2D = Iw2DCreateImage(*new_image); delete new_image; } else Image2D = Iw2DCreateImage(image); if (FilterSet) setFilter(Filter); if (Image2D == NULL || image.GetFormat() == CIwImage::FORMAT_UNDEFINED) { #if defined(_DEBUG) CIwGameError::LogError("Error: CIwGameImage::Init() - Could not create image!"); #endif // _DEBUG return false; } else { Width = Image2D->GetWidth(); Height = Image2D->GetHeight(); #if defined(_DEBUG) CIwGameError::LogError("Info: CIwGameImage::Init() - Size = ", CIwGameString(memory_file_size).c_str()); CIwGameError::LogError("Info: CIwGameImage::Init() - Width = ", CIwGameString(Width).c_str()); CIwGameError::LogError("Info: CIwGameImage::Init() - Height = ", CIwGameString(Height).c_str()); CIwGameError::LogError("Info: CIwGameImage::Init() - Bit depth = ", CIwGameString(image.GetBitDepth()).c_str()); CIwGameError::LogError("Info: CIwGameImage::Init() - Format = ", CIwGameString(image.GetFormat()).c_str()); #endif // _DEBUG } } // Sanity check if (Width <= 0 || Height <= 0 || Width > 16384 || Height > 16384) return false; State = State_Loaded; return true; }
CSourceDataGenerator::EMGL_font_t * CSourceDataGenerator::LoadEMGLFont(const CharMap &charmap, bool compress, enum OutputFormat format) { EMGL_font_t *result = new EMGL_font_t; int ascender = 0; int ascenderDiv = 0; result->bpp = GetBpp(format); result->numCodepages = charmap.GetCountCodePages(); result->codePagePtrs = new EMGL_codePage_t[result->numCodepages]; std::list<CodePage *>::const_iterator it = charmap.CBegin(); for (int i = 0; i < result->numCodepages; ++i, ++it) { wxASSERT(it != charmap.CEnd()); wxASSERT(*it != NULL); EMGL_codePage_t *outCodepage = &result->codePagePtrs[i]; outCodepage->startCode = (*it)->GetRangeStart(); outCodepage->endCode = (*it)->GetRangeEnd(); const wxString &name = (*it)->GetName(); for (unsigned int j = 0; j < 16; ++j) { outCodepage->name[j] = j < name.Length() ? name.ToAscii()[j] : '\0'; } outCodepage->name[16] = '\0'; int numGlyphs = outCodepage->endCode - outCodepage->startCode + 1; outCodepage->glyphPtrs = new EMGL_glyph_t *[numGlyphs]; for (int j = 0; j < numGlyphs; ++j) { const CharMapEntry *entry = (*it)->GetCharMapEntry((*it)->GetRangeStart() + j); if (entry == NULL) { outCodepage->glyphPtrs[j] = NULL; continue; } LoadFont(entry->GetFamily(), entry->GetStyle(), entry->GetSize(), entry->GetEncodingID()); wxUint32 glyph; if (!m_loadedFont.IsOk() || (glyph = m_loadedFont.GetGlyphIndex(entry->GetCode())) == 0) { wxLogWarning("Could not load glyph with code %u", (*it)->GetRangeStart() + j); outCodepage->glyphPtrs[j] = NULL; continue; } int bitmapWidth, bitmapHeight; int bitmapSize = m_loadedFont.GetGlyphBitmap(glyph, NULL, &bitmapWidth, &bitmapHeight); if (bitmapSize < 0) { wxLogWarning("Error while getting bitmap for glyph with code %u", (*it)->GetRangeStart() + j); outCodepage->glyphPtrs[j] = NULL; continue; } wxUint8 *bitmap = new wxUint8[bitmapSize]; int result = m_loadedFont.GetGlyphBitmap(glyph, bitmap, &bitmapWidth, &bitmapHeight); wxASSERT(result == bitmapSize); bitmapSize = ConvertToFormat(bitmap, bitmapSize, format); if (compress) { CompressBitmap(&bitmap, &bitmapSize, bitmapWidth * bitmapHeight, format); } ascender += m_loadedFont.GetAscender(); ascenderDiv++; wxPoint advance = m_loadedFont.GetGlyphAdvance(glyph); wxPoint bearing = m_loadedFont.GetGlyphBearing(glyph); outCodepage->glyphPtrs[j] = new EMGL_glyph_t; outCodepage->glyphPtrs[j]->advanceX = advance.x; outCodepage->glyphPtrs[j]->advanceY = advance.y; outCodepage->glyphPtrs[j]->bearingX = bearing.x; outCodepage->glyphPtrs[j]->bearingY = bearing.y; outCodepage->glyphPtrs[j]->bitmapWidth = bitmapWidth; outCodepage->glyphPtrs[j]->bitmapHeight = bitmapHeight; outCodepage->glyphPtrs[j]->bitmapSize = bitmapSize; outCodepage->glyphPtrs[j]->bitmapData = bitmap; } } ascender = ascenderDiv > 0 ? ascender / ascenderDiv : 0; wxASSERT(ascender >= INT16_MIN && ascender <= INT16_MAX); result->ascender = ascender; return result; }
void BinaryDataGenerator::SaveCharmap(const CharMap &charmap, bool compress, enum OutputFormat format, const wxString &filePath) { // Try opening the file for writing wxFFileOutputStream file(filePath); if (!file.IsOk()) { wxLogError(_("Could not open the file for writing")); return; } wxDataOutputStream out(file); out.BigEndianOrdered(false); out.UseBasicPrecisions(); int numCodePages = charmap.GetCountCodePages(); int headerSize = sizeof(wxUint32) + sizeof(wxInt16) + 2 * sizeof(wxUint8) + numCodePages * sizeof(wxUint32); int codePageBaseSize = sizeof(wxUint32) * 2 + 16; wxASSERT(numCodePages <= UINT8_MAX); if(compress) wxLogDebug("Compression not supported yet"); // TODO: add compression // Write header wxUint32 magic = 'EMGF'; wxUint8 flags = format; if (compress) flags |= FLAG_COMPRESSED; int ascender = 0; int ascenderDiv = 0; out.Write32(magic); file.SeekO(sizeof(wxInt16), wxFromCurrent); out.Write8(flags); out.Write8((wxUint8)numCodePages); wxUint32 *codepagePtrs = new wxUint32[numCodePages]; wxUint32 **glyphPtrs = new wxUint32*[numCodePages]; // Write codepages file.SeekO(headerSize); std::list<CodePage *>::const_iterator it = charmap.CBegin(); for (int i = 0; i < numCodePages; i++) { wxASSERT(it != charmap.CEnd()); codepagePtrs[i] = file.TellO(); out.Write32((*it)->GetRangeStart()); out.Write32((*it)->GetRangeEnd()); const wxString &name = (*it)->GetName(); for (unsigned int j = 0; j < 16; ++j) { out.Write8(j < name.Length() ? name.ToAscii()[j] : '\0'); } glyphPtrs[i] = new wxUint32[(*it)->GetSize()]; file.SeekO(sizeof(wxUint32) * (*it)->GetSize(), wxFromCurrent); } // Write glyphs it = charmap.CBegin(); for (int i = 0; i < numCodePages; i++) { wxASSERT(it != charmap.CEnd()); for (unsigned int j = 0; j < (*it)->GetSize(); j++) { const CharMapEntry *entry = (*it)->GetCharMapEntry((*it)->GetRangeStart() + j); if (entry == NULL) { glyphPtrs[i][j] = 0; continue; } LoadFont(entry->GetFamily(), entry->GetStyle(), entry->GetSize(), entry->GetEncodingID()); wxUint32 glyph; if (!m_loadedFont.IsOk() || (glyph = m_loadedFont.GetGlyphIndex(entry->GetCode())) == 0) { wxLogWarning("Could not load glyph with code %u", (*it)->GetRangeStart() + j); glyphPtrs[i][j] = 0; continue; } wxPoint advance = m_loadedFont.GetGlyphAdvance(glyph); wxPoint bitmapTL = m_loadedFont.GetGlyphBitmapTL(glyph); int bitmapWidth, bitmapHeight; int bitmapSize = m_loadedFont.GetGlyphBitmap(glyph, NULL, &bitmapWidth, &bitmapHeight); if (bitmapSize < 0) { wxLogWarning("Error while getting bitmap for glyph with code %u", (*it)->GetRangeStart() + j); glyphPtrs[i][j] = 0; continue; } wxUint8 *bitmap = new wxUint8[bitmapSize]; int result = m_loadedFont.GetGlyphBitmap(glyph, bitmap, &bitmapWidth, &bitmapHeight); wxASSERT(result == bitmapSize); bitmapSize = ConvertToFormat(bitmap, bitmapSize, format); if (compress) { CompressBitmap(&bitmap, &bitmapSize, bitmapWidth * bitmapHeight, format); } ascender += m_loadedFont.GetAscender(); ascenderDiv++; glyphPtrs[i][j] = file.TellO(); out.Write16((wxInt16)advance.x); out.Write16((wxInt16)advance.y); out.Write16((wxInt16)bitmapTL.x); out.Write16((wxInt16)bitmapTL.y); out.Write16((wxUint16)bitmapWidth); out.Write16((wxUint16)bitmapHeight); out.Write32(bitmapSize); file.Write(bitmap, bitmapSize & ~(1<<31)); delete[] bitmap; } } // Write glyph pointers it = charmap.CBegin(); for (int i = 0; i < numCodePages; i++) { wxASSERT(it != charmap.CEnd()); file.SeekO(codepagePtrs[i] + codePageBaseSize); for (unsigned int j = 0; j < (*it)->GetSize(); j++) { out.Write32(glyphPtrs[i][j]); } delete[] glyphPtrs[i]; file.Sync(); } delete[] glyphPtrs; // Write codepage pointers file.SeekO(headerSize - numCodePages * sizeof(wxUint32)); for (int i = 0; i < numCodePages; i++) { out.Write32(codepagePtrs[i]); } delete[] codepagePtrs; delete[] glyphPtrs; // Write font wide parameters file.SeekO(sizeof(wxUint32)); ascender = ascenderDiv > 0 ? ascender / ascenderDiv : 0; wxASSERT(ascender >= INT16_MIN && ascender <= INT16_MAX); out.Write16(ascender); file.Close(); }