/// <summary> /// Initializes a new <see cref="SpriteProperties"/> instance using the supplied <see cref="SequenceProperties"/> instance and sequence index. /// </summary> /// <param name="sequence">The Halo 2 sequence instance.</param> /// <param name="index">The sprite index within the sequence.</param> /// <exception cref="ArgumentNullException"><paramref name="sequence"/> is null.</exception> /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> it outside of valid range.</exception> public SpriteProperties(SequenceProperties sequence, int index) { //Check if (sequence == null) { throw new ArgumentNullException(nameof(bitmap)); } if (index < 0 || sequence.bitmap.tag.Sprites[sequence.index].Length <= index) { throw new ArgumentOutOfRangeException(nameof(index)); } this.sequence = sequence; this.index = index; }
public static SequenceProperties GetNcbiGenbank(string gid, ref string message) { SequenceProperties ncbidat = null; try { ncbidat = WfComponent.Utils.Genbank.GetSequenceProperties(gid, ref message); if (!string.IsNullOrEmpty(message) || ncbidat == null || string.IsNullOrEmpty(ncbidat.accession)) { message += "get genbank sequence data error. "; return(ncbidat); // genbank error } } catch (Exception e) { message += e.Message; } return(ncbidat); }
/// <summary> /// NCBI Reference nuc /// </summary> /// <param name="referenceName">NCBI Reference Accession </param> /// <param name="nucName">consensus name</param> /// <param name="nucs">consensus nucreotides</param> /// <param name="outAlignPath">output agliment file path</param> public static void OutAglimentNcbi(string referenceName, string nucName, string nucs, string outAlignPath, ref string message) { SequenceProperties ncbidat = null; try { ncbidat = WfComponent.Utils.Genbank.GetSequenceProperties(referenceName, ref message); if (!string.IsNullOrEmpty(message) || ncbidat == null || string.IsNullOrEmpty(ncbidat.accession)) { message += "get genbank sequence data error. "; return; // genbank error } } catch (Exception e) { message += e.Message; return; } var tmpFasta = outAlignPath.EndsWith(".fasta") ? Path.ChangeExtension(outAlignPath, ".fna") : Path.ChangeExtension(outAlignPath, ".fasta"); var fastaLines = new string[] { ">" + ncbidat.accession, ncbidat.sequence, ">" + nucName, nucs }; // 一時Fasta WfComponent.Utils.FileUtils.WriteFile(tmpFasta, fastaLines, ref message); if (!string.IsNullOrEmpty(message)) { return; } var alignProc = new WfComponent.External.Kalign( new WfComponent.External.KalignOptions() { fastaPath = tmpFasta, outAlign = outAlignPath, isFastaOut = true }); // ほぼ一瞬で終わるはずなのでCancel 考えない。 try { alignProc.StartProcess(); message = alignProc.Message; if (File.Exists(outAlignPath)) { message = AliView.AliViewStart(outAlignPath); } } catch (Exception e) { message += e.Message; message += "execute agliment program error, " + alignProc.Message; } return; }
/// <summary> /// Initializes a new <see cref="HaloBitmap"/> instance using the supplied object index entry. /// </summary> /// <param name="entry">The object index entry that contains the bitmap tag group data.</param> /// <exception cref="ArgumentNullException"><paramref name="entry"/> is null.</exception> /// <exception cref="ArgumentException"><paramref name="entry"/> is not a bitmap.</exception> public HaloBitmap(IndexEntry entry) { //Check if (entry == null) { throw new ArgumentNullException(nameof(entry)); } else if (entry.Root != HaloTags.bitm) { throw new ArgumentException("Index entry is not bitmap.", nameof(entry)); } //Setup this.entry = entry; tag = new BitmapTag(entry); maps = new Bitmap[tag.Bitmaps.Length][][]; //Setup Property Accessors bitmaps = new BitmapProperties[tag.Bitmaps.Length]; for (int i = 0; i < tag.Bitmaps.Length; i++) { bitmaps[i] = new BitmapProperties(this, i); } sequences = new SequenceProperties[tag.Sequences.Length]; for (int i = 0; i < tag.Sequences.Length; i++) { sequences[i] = new SequenceProperties(this, i); } //Check if (tag == null) { return; } else if (tag.Bitmaps.Length == 0) { return; } //Loop through bitmaps for (int k = 0; k < tag.Bitmaps.Length; k++) { //Setup BitmapTagGroup.Bitmap bitmap = tag.Bitmaps[k]; maps[k] = new Bitmap[6][]; //Loop through LODs byte[] sourceData = null; for (int l = 0; l < 6; l++) { //Get source data if (bitmap.rawOffsets[l] != uint.MaxValue) { RawLocation rawLocation = (RawLocation)(bitmap.rawOffsets[l] & 0xC0000000); if (rawLocation == RawLocation.Local) { sourceData = entry.Raws[RawSection.Bitmap][(int)bitmap.rawOffsets[l]].GetBuffer(); } else { string filelocation = string.Empty; int rawOffset = (int)(bitmap.rawOffsets[l] & (uint)RawLocation.LocalMask); switch (rawLocation) { case RawLocation.Mainmenu: filelocation = HaloSettings.MainmenuPath; break; case RawLocation.Shared: filelocation = HaloSettings.SharedPath; break; case RawLocation.SinglePlayerShared: filelocation = HaloSettings.SingleplayerSharedPath; break; } //Check if (File.Exists(filelocation)) { using (FileStream fs = new FileStream(filelocation, FileMode.Open)) using (BinaryReader mapReader = new BinaryReader(fs)) { fs.Seek(rawOffset, SeekOrigin.Begin); sourceData = mapReader.ReadBytes(bitmap.rawLengths[l]); } } } } //Set if (sourceData.Length == 0) { continue; } //Prepare BitmapFlags flags = (BitmapFlags)bitmap.flags; BitmapFormat format = (BitmapFormat)bitmap.format; PixelFormat bitmapFormat = PixelFormat.Format32bppArgb; int sourceBits = 32; switch (format) { case BitmapFormat.A8: case BitmapFormat.Y8: case BitmapFormat.P8Bump: case BitmapFormat.P8: case BitmapFormat.Ay8: sourceBits = 8; break; case BitmapFormat.A8y8: case BitmapFormat.A1r5g5b5: case BitmapFormat.A4r4g4b4: case BitmapFormat.V8u8: case BitmapFormat.G8b8: case BitmapFormat.R5g6b5: sourceBits = 16; break; case BitmapFormat.Dxt1: sourceBits = 4; break; case BitmapFormat.Dxt5: case BitmapFormat.Dxt3: sourceBits = 8; break; case BitmapFormat.Argbfp32: sourceBits = 128; break; } //Handle switch (format) { case BitmapFormat.R5g6b5: bitmapFormat = PixelFormat.Format16bppRgb565; break; case BitmapFormat.A1r5g5b5: bitmapFormat = PixelFormat.Format16bppArgb1555; break; case BitmapFormat.X8r8g8b8: bitmapFormat = PixelFormat.Format32bppRgb; break; case BitmapFormat.P8Bump: bitmapFormat = PixelFormat.Format8bppIndexed; break; case BitmapFormat.P8: bitmapFormat = PixelFormat.Format8bppIndexed; break; } //Prepare int width = bitmap.width, height = bitmap.height; if (flags.HasFlag(BitmapFlags.Linear)) { width = (int)Math.Ceiling(width / 16f) * 16; } //Loop LOD for (int i = 0; i < l; i++) { width /= 2; height /= 2; } //Prepare int mapWidth = width, mapHeight = height, location = 0; int mipmapCount = bitmap.mipmapCount; maps[k][l] = new Bitmap[mipmapCount + 1]; Size bitmapSize = Size.Empty; byte[] mapData = null; //Loop for (int i = 1; i <= mipmapCount + 1; i++) { //Prepare int mapIndex = i - 1; mapWidth = width; mapHeight = height; for (int j = 1; j < i; j++) { mapWidth /= 2; mapHeight /= 2; } //Get Size bitmapSize = new Size(mapWidth, mapHeight); //Check if (bitmapSize.Width == 0 || bitmapSize.Height == 0) { continue; } //Create Map int mapStride = mapWidth * sourceBits / 8; int mapSize = mapStride * mapHeight; //Ehh? switch (format) { case BitmapFormat.Dxt1: mapSize = Math.Max(mapSize, 8); break; case BitmapFormat.Dxt3: case BitmapFormat.Dxt5: mapSize = Math.Max(mapSize, 16); break; case BitmapFormat.P8: case BitmapFormat.P8Bump: mapSize = Math.Max(mapSize, 16); break; default: mapSize = Math.Max(mapSize, 1); break; } mapData = new byte[mapSize]; if (location + mapSize > sourceData.Length) { continue; } Array.Copy(sourceData, location, mapData, 0, mapSize); //Deswizzle? if ((flags & BitmapFlags.Swizzled) == BitmapFlags.Swizzled) { mapData = Swizzler.Swizzle(mapData, mapWidth, mapHeight, bitmap.depth, sourceBits, true); } if (mapData == null) { mapData = new byte[mapSize]; } using (Bitmap map = new Bitmap(mapWidth, mapHeight, bitmapFormat)) { unsafe { //Lock Bits BitmapData data = map.LockBits(new Rectangle(0, 0, mapWidth, mapHeight), ImageLockMode.ReadWrite, bitmapFormat); //Prepare Buffer byte[] bitmapData = new byte[data.Stride * data.Height]; int dataLength = Math.Min(bitmapData.Length, mapSize); //Handle Format... switch (format) { case BitmapFormat.A8: for (int x = 0; x < mapWidth * mapHeight; x++) { bitmapData[x * 4 + 0] = 255; bitmapData[x * 4 + 1] = 255; bitmapData[x * 4 + 2] = 255; bitmapData[x * 4 + 3] = mapData[x]; } break; case BitmapFormat.Y8: for (int x = 0; x < mapWidth * mapHeight; x++) { bitmapData[x * 4 + 0] = mapData[x]; bitmapData[x * 4 + 1] = mapData[x]; bitmapData[x * 4 + 2] = mapData[x]; bitmapData[x * 4 + 3] = 255; } break; case BitmapFormat.Ay8: for (int x = 0; x < mapWidth * mapHeight; x++) { bitmapData[x * 4 + 0] = mapData[x]; bitmapData[x * 4 + 1] = mapData[x]; bitmapData[x * 4 + 2] = mapData[x]; bitmapData[x * 4 + 3] = mapData[x]; } break; case BitmapFormat.A8y8: for (int x = 0; x < mapWidth * mapHeight; x++) { bitmapData[x * 4 + 0] = mapData[x * 2]; bitmapData[x * 4 + 1] = mapData[x * 2]; bitmapData[x * 4 + 2] = mapData[x * 2]; bitmapData[x * 4 + 3] = mapData[x * 2 + 1]; } break; case BitmapFormat.A4r4g4b4: for (int x = 0; x < mapWidth * mapHeight; x++) { bitmapData[x * 4 + 0] = (byte)(mapData[x * 2 + 0] & 0xF0); bitmapData[x * 4 + 1] = (byte)(mapData[x * 2 + 0] & 0x0F); bitmapData[x * 4 + 2] = (byte)(mapData[x * 2 + 1] & 0xF0); bitmapData[x * 4 + 3] = (byte)(mapData[x * 2 + 1] & 0x0F); } break; case BitmapFormat.P8Bump: case BitmapFormat.P8: Array.Copy(mapData, 0, bitmapData, 0, dataLength); break; case BitmapFormat.R5g6b5: Array.Copy(mapData, 0, bitmapData, 0, dataLength); break; case BitmapFormat.A1r5g5b5: Array.Copy(mapData, 0, bitmapData, 0, dataLength); break; case BitmapFormat.X8r8g8b8: Array.Copy(mapData, 0, bitmapData, 0, dataLength); break; case BitmapFormat.A8r8g8b8: Array.Copy(mapData, 0, bitmapData, 0, dataLength); break; case BitmapFormat.Dxt1: if (mapWidth >= 4 && mapHeight >= 4) { S3TC.DecompressDxt1(ref bitmapData, mapData, bitmapSize); } break; case BitmapFormat.Dxt3: if (mapWidth >= 4 && mapHeight >= 4) { S3TC.DecompressDxt3(ref bitmapData, mapData, bitmapSize); } break; case BitmapFormat.Dxt5: if (mapWidth >= 4 && mapHeight >= 4) { S3TC.DecompressDxt5(ref bitmapData, mapData, bitmapSize); } break; case BitmapFormat.Argbfp32: for (int x = 0; x < mapWidth * mapHeight; x++) { bitmapData[x * 4 + 0] = (byte)Math.Min((BitConverter.ToSingle(mapData, x * 16 + 8) * 255f), 255f); bitmapData[x * 4 + 1] = (byte)Math.Min((BitConverter.ToSingle(mapData, x * 16 + 4) * 255f), 255f); bitmapData[x * 4 + 2] = (byte)Math.Min((BitConverter.ToSingle(mapData, x * 16) * 255f), 255f); bitmapData[x * 4 + 3] = (byte)Math.Min((BitConverter.ToSingle(mapData, x * 16 + 12) * 255f), 255f); } break; case BitmapFormat.Rgbfp32: break; case BitmapFormat.Rgbfp16: break; case BitmapFormat.V8u8: for (int x = 0; x < mapWidth * mapHeight; x++) { bitmapData[x * 4 + 0] = 255; bitmapData[x * 4 + 1] = (byte)(127 + (sbyte)mapData[x * 2 + 1]); bitmapData[x * 4 + 2] = (byte)(127 + (sbyte)mapData[x * 2]); bitmapData[x * 4 + 3] = 255; } break; case BitmapFormat.G8b8: break; } //Copy Marshal.Copy(bitmapData, 0, data.Scan0, bitmapData.Length); map.UnlockBits(data); //Setup Palettes if (format == BitmapFormat.P8Bump) { map.SetNormalMapPalette(); } else if (format == BitmapFormat.P8) { map.SetGrayscalePalette(); } //Set location += mapSize; } //Draw into cropped image maps[k][l][mapIndex] = new Bitmap(bitmap.width, bitmap.height, bitmapFormat); using (Graphics g = Graphics.FromImage(maps[k][l][mapIndex])) g.DrawImage(map, Point.Empty); } } } } }