Skip to content

Commit

Permalink
Redefine standard image class (LostArtefacts#659)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahm86 authored May 5, 2024
1 parent 07b4a34 commit 80feae9
Show file tree
Hide file tree
Showing 31 changed files with 59 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Drawing;
using TRImageControl.Helpers;
using TRImageControl;
using TRImageControl.Packing;
using TRLevelControl.Model;

Expand Down Expand Up @@ -58,8 +58,8 @@ private List<EMTextureMap> BuildAndPackTextures<E, L>(TRTexturePacker<E, L> pack
Index = data.Background,
Texture = textures[data.Background]
};
BitmapGraphics tile = packer.Tiles[indexedTexture.Atlas].BitmapGraphics;
BitmapGraphics clip = new(tile.Extract(indexedTexture.Bounds));
TRImage tile = packer.Tiles[indexedTexture.Atlas].BitmapGraphics;
TRImage clip = new(tile.Extract(indexedTexture.Bounds));
clip.Overlay(new Bitmap(data.Overlay));

IndexedTRObjectTexture texture = CreateTexture(clip.Bitmap.Size);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Drawing;
using TRImageControl;
using TRImageControl.Helpers;
using TRLevelControl;
using TRLevelControl.Model;

Expand All @@ -15,7 +14,7 @@ public class EMImportTextureFunction : BaseEMFunction

public override void ApplyToLevel(TR1Level level)
{
using BitmapGraphics bg = new(new Bitmap(Bitmap));
using TRImage bg = new(new Bitmap(Bitmap));
List<Color> palette = level.Palette.Select(c => c.ToTR1Color()).ToList();
Rectangle size = new(0, 0, bg.Bitmap.Width, bg.Bitmap.Height);
bg.Scan(size, (c, x, y) =>
Expand All @@ -38,15 +37,15 @@ public override void ApplyToLevel(TR1Level level)

public override void ApplyToLevel(TR2Level level)
{
using BitmapGraphics bg = new(level.Images16[Tile].ToBitmap());
using TRImage bg = new(level.Images16[Tile].ToBitmap());
using Bitmap bmp = new(Bitmap);
bg.Import(bmp, new Rectangle(X, Y, bmp.Width, bmp.Height));
level.Images16[Tile].Pixels = TextureUtilities.ImportFromBitmap(bg.Bitmap);
}

public override void ApplyToLevel(TR3Level level)
{
using BitmapGraphics bg = new(level.Images16[Tile].ToBitmap());
using TRImage bg = new(level.Images16[Tile].ToBitmap());
using Bitmap bmp = new(Bitmap);
bg.Import(bmp, new Rectangle(X, Y, bmp.Width, bmp.Height));
level.Images16[Tile].Pixels = TextureUtilities.ImportFromBitmap(bg.Bitmap);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Drawing;
using TRImageControl.Helpers;
using TRImageControl;
using TRImageControl.Packing;
using TRLevelControl.Model;

Expand Down Expand Up @@ -73,7 +73,7 @@ private void ApplyOverwrites(Func<ushort, Tuple<TexturedTile, TexturedTileSegmen
foreach (TextureOverwrite overwrite in Overwrites)
{
Tuple<TexturedTile, TexturedTileSegment> segment = segmentAction(overwrite.Texture);
BitmapGraphics segmentBmp = new(segment.Item2.Bitmap);
TRImage segmentBmp = new(segment.Item2.Bitmap);
Bitmap clipBmp = segmentBmp.Extract(overwrite.Clip);

foreach (ushort targetTexture in overwrite.Targets.Keys)
Expand Down
2 changes: 1 addition & 1 deletion TRDataControl/Handlers/ColourTransportHandler.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using TRImageControl.Helpers;
using TRImageControl;
using TRLevelControl.Model;
using TRModelTransporter.Model.Definitions;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using TRLevelControl.Model;
using TRModelTransporter.Data;
using TRModelTransporter.Model;
using TRImageControl.Helpers;
using TRImageControl;

namespace TRModelTransporter.Handlers;

Expand Down Expand Up @@ -79,7 +79,7 @@ protected virtual void CollateSegments()
}

_importSegments[definition] = new List<TexturedTileSegment>();
using BitmapGraphics bg = new(definition.Bitmap);
using TRImage bg = new(definition.Bitmap);
foreach (int segmentIndex in definition.ObjectTextures.Keys)
{
Bitmap segmentClip = bg.Extract(definition.TextureSegments[segmentIndex]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using TRImageControl.Helpers;
using TRImageControl;
using TRImageControl.Packing;
using TRLevelControl.Model;
using TRModelTransporter.Helpers;
Expand Down
2 changes: 1 addition & 1 deletion TRDataControl/Transport/TR1/TR1DataImporter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Newtonsoft.Json;
using TRImageControl.Helpers;
using TRImageControl;
using TRImageControl.Packing;
using TRLevelControl.Model;
using TRModelTransporter.Data;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Drawing;
using TRLevelControl.Model;

namespace TRImageControl.Helpers;
namespace TRImageControl;

//https://www.codeproject.com/Articles/19045/Manipulating-colors-in-NET-Part-1
public static class ColourExtensions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace TRImageControl.Helpers;
namespace TRImageControl;

public struct HSB
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace TRImageControl.Helpers;
namespace TRImageControl;

public class HSBOperation
{
Expand Down
3 changes: 1 addition & 2 deletions TRImageControl/Packing/TRTexturePacker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using RectanglePacker.Organisation;
using System.Collections.Immutable;
using System.Drawing;
using TRImageControl.Helpers;
using TRLevelControl;
using TRLevelControl.Model;

Expand Down Expand Up @@ -55,7 +54,7 @@ public TRTexturePacker(L level, int maximumTiles = 16, ITextureClassifier classi
for (int i = 0; i < NumLevelImages; i++)
{
TexturedTile tile = AddTile();
tile.BitmapGraphics = new BitmapGraphics(GetTile(i));
tile.BitmapGraphics = new TRImage(GetTile(i));
tile.AllowOverlapping = true; // Allow initially for the likes of Opera House - see tile 3 [128, 128]
}

Expand Down
5 changes: 2 additions & 3 deletions TRImageControl/Packing/TexturedTile.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using RectanglePacker.Defaults;
using System.Drawing;
using System.Drawing.Imaging;
using TRImageControl.Helpers;

namespace TRImageControl.Packing;

public class TexturedTile : DefaultTile<TexturedTileSegment>, IDisposable
{
private BitmapGraphics _graphics;
public BitmapGraphics BitmapGraphics
private TRImage _graphics;
public TRImage BitmapGraphics
{
get => _graphics;
set
Expand Down
1 change: 0 additions & 1 deletion TRImageControl/Packing/Types/TR1TexturePacker.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Drawing;
using TRImageControl.Helpers;
using TRLevelControl;
using TRLevelControl.Model;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Drawing;
using TRLevelControl.Model;

namespace TRImageControl.Helpers;
namespace TRImageControl;

public class TRPalette16Control
{
Expand All @@ -19,11 +19,8 @@ public TRPalette16Control(List<TRColour4> palette16, IEnumerable<TRMesh> meshes)
_palette = palette16;

IEnumerable<int> colourRefs = meshes
.SelectMany(m => m.ColouredRectangles)
.Select(f => f.Texture >> 8)
.Concat(meshes
.SelectMany(m => m.ColouredTriangles)
.Select(f => f.Texture >> 8));
.SelectMany(m => m.ColouredFaces)
.Select(f => f.Texture >> 8);

List<int> range = new(Enumerable.Range(0, palette16.Count));
_freeIndices = new(range.Except(colourRefs));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using TRLevelControl;
using TRLevelControl.Model;

namespace TRImageControl.Helpers;
namespace TRImageControl;

public class TRPalette8Control : IDisposable
{
Expand Down Expand Up @@ -40,7 +40,7 @@ public void MergeTiles()
for (int i = 0; i < Level.Images8.Count; i++)
{
Bitmap bmp = ChangedTiles.ContainsKey(i) ? ChangedTiles[i] : GetOriginalTile(i);
BitmapGraphics bg = new(bmp);
TRImage bg = new(bmp);
bg.Scan(_defaultBounds, (c, x, y) =>
{
int colIndex;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
using System.Runtime.InteropServices;
using TRImageControl.Textures;

namespace TRImageControl.Helpers;
namespace TRImageControl;

public class BitmapGraphics : IDisposable
public class TRImage : IDisposable
{
private Bitmap _bitmap;
private int _width, _height;
Expand All @@ -28,7 +28,7 @@ public Bitmap Bitmap

public event EventHandler GraphicChanged;

public BitmapGraphics(Bitmap bitmap)
public TRImage(Bitmap bitmap)
{
Bitmap = bitmap;
}
Expand Down
1 change: 0 additions & 1 deletion TRImageControl/TextureUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using TRImageControl.Helpers;
using TRLevelControl;
using TRLevelControl.Model;

Expand Down
15 changes: 7 additions & 8 deletions TRImageControl/Textures/Mapping/AbstractTextureMapping.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Newtonsoft.Json;
using System.Drawing;
using TRImageControl.Helpers;
using TRLevelControl.Model;

namespace TRImageControl.Textures;
Expand All @@ -19,14 +18,14 @@ public abstract class AbstractTextureMapping<E, L> : IDisposable
public Color DefaultSkyBox { get; set; }
public Dictionary<E, E> EntityMap { get; set; }

protected readonly Dictionary<int, BitmapGraphics> _tileMap;
protected readonly Dictionary<int, TRImage> _tileMap;
protected readonly L _level;
protected bool _committed;

protected AbstractTextureMapping(L level)
{
_level = level;
_tileMap = new Dictionary<int, BitmapGraphics>();
_tileMap = new Dictionary<int, TRImage>();
_committed = false;
}

Expand Down Expand Up @@ -190,7 +189,7 @@ private void RedrawDynamicTargets(Dictionary<int, List<Rectangle>> targets, HSBO
{
foreach (int tileIndex in targets.Keys)
{
BitmapGraphics bg = GetBitmapGraphics(tileIndex);
TRImage bg = GetBitmapGraphics(tileIndex);
foreach (Rectangle rect in targets[tileIndex])
{
bg.AdjustHSB(rect, operation);
Expand Down Expand Up @@ -395,7 +394,7 @@ public void DrawReplacements()
// Scan each tile and replace colour Search with above
foreach (int tile in replacement.ReplacementMap.Keys)
{
BitmapGraphics graphics = GetBitmapGraphics(tile);
TRImage graphics = GetBitmapGraphics(tile);
foreach (Rectangle rect in replacement.ReplacementMap[tile])
{
graphics.Replace(search, replace, rect);
Expand Down Expand Up @@ -446,11 +445,11 @@ private static ushort ConvertMeshTexture(ushort texture, Dictionary<int, int> re
return texture;
}

private BitmapGraphics GetBitmapGraphics(int tile)
private TRImage GetBitmapGraphics(int tile)
{
if (!_tileMap.ContainsKey(tile))
{
_tileMap.Add(tile, new BitmapGraphics(GetTile(tile)));
_tileMap.Add(tile, new TRImage(GetTile(tile)));
}

return _tileMap[tile];
Expand All @@ -468,7 +467,7 @@ public virtual void CommitGraphics()
{
foreach (int tile in _tileMap.Keys)
{
using BitmapGraphics bmp = _tileMap[tile];
using TRImage bmp = _tileMap[tile];
SetTile(tile, bmp.Bitmap);
}
_committed = true;
Expand Down
1 change: 0 additions & 1 deletion TRImageControl/Textures/Mapping/TR1TextureMapping.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Drawing;
using TRLevelControl.Model;
using TRImageControl.Helpers;

namespace TRImageControl.Textures;

Expand Down
1 change: 0 additions & 1 deletion TRImageControl/Textures/Mapping/TR2TextureMapping.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Drawing;
using TRLevelControl.Model;
using TRImageControl.Helpers;

namespace TRImageControl.Textures;

Expand Down
1 change: 0 additions & 1 deletion TRImageControl/Textures/Mapping/TR3TextureMapping.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Drawing;
using TRImageControl.Helpers;
using TRLevelControl.Model;

namespace TRImageControl.Textures;
Expand Down
4 changes: 1 addition & 3 deletions TRImageControl/Textures/Source/DynamicTextureSource.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using TRImageControl.Helpers;

namespace TRImageControl.Textures;
namespace TRImageControl.Textures;

public class DynamicTextureSource : AbstractTextureSource
{
Expand Down
6 changes: 2 additions & 4 deletions TRImageControl/Textures/Target/LandmarkTextureTarget.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using TRImageControl.Helpers;

namespace TRImageControl.Textures;
namespace TRImageControl.Textures;

public class LandmarkTextureTarget
{
public int MappedTextureIndex { get; set; }
public int BackgroundIndex { get; set; }
public BitmapGraphics Background { get; set; }
public TRImage Background { get; set; }
public int RoomNumber { get; set; }
public List<int> RectangleIndices { get; set; }
public PortalSector PortalSector { get; set; }
Expand Down
6 changes: 3 additions & 3 deletions TRRandomizerCore/Editors/TR1ClassicEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Drawing;
using TRGE.Coord;
using TRGE.Core;
using TRImageControl.Helpers;
using TRImageControl;
using TRLevelControl.Model;
using TRRandomizerCore.Helpers;
using TRRandomizerCore.Processors;
Expand Down Expand Up @@ -359,7 +359,7 @@ private void AmendTitleAndCredits(AbstractTRScriptEditor scriptEditor, TRSaveMon
if (File.Exists(backupTitle))
{
string editedTitle = Path.Combine(GetWriteBasePath(), mainMenuPic);
using BitmapGraphics bg = new(new Bitmap(backupTitle));
using TRImage bg = new(new Bitmap(backupTitle));
using Bitmap badge = new(@"Resources\Shared\Graphics\goldbadge-small.png");
bg.Graphics.DrawImage(badge, new Rectangle(
scriptEditor.GameMode == GameMode.Gold ? _goldBadgePos : _regularBadgePos,
Expand All @@ -378,7 +378,7 @@ private void AmendTitleAndCredits(AbstractTRScriptEditor scriptEditor, TRSaveMon
string creditFile = Path.Combine(_io.OutputDirectory.FullName, "trrando.png");
string creditPath = @"data\trrando.png";

using (BitmapGraphics bg = new(new Bitmap(1920, 1080)))
using (TRImage bg = new(new Bitmap(1920, 1080)))
using (Bitmap badge = new(@"Resources\Shared\Graphics\goldbadge-large.png"))
{
bg.Graphics.FillRectangle(new SolidBrush(Color.Black), new Rectangle(0, 0, 1920, 1080));
Expand Down
2 changes: 1 addition & 1 deletion TRRandomizerCore/Randomizers/TR1/TR1OutfitRandomizer.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Drawing;
using TRDataControl.Environment;
using TRGE.Core;
using TRImageControl.Helpers;
using TRImageControl;
using TRImageControl.Packing;
using TRImageControl.Textures;
using TRLevelControl;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using RectanglePacker.Events;
using System.Drawing;
using TRImageControl.Helpers;
using TRImageControl;
using TRImageControl.Packing;
using TRImageControl.Textures;
using TRLevelControl;
Expand Down Expand Up @@ -106,8 +106,8 @@ public bool Import(L level, AbstractTextureMapping<E, L> mapping, bool isLevelMi
Index = target.BackgroundIndex,
Texture = textures[target.BackgroundIndex]
};
BitmapGraphics tile = packer.Tiles[indexedTexture.Atlas].BitmapGraphics;
BitmapGraphics clip = new(tile.Extract(indexedTexture.Bounds));
TRImage tile = packer.Tiles[indexedTexture.Atlas].BitmapGraphics;
TRImage clip = new(tile.Extract(indexedTexture.Bounds));
clip.Overlay(source.Bitmap);
image = clip.Bitmap;

Expand Down
Loading

0 comments on commit 80feae9

Please sign in to comment.