Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.IO;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using static System.Windows.Forms.VisualStyles.VisualStyleElement;
- namespace viewer_test
- {
- public partial class Form1 : Form
- {
- private string currentFilePath;
- private const int TILE_SIZE = 8;
- private const int TILES_PER_ROW = 5;
- private const int TILE_COUNT = 25;
- private const int IMAGE_SIZE = TILE_SIZE * TILES_PER_ROW;
- private Color[] palette = new Color[16];
- public Form1()
- {
- InitializeComponent();
- listView.View = View.Details;
- listView.Columns.Add("File", 150);
- listView.Columns.Add("Offset", 120);
- listView.Columns.Add("PaletteOffset", 110);
- listView.SelectedIndexChanged += ListView_SelectedIndexChanged;
- btnOpenFile.Click += BtnOpenFile_Click;
- }
- private void BtnOpenFile_Click(object sender, EventArgs e)
- {
- using (OpenFileDialog ofd = new OpenFileDialog())
- {
- ofd.Filter = "NICA Files (*.nica)|*.nica|All Files (*.*)|*.*";
- if (ofd.ShowDialog() == DialogResult.OK)
- {
- currentFilePath = ofd.FileName;
- LoadNICAFile(currentFilePath);
- }
- }
- }
- private void LoadNICAFile(string filePath)
- {
- byte[] data = File.ReadAllBytes(filePath);
- listView.Items.Clear();
- // データ部を読み込む
- int dataOffset = 0x08;
- for (int i = 0; i < 489; i++)
- {
- string dataName = Encoding.ASCII.GetString(data, dataOffset, 12).Trim();
- int offset = BitConverter.ToInt32(data, dataOffset + 0x20);
- int size = BitConverter.ToInt32(data, dataOffset + 0x24);
- int paOffset = BitConverter.ToInt32(data, dataOffset + 0x4C88);
- int paletteSize = BitConverter.ToInt32(data, dataOffset + 0x4C8C);
- listView.Items.Add(new ListViewItem(new[] { dataName, $"0x{offset:X}", $"0x{paOffset:X}" }));
- dataOffset += 40; // 次のデータへ
- }
- // パレット部を読み込む(517 個)
- int paletteOffset = 0x4C70;
- for (int i = 0; i < 489; i++)
- {
- string paletteName = Encoding.ASCII.GetString(data, paletteOffset, 12).Trim();
- int paletteDataOffset = BitConverter.ToInt32(data, paletteOffset + 0x20);
- int paletteDataSize = BitConverter.ToInt32(data, paletteOffset + 0x24);
- paletteOffset += 40; // 次のパレットデータへ
- }
- }
- private void ListView_SelectedIndexChanged(object sender, EventArgs e)
- {
- if (listView.SelectedItems.Count > 0 && !string.IsNullOrEmpty(currentFilePath))
- {
- // 選択されたアイテムからオフセットを取得
- string offsetString1 = listView.SelectedItems[0].SubItems[1].Text; // オフセットが表示されている列
- string offsetString2 = listView.SelectedItems[0].SubItems[2].Text;
- int offset = Convert.ToInt32(offsetString1, 16); // 16進数を整数に変換
- int paOffset = Convert.ToInt32(offsetString2, 16);
- // そのオフセットを基にデータを読み込む
- LoadNCGRData(currentFilePath, offset, paOffset);
- }
- }
- private void LoadNCGRData(string filePath, int offset, int paOffset)
- {
- byte[] data = File.ReadAllBytes(filePath); // ファイルをバイト配列で読み込む
- // パレットデータをデコード(16色 × 2バイト = 32バイト)
- Color[] palette = new Color[16];
- for (int i = 0; i < 16; i++)
- {
- int Aoffset = 0x915C8;
- int colorData = BitConverter.ToUInt16(data, paOffset + (i * 2)); // 2バイトずつ取得
- int r = (colorData & 0x1F) * 8;
- int g = ((colorData >> 5) & 0x1F) * 8;
- int b = ((colorData >> 10) & 0x1F) * 8;
- palette[i] = Color.FromArgb(r, g, b);
- }
- // 画像データをデコード
- int TILE_SIZE = 8;
- int TILES_PER_ROW = 5;
- int TILE_COUNT = 25;
- int IMAGE_SIZE = TILE_SIZE * TILES_PER_ROW;
- Bitmap bitmap = new Bitmap(IMAGE_SIZE, IMAGE_SIZE);
- for (int tile = 0; tile < TILE_COUNT; tile++)
- {
- int newTileIndex = tile;
- if (tile < 20)
- {
- newTileIndex = ((tile % 4) * 5) + (tile / 4);
- if (tile < 16)
- {
- newTileIndex = ((tile / 4) * 5) + (tile % 4);
- }
- }
- int tileX = (newTileIndex % TILES_PER_ROW) * TILE_SIZE;
- int tileY = (newTileIndex / TILES_PER_ROW) * TILE_SIZE;
- int Boffset = offset + 0x30 + (tile * 32);
- for (int y = 0; y < TILE_SIZE; y++)
- {
- for (int x = 0; x < TILE_SIZE; x += 2)
- {
- if (Boffset + (y * 4) + (x / 2) >= data.Length)
- continue; // 範囲外ならスキップ
- byte pixelData = data[Boffset + (y * 4) + (x / 2)];
- byte index1 = (byte)(pixelData & 0x0F);
- byte index2 = (byte)((pixelData >> 4) & 0x0F);
- bitmap.SetPixel(tileX + x, tileY + y, palette[index1]);
- bitmap.SetPixel(tileX + x + 1, tileY + y, palette[index2]);
- }
- }
- }
- // `PictureBox` に画像を表示
- pictureBox.Image = bitmap;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement