7#include "absl/strings/str_format.h"
8#include "absl/strings/str_join.h"
29 const std::vector<uint8_t>& indexed_pixels) {
31 return absl::InvalidArgumentError(
32 absl::StrFormat(
"Sheet 0x%02X is out of range", sheet_id));
36 return absl::FailedPreconditionError(absl::StrFormat(
37 "Sheet 0x%02X is a 2bpp sheet; PNG import and export cover 3bpp "
42 return absl::FailedPreconditionError(absl::StrFormat(
43 "Sheet 0x%02X is not loaded as a 128x32 sheet", sheet_id));
45 const auto high = std::find_if(indexed_pixels.begin(), indexed_pixels.end(),
46 [](uint8_t index) { return index > 7; });
47 if (high != indexed_pixels.end()) {
48 const size_t at =
static_cast<size_t>(high - indexed_pixels.begin());
49 return absl::FailedPreconditionError(absl::StrFormat(
50 "Sheet 0x%02X uses color %d at (%zu, %zu); 3bpp sheets hold colors "
52 sheet_id, *high, at % 128, at / 128));
54 return absl::OkStatus();
70 uint16_t sheet_id,
const std::vector<uint8_t>& indexed_pixels,
78 uint16_t sheet_id,
const std::vector<uint8_t>& indexed_pixels,
87 return MakePreview(sheet_id, result);
92 const std::vector<uint8_t>& png_bytes,
97 std::vector<SheetPngImportPreview> previews;
98 previews.reserve(imports.size());
100 previews.push_back(MakePreview(sheet_import.sheet, sheet_import.result));
109 if (store ==
nullptr || !store->HasSheet(preview.
sheet_id) ||
111 return absl::FailedPreconditionError(absl::StrFormat(
112 "Sheet 0x%02X is not in this ROM's loaded graphics; import it again",
118 return absl::OkStatus();
122 if (undo_manager !=
nullptr) {
123 undo_manager->
Push(std::make_unique<GraphicsPixelEditAction>(
124 &state, std::move(diff),
125 absl::StrFormat(
"Import PNG into sheet 0x%02X", preview.
sheet_id)));
127 return absl::OkStatus();
132 return absl::StrFormat(
"Sheet 0x%02X: no change", preview.
sheet_id);
134 return absl::StrFormat(
135 "Sheet 0x%02X: block%s %s (%zu tile%s)", preview.
sheet_id,
The Rom class is used to load, save, and modify Rom data. This is a generic SNES ROM container and do...
Shared state between GraphicsEditor panel components.
Manages undo/redo stacks for a single editor.
void Push(std::unique_ptr< UndoAction > action)
Locates graphics sheets in the ROM and writes edited sheets back.
One ROM session's graphics sheet pixels, with a revision per sheet.
The Graphics editor's path for sheet pixel writes.
#define ASSIGN_OR_RETURN(type_variable_name, expression)
constexpr size_t kSheetPixels
SheetPngImportPreview MakePreview(uint16_t sheet_id, const zelda3::SheetImportResult &result)
absl::Status CheckSheetPixels(uint16_t sheet_id, const std::vector< uint8_t > &indexed_pixels)
Editors are the view controllers for the application.
absl::StatusOr< std::vector< uint8_t > > ExportSheetPixelsPng(uint16_t sheet_id, const std::vector< uint8_t > &indexed_pixels, const zelda3::SheetPalette &palette)
zelda3::GraphicsSheetStore * ActiveSheetStore(const GraphicsEditorState &state)
std::string DescribeSheetPngImport(const SheetPngImportPreview &preview)
"Sheet 0x20: blocks 0, 3 (5 tiles)" for the preview list.
absl::Status CommitSheetPixels(GraphicsEditorState &state, uint16_t sheet, absl::Span< const uint8_t > pixels)
absl::StatusOr< std::vector< SheetPngImportPreview > > PreviewRoomPngImport(const Rom &rom, const zelda3::RoomBackgroundSet &set, const std::vector< uint8_t > &png_bytes, const zelda3::SheetPalette &palette)
absl::StatusOr< SheetPngImportPreview > PreviewSheetPngImport(uint16_t sheet_id, const std::vector< uint8_t > &indexed_pixels, const std::vector< uint8_t > &png_bytes, const zelda3::SheetPalette &palette, int first_block)
SheetPixelDiff MakeSheetPixelDiff(uint16_t sheet, absl::Span< const uint8_t > before, absl::Span< const uint8_t > after)
The pixels that differ between two 4096-byte sheets.
absl::Status ApplySheetPngImport(const SheetPngImportPreview &preview, GraphicsEditorState &state, UndoManager *undo_manager)
std::vector< uint8_t > SnesTo8bppSheet(std::span< const uint8_t > sheet, int bpp, int num_sheets)
std::vector< uint8_t > IndexedToSnesSheet(std::span< const uint8_t > sheet, int bpp, int num_sheets)
absl::StatusOr< PngImage > DecodePng(const std::vector< uint8_t > &bytes)
absl::StatusOr< SheetImportResult > ImportSheetBlocksPng(const std::vector< uint8_t > &snes_3bpp, const util::PngImage &png, int first_block, const SheetPalette &palette)
Places a PNG's 16x16 blocks over a sheet from first_block.
GfxSheetStorageKind GetGfxSheetStorageKind(uint16_t sheet_id)
constexpr size_t kGfxSheetCount
std::array< SheetColor, 8 > SheetPalette
Colors for 3bpp indices 0-7. Index 0 is exported as transparent.
absl::StatusOr< std::vector< uint8_t > > ExportSheetBlocksPng(const std::vector< uint8_t > &snes_3bpp, const SheetPalette &palette, int first_block, int block_count)
absl::StatusOr< std::vector< RoomSheetImport > > ImportRoomBackgroundPng(const Rom &rom, const RoomBackgroundSet &set, const util::PngImage &png, const SheetPalette &palette)
PNG export and import for the Graphics editor.
#define RETURN_IF_ERROR(expr)
The pixels one undo step changes in one sheet.
One sheet's pixels after an import, and what changed.
std::vector< uint8_t > indexed_pixels
std::vector< int > changed_tiles
std::vector< int > changed_blocks
A decoded PNG: palette indices for indexed files, RGBA otherwise.
std::vector< uint8_t > snes_3bpp
std::vector< int > changed_tiles
std::vector< int > changed_blocks