Advertisement
namofure

DQMJ2P bmp

Feb 6th, 2025
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.93 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.IO;
  4. using System.Windows.Forms;
  5.  
  6. public class NCGRViewer : Form
  7. {
  8.     private PictureBox pictureBox;
  9.     private Bitmap bitmap;
  10.     private const int TILE_SIZE = 8;
  11.     private const int TILES_PER_ROW = 5;
  12.     private const int TILE_COUNT = 25;
  13.     private const int IMAGE_SIZE = TILE_SIZE * TILES_PER_ROW;
  14.     private Color[] palette = new Color[16];
  15.  
  16.     public NCGRViewer()
  17.     {
  18.         this.Text = "NCGR Viewer";
  19.         this.ClientSize = new Size(IMAGE_SIZE, IMAGE_SIZE);
  20.  
  21.         pictureBox = new PictureBox
  22.         {
  23.             Dock = DockStyle.Fill,
  24.             SizeMode = PictureBoxSizeMode.StretchImage
  25.         };
  26.  
  27.         this.Controls.Add(pictureBox);
  28.         LoadNCGR("test.bin");
  29.     }
  30.  
  31.     private void LoadNCGR(string filePath)
  32.     {
  33.         byte[] data = File.ReadAllBytes(filePath);
  34.  
  35.         // パレットデコード
  36.         for (int i = 0; i < 16; i++)
  37.         {
  38.             int offset = 0x350 + (i * 2);
  39.             ushort colorData = BitConverter.ToUInt16(data, offset);
  40.             int r = (colorData & 0x1F) * 8;
  41.             int g = ((colorData >> 5) & 0x1F) * 8;
  42.             int b = ((colorData >> 10) & 0x1F) * 8;
  43.             palette[i] = Color.FromArgb(r, g, b);
  44.         }
  45.  
  46.         // 画像データデコード
  47.         bitmap = new Bitmap(IMAGE_SIZE, IMAGE_SIZE);
  48.  
  49.         for (int tile = 0; tile < TILE_COUNT; tile++)
  50.         {
  51.             int newTileIndex = tile;
  52.             int tileX;
  53.             int tileY;
  54.  
  55.             if (tile < 20)
  56.             {
  57.                 newTileIndex = ((tile % 4) * 5) + (tile / 4);
  58.                 if (tile < 16)
  59.                 {
  60.                     newTileIndex = ((tile / 4) * 5) + (tile % 4);
  61.                 }
  62.             }
  63.  
  64.                 tileX = (newTileIndex % TILES_PER_ROW) * TILE_SIZE;
  65.                 tileY = (newTileIndex / TILES_PER_ROW) * TILE_SIZE;
  66.  
  67.  
  68.             int tileOffset = 0x30 + (tile * 32);
  69.  
  70.             for (int y = 0; y < TILE_SIZE; y++)
  71.             {
  72.                 for (int x = 0; x < TILE_SIZE; x += 2)
  73.                 {
  74.                     if (tileOffset + (y * 4) + (x / 2) >= data.Length)
  75.                         continue; // 範囲外ならスキップ
  76.  
  77.                     byte pixelData = data[tileOffset + (y * 4) + (x / 2)];
  78.  
  79.                     byte index1 = (byte)(pixelData & 0x0F);        // 下位4bit 左のピクセル
  80.                     byte index2 = (byte)((pixelData >> 4) & 0x0F); // 上位4bit 右のピクセル
  81.  
  82.                     bitmap.SetPixel(tileX + x, tileY + y, palette[index1]); // 1ピクセル目
  83.                     bitmap.SetPixel(tileX + x + 1, tileY + y, palette[index2]); // 2ピクセル目
  84.                 }
  85.             }
  86.         }
  87.         pictureBox.Image = bitmap;
  88.     }
  89.  
  90.     [STAThread]
  91.     public static void Main()
  92.     {
  93.         Application.EnableVisualStyles();
  94.         Application.Run(new NCGRViewer());
  95.     }
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement