void updateEntry() { // Read file MemChunk data; data.importFile(filename); // Read image SImage image; image.open(data, 0, "png"); image.convertPaletted(&palette); // Convert image to entry gfx format SIFormat* format = SIFormat::getFormat(gfx_format); if (format) { MemChunk conv_data; if (format->saveImage(image, conv_data, &palette)) { // Update entry data entry->importMemChunk(conv_data); EntryOperations::setGfxOffsets(entry, offsets.x, offsets.y); } else { LOG_MESSAGE(1, "Unable to convert external png to %s", format->getName()); } } }
bool convertWritable(SImage& image, convert_options_t opt) { // Firstly, make image paletted image.convertPaletted(opt.pal_target, opt.pal_current); // Secondly, remove any alpha information image.fillAlpha(255); // Quick hack for COLORMAP size // TODO: Remove me when a proper COLORMAP editor is implemented if (image.getWidth() == 256 && image.getHeight() >= 32 && image.getHeight() <= 34) return true; // Check for fullscreen/autopage size if (image.getWidth() == 320) return true; // And finally, find a suitable flat size and crop to that size int width = 0; int height = 0; for (unsigned a = 1; a < n_valid_flat_sizes; a++) { // Ignore non-writable flat sizes if (valid_flat_size[a][2] == 0) continue; // Check for exact match (no need to crop) if (image.getWidth() == valid_flat_size[a][0] && image.getHeight() == valid_flat_size[a][1]) return true; // If the flat will fit within this size, crop to the previous size // (this works because flat sizes list is in size-order) if (image.getWidth() <= (int)valid_flat_size[a][0] && image.getHeight() <= (int)valid_flat_size[a][1] && width > 0 && height > 0) { image.crop(0, 0, width, height); return true; } // Save 'previous' valid size width = valid_flat_size[a][0]; height = valid_flat_size[a][1]; } return false; }
/* EntryOperations::gfxConvert * Converts the image [entry] to [target_format], using conversion * options specified in [opt] and converting to [target_colformat] * colour format if possible. Returns false if the conversion failed, * true otherwise *******************************************************************/ bool EntryOperations::gfxConvert(ArchiveEntry* entry, string target_format, SIFormat::convert_options_t opt, int target_colformat) { // Init variables SImage image; // Get target image format SIFormat* fmt = SIFormat::getFormat(target_format); if (fmt == SIFormat::unknownFormat()) return false; // Check format and target colour type are compatible if (target_colformat >= 0 && !fmt->canWriteType((SIType)target_colformat)) { if (target_colformat == RGBA) wxLogMessage("Format \"%s\" cannot be written as RGBA data", fmt->getName()); else if (target_colformat == PALMASK) wxLogMessage("Format \"%s\" cannot be written as paletted data", fmt->getName()); return false; } // Load entry to image Misc::loadImageFromEntry(&image, entry); // Check if we can write the image to the target format int writable = fmt->canWrite(image); if (writable == SIFormat::NOTWRITABLE) { wxLogMessage("Entry \"%s\" could not be converted to target format \"%s\"", entry->getName(), fmt->getName()); return false; } else if (writable == SIFormat::CONVERTIBLE) fmt->convertWritable(image, opt); // Now we apply the target colour format (if any) if (target_colformat == PALMASK) image.convertPaletted(opt.pal_target, opt.pal_current); else if (target_colformat == RGBA) image.convertRGBA(opt.pal_current); // Finally, write new image data back to the entry fmt->saveImage(image, entry->getMCData(), opt.pal_target); return true; }