Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Drawing;
- using System.IO;
- using System.Windows.Forms;
- public class NCGRViewer : Form
- {
- private PictureBox pictureBox;
- private Bitmap bitmap;
- 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 NCGRViewer()
- {
- this.Text = "NCGR Viewer";
- this.ClientSize = new Size(IMAGE_SIZE, IMAGE_SIZE);
- pictureBox = new PictureBox
- {
- Dock = DockStyle.Fill,
- SizeMode = PictureBoxSizeMode.StretchImage
- };
- this.Controls.Add(pictureBox);
- LoadNCGR("test.bin");
- }
- private void LoadNCGR(string filePath)
- {
- byte[] data = File.ReadAllBytes(filePath);
- // パレットデコード
- for (int i = 0; i < 16; i++)
- {
- int offset = 0x350 + (i * 2);
- ushort colorData = BitConverter.ToUInt16(data, offset);
- int r = (colorData & 0x1F) * 8;
- int g = ((colorData >> 5) & 0x1F) * 8;
- int b = ((colorData >> 10) & 0x1F) * 8;
- palette[i] = Color.FromArgb(r, g, b);
- }
- // 画像データデコード
- bitmap = new Bitmap(IMAGE_SIZE, IMAGE_SIZE);
- for (int tile = 0; tile < TILE_COUNT; tile++)
- {
- int newTileIndex = tile;
- int tileX;
- int tileY;
- if (tile < 20)
- {
- newTileIndex = ((tile % 4) * 5) + (tile / 4);
- if (tile < 16)
- {
- newTileIndex = ((tile / 4) * 5) + (tile % 4);
- }
- }
- tileX = (newTileIndex % TILES_PER_ROW) * TILE_SIZE;
- tileY = (newTileIndex / TILES_PER_ROW) * TILE_SIZE;
- int tileOffset = 0x30 + (tile * 32);
- for (int y = 0; y < TILE_SIZE; y++)
- {
- for (int x = 0; x < TILE_SIZE; x += 2)
- {
- if (tileOffset + (y * 4) + (x / 2) >= data.Length)
- continue; // 範囲外ならスキップ
- byte pixelData = data[tileOffset + (y * 4) + (x / 2)];
- byte index1 = (byte)(pixelData & 0x0F); // 下位4bit 左のピクセル
- byte index2 = (byte)((pixelData >> 4) & 0x0F); // 上位4bit 右のピクセル
- bitmap.SetPixel(tileX + x, tileY + y, palette[index1]); // 1ピクセル目
- bitmap.SetPixel(tileX + x + 1, tileY + y, palette[index2]); // 2ピクセル目
- }
- }
- }
- pictureBox.Image = bitmap;
- }
- [STAThread]
- public static void Main()
- {
- Application.EnableVisualStyles();
- Application.Run(new NCGRViewer());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement