public static Optional <TextureXImage> From(ByteReader reader) { try { UpperString name = reader.StringWithoutNulls(8); TextureXFlags flags = (TextureXFlags)reader.UShort(); Vector2 scale = new Vector2(reader.Byte(), reader.Byte()); Dimension dimension = new Dimension(reader.Short(), reader.Short()); int columnDirectory = reader.Int(); int patchCount = reader.Short(); List <TextureXPatch> patches = Range(patchCount).Map(i => { Vec2I offset = new Vec2I(reader.Short(), reader.Short()); short patchIndex = reader.Short(); short stepDirection = reader.Short(); short colormap = reader.Short(); return(new TextureXPatch(offset, patchIndex, stepDirection, colormap)); }).ToList(); return(new TextureXImage(name, flags, scale, dimension, columnDirectory, patches)); } catch { return(Empty); } }
/// <summary> /// Creates a palette image from a column-based image. /// </summary> /// <param name="data">The data for the image.</param> /// <param name="resourceNamespace">The namespace this image belongs /// to.</param> /// <returns>The palette image for the data, or null if the image is /// corrupt and would not make a valid column image.</returns> public static Optional <PaletteImage> FromColumn(byte[] data, ResourceNamespace resourceNamespace) { // OPTIMIZE: Short circuit if it's likely not a column image. // OPTIMIZE: Write posts horizontally, then rotate for speed? try { ByteReader reader = ByteReader.From(ByteOrder.Little, data); int width = reader.Short(); int height = reader.Short(); Vec2I offset = (reader.Short(), reader.Short()); if (offset.X < 0 || offset.Y < 0) { return(Empty); } int[] offsets = new int[width]; for (int i = 0; i < width; i++) { offsets[i] = reader.Int(); } short[] indices = Arrays.Create(width * height, TransparentIndex); for (int col = 0; col < width; col++) { reader.Offset = offsets[col]; while (true) { int rowStart = reader.Byte(); if (rowStart == 0xFF) { break; } int indicesCount = reader.Byte(); reader.Skip(1); byte[] paletteIndices = reader.Bytes(indicesCount); reader.Skip(1); int indicesOffset = (rowStart * width) + col; for (int i = 0; i < paletteIndices.Length; i++) { indices[indicesOffset] = paletteIndices[i]; indicesOffset += width; } } } return(new PaletteImage(width, height, indices) { Namespace = resourceNamespace, Offset = offset }); } catch { return(Empty); } }