yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
visual_analysis_tool.h
Go to the documentation of this file.
1
12#ifndef YAZE_SRC_CLI_SERVICE_AGENT_TOOLS_VISUAL_ANALYSIS_TOOL_H_
13#define YAZE_SRC_CLI_SERVICE_AGENT_TOOLS_VISUAL_ANALYSIS_TOOL_H_
14
15#include <cstdint>
16#include <map>
17#include <string>
18#include <vector>
19
20#include "absl/status/status.h"
21#include "absl/status/statusor.h"
23
24namespace yaze {
25namespace cli {
26namespace agent {
27namespace tools {
28
34 double similarity_score; // 0.0 - 100.0
36 int x_position; // X position in sheet (pixels)
37 int y_position; // Y position in sheet (pixels)
38};
39
45 int x;
46 int y;
47 int width;
48 int height;
49 int tile_count; // Number of 8x8 tiles in region
50};
51
59 std::vector<int> used_by_maps; // Map IDs using this palette
60};
61
69 std::vector<int> locations; // Map/room IDs where used
70};
71
82 public:
83 // Constants for SNES graphics (public for testing)
84 static constexpr int kTileWidth = 8;
85 static constexpr int kTileHeight = 8;
86 static constexpr int kTilePixels = kTileWidth * kTileHeight; // 64
87 static constexpr int kSheetWidth = 128; // pixels
88 static constexpr int kSheetHeight = 32; // pixels per sheet row
89 static constexpr int kTilesPerRow = kSheetWidth / kTileWidth; // 16
90 static constexpr int kMaxSheets = 223;
91
92 protected:
99 double ComputePixelDifference(const std::vector<uint8_t>& tile_a,
100 const std::vector<uint8_t>& tile_b) const;
101
110 double ComputeStructuralSimilarity(const std::vector<uint8_t>& tile_a,
111 const std::vector<uint8_t>& tile_b) const;
112
120 absl::StatusOr<std::vector<uint8_t>> ExtractTile(Rom* rom, int sheet_index,
121 int tile_index) const;
122
131 absl::StatusOr<std::vector<uint8_t>> ExtractTileAtPosition(Rom* rom,
132 int sheet_index,
133 int x,
134 int y) const;
135
141 bool IsRegionEmpty(const std::vector<uint8_t>& data) const;
142
148 int GetTileCountForSheet(int sheet_index) const;
149
153 std::string FormatMatchesAsJson(
154 const std::vector<TileSimilarityMatch>& matches) const;
155
159 std::string FormatRegionsAsJson(
160 const std::vector<UnusedRegion>& regions) const;
161
165 std::string FormatPaletteUsageAsJson(
166 const std::vector<PaletteUsageStats>& stats) const;
167
171 std::string FormatHistogramAsJson(
172 const std::vector<TileUsageEntry>& entries) const;
173};
174
185 public:
186 std::string GetName() const override { return "visual-find-similar-tiles"; }
187
188 std::string GetDescription() const {
189 return "Find tiles with similar patterns to a reference tile";
190 }
191
192 std::string GetUsage() const override {
193 return "visual-find-similar-tiles --tile_id <id> [--sheet <index>] "
194 "[--threshold <0-100>] [--method <pixel|structural>]";
195 }
196
197 protected:
198 absl::Status ValidateArgs(const resources::ArgumentParser& parser) override;
199
200 absl::Status Execute(Rom* rom, const resources::ArgumentParser& parser,
201 resources::OutputFormatter& formatter) override;
202
203 bool RequiresLabels() const override { return false; }
204};
205
216 public:
217 std::string GetName() const override { return "visual-analyze-spritesheet"; }
218
219 std::string GetDescription() const {
220 return "Identify unused regions in graphics sheets for ROM hacking";
221 }
222
223 std::string GetUsage() const override {
224 return "visual-analyze-spritesheet [--sheet <index>] [--tile_size <8|16>] "
225 "[--format <json|text>]";
226 }
227
228 protected:
229 absl::Status ValidateArgs(const resources::ArgumentParser& parser) override;
230
231 absl::Status Execute(Rom* rom, const resources::ArgumentParser& parser,
232 resources::OutputFormatter& formatter) override;
233
234 bool RequiresLabels() const override { return false; }
235
236 private:
240 std::vector<UnusedRegion> FindUnusedRegions(Rom* rom, int sheet_index,
241 int tile_size) const;
242
246 std::vector<UnusedRegion> MergeAdjacentRegions(
247 const std::vector<UnusedRegion>& regions) const;
248};
249
260 public:
261 std::string GetName() const override { return "visual-palette-usage"; }
262
263 std::string GetDescription() const {
264 return "Analyze palette usage statistics across maps";
265 }
266
267 std::string GetUsage() const override {
268 return "visual-palette-usage [--type <overworld|dungeon|all>] "
269 "[--format <json|text>]";
270 }
271
272 protected:
273 absl::Status ValidateArgs(const resources::ArgumentParser& parser) override;
274
275 absl::Status Execute(Rom* rom, const resources::ArgumentParser& parser,
276 resources::OutputFormatter& formatter) override;
277
278 bool RequiresLabels() const override { return false; }
279
280 private:
284 std::vector<PaletteUsageStats> AnalyzeOverworldPalettes(Rom* rom) const;
285
289 std::vector<PaletteUsageStats> AnalyzeDungeonPalettes(Rom* rom) const;
290};
291
302 public:
303 std::string GetName() const override { return "visual-tile-histogram"; }
304
305 std::string GetDescription() const {
306 return "Generate frequency histogram of tile usage across maps";
307 }
308
309 std::string GetUsage() const override {
310 return "visual-tile-histogram [--type <overworld|dungeon>] [--top <n>] "
311 "[--format <json|text>]";
312 }
313
314 protected:
315 absl::Status ValidateArgs(const resources::ArgumentParser& parser) override;
316
317 absl::Status Execute(Rom* rom, const resources::ArgumentParser& parser,
318 resources::OutputFormatter& formatter) override;
319
320 bool RequiresLabels() const override { return false; }
321
322 private:
326 std::map<int, TileUsageEntry> CountOverworldTiles(Rom* rom) const;
327
331 std::map<int, TileUsageEntry> CountDungeonTiles(Rom* rom) const;
332};
333
334#ifdef YAZE_WITH_OPENCV
341namespace opencv {
342
350std::pair<std::pair<int, int>, double> TemplateMatch(
351 const std::vector<uint8_t>& reference,
352 const std::vector<uint8_t>& search_region, int width, int height,
353 int method);
354
361double FeatureMatch(const std::vector<uint8_t>& tile_a,
362 const std::vector<uint8_t>& tile_b);
363
370double ComputeSSIM(const std::vector<uint8_t>& tile_a,
371 const std::vector<uint8_t>& tile_b);
372
373} // namespace opencv
374#endif // YAZE_WITH_OPENCV
375
376} // namespace tools
377} // namespace agent
378} // namespace cli
379} // namespace yaze
380
381#endif // YAZE_SRC_CLI_SERVICE_AGENT_TOOLS_VISUAL_ANALYSIS_TOOL_H_
382
The Rom class is used to load, save, and modify Rom data. This is a generic SNES ROM container and do...
Definition rom.h:24
Analyze palette usage across maps.
absl::Status ValidateArgs(const resources::ArgumentParser &parser) override
Validate command arguments.
std::string GetUsage() const override
Get the command usage string.
std::string GetName() const override
Get the command name.
std::vector< PaletteUsageStats > AnalyzeOverworldPalettes(Rom *rom) const
Analyze overworld palette usage.
bool RequiresLabels() const override
Check if the command requires ROM labels.
std::vector< PaletteUsageStats > AnalyzeDungeonPalettes(Rom *rom) const
Analyze dungeon palette usage.
absl::Status Execute(Rom *rom, const resources::ArgumentParser &parser, resources::OutputFormatter &formatter) override
Execute the command business logic.
Analyze spritesheets for unused graphics regions.
absl::Status Execute(Rom *rom, const resources::ArgumentParser &parser, resources::OutputFormatter &formatter) override
Execute the command business logic.
bool RequiresLabels() const override
Check if the command requires ROM labels.
std::vector< UnusedRegion > MergeAdjacentRegions(const std::vector< UnusedRegion > &regions) const
Merge adjacent empty regions.
absl::Status ValidateArgs(const resources::ArgumentParser &parser) override
Validate command arguments.
std::vector< UnusedRegion > FindUnusedRegions(Rom *rom, int sheet_index, int tile_size) const
Find contiguous empty regions in a sheet.
std::string GetName() const override
Get the command name.
std::string GetUsage() const override
Get the command usage string.
std::map< int, TileUsageEntry > CountDungeonTiles(Rom *rom) const
Count tile usage in dungeons.
std::string GetName() const override
Get the command name.
absl::Status ValidateArgs(const resources::ArgumentParser &parser) override
Validate command arguments.
bool RequiresLabels() const override
Check if the command requires ROM labels.
std::map< int, TileUsageEntry > CountOverworldTiles(Rom *rom) const
Count tile usage in overworld.
std::string GetUsage() const override
Get the command usage string.
absl::Status Execute(Rom *rom, const resources::ArgumentParser &parser, resources::OutputFormatter &formatter) override
Execute the command business logic.
std::string GetName() const override
Get the command name.
absl::Status Execute(Rom *rom, const resources::ArgumentParser &parser, resources::OutputFormatter &formatter) override
Execute the command business logic.
bool RequiresLabels() const override
Check if the command requires ROM labels.
absl::Status ValidateArgs(const resources::ArgumentParser &parser) override
Validate command arguments.
std::string GetUsage() const override
Get the command usage string.
Base class for visual analysis tools.
double ComputePixelDifference(const std::vector< uint8_t > &tile_a, const std::vector< uint8_t > &tile_b) const
Compute simple pixel difference (Mean Absolute Error)
int GetTileCountForSheet(int sheet_index) const
Get the number of tiles in a graphics sheet.
absl::StatusOr< std::vector< uint8_t > > ExtractTile(Rom *rom, int sheet_index, int tile_index) const
Extract 8x8 tile data from ROM graphics.
std::string FormatHistogramAsJson(const std::vector< TileUsageEntry > &entries) const
Format tile histogram as JSON.
std::string FormatRegionsAsJson(const std::vector< UnusedRegion > &regions) const
Format unused regions as JSON.
std::string FormatMatchesAsJson(const std::vector< TileSimilarityMatch > &matches) const
Format similarity matches as JSON.
absl::StatusOr< std::vector< uint8_t > > ExtractTileAtPosition(Rom *rom, int sheet_index, int x, int y) const
Extract 8x8 tile at specific pixel coordinates.
bool IsRegionEmpty(const std::vector< uint8_t > &data) const
Check if a tile region is empty (all 0x00 or 0xFF)
double ComputeStructuralSimilarity(const std::vector< uint8_t > &tile_a, const std::vector< uint8_t > &tile_b) const
Compute structural similarity index (SSIM-like)
std::string FormatPaletteUsageAsJson(const std::vector< PaletteUsageStats > &stats) const
Format palette usage as JSON.
Utility for parsing common CLI argument patterns.
Base class for CLI command handlers.
Utility for consistent output formatting across commands.
Result of a tile similarity comparison.
Represents a contiguous unused region in a spritesheet.