/// <summary> /// Saves a tile to the ROM image. /// </summary> /// <param name="tile">The tile to save.</param> public void writeTile(MDTile tile) { int address = tile.Address; //move to the correct position this.setStreamPosition(this.writer.BaseStream, address); //this will obvious break if arrays are staggered int xDimension = tile.PixelData.Length; int yDimension = tile.PixelData[0].Length; //recombine the data int size = (xDimension * yDimension) / 2; byte[] buffer = new byte[size]; int xCounter = 0; int yCounter = 0; for (int index = 0; index < size; index++) { string combine = tile.PixelData[xCounter][yCounter].ToString("X") + tile.PixelData[xCounter + 1][yCounter].ToString("X"); buffer[index] = Byte.Parse(combine, System.Globalization.NumberStyles.HexNumber); xCounter += 2; if (xCounter == xDimension) { xCounter = 0; yCounter++; } } //write the buffer back this.writer.Write(buffer); }
/// <summary> /// Read a tile from the ROM image. /// </summary> /// <param name="offset">The address to start at.</param> /// <param name="xDimension">The xDimension (width) of the tile.</param> /// <param name="yDimension">The yDimension (height) of the tile.</param> /// <returns></returns> public MDTile readTile(int offset, int xDimension, int yDimension) { //initialize the tile MDTile tile = new MDTile(); tile.Address = offset; tile.PixelData = new byte[xDimension][]; for (int index = 0; index < xDimension; index++) { tile.PixelData[index] = new byte[yDimension]; } //move to the correct position this.setStreamPosition(this.reader.BaseStream, offset); //read the tile data - each byte contains *two* pixels int size = (xDimension * yDimension) / 2; byte[] buffer = this.reader.ReadBytes(size); int xCounter = 0; int yCounter = 0; for (int index = 0; index < size; index++) { byte byteValue = buffer[index]; //split the byte string byteString = byteValue.ToString("X"); if (byteString.Length < 2) { byteString = "0" + byteString; } tile.PixelData[xCounter][yCounter] = byte.Parse(byteString.Substring(0, 1), System.Globalization.NumberStyles.HexNumber); xCounter++; tile.PixelData[xCounter][yCounter] = byte.Parse(byteString.Substring(1, 1), System.Globalization.NumberStyles.HexNumber); xCounter++; if (xCounter == xDimension) { xCounter = 0; yCounter++; } } return(tile); }