2009-04-19 18:07:22 +00:00
#region = = = = = = = = = = = = = = = = = = Copyright ( c ) 2007 Pascal vd Heiden
/ *
* Copyright ( c ) 2007 Pascal vd Heiden , www . codeimp . com
* This program is released under GNU General Public License
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* /
#endregion
#region = = = = = = = = = = = = = = = = = = Namespaces
using System ;
using System.Collections.Generic ;
using System.Drawing ;
using System.Drawing.Imaging ;
2014-12-30 20:39:45 +00:00
using System.IO ;
2014-12-03 09:06:05 +00:00
using CodeImp.DoomBuilder.Controls ;
2009-04-19 18:07:22 +00:00
using CodeImp.DoomBuilder.IO ;
2014-12-30 20:39:45 +00:00
using CodeImp.DoomBuilder.Rendering ;
2009-04-19 18:07:22 +00:00
#endregion
namespace CodeImp.DoomBuilder.Data
{
2016-03-04 08:10:56 +00:00
internal sealed unsafe class TEXTURESImage : ImageData
2009-04-19 18:07:22 +00:00
{
#region = = = = = = = = = = = = = = = = = = Variables
2014-11-25 11:52:01 +00:00
private readonly List < TexturePatch > patches ; //mxd
2016-06-08 00:06:20 +00:00
private readonly bool optional ; //mxd
private readonly bool nulltexture ; //mxd
2009-04-19 18:07:22 +00:00
#endregion
#region = = = = = = = = = = = = = = = = = = Constructor / Disposer
// Constructor
2016-06-08 00:06:20 +00:00
public TEXTURESImage ( string name , string virtualpath , int width , int height , float scalex , float scaley ,
bool worldpanning , bool isflat , bool optional , bool nulltexture )
2009-04-19 18:07:22 +00:00
{
// Initialize
this . width = width ;
this . height = height ;
2010-01-02 20:22:05 +00:00
this . scale . x = scalex ;
this . scale . y = scaley ;
2010-05-08 13:14:48 +00:00
this . worldpanning = worldpanning ;
2016-06-08 00:06:20 +00:00
this . optional = optional ; //mxd
this . nulltexture = nulltexture ; //mxd
2014-07-15 08:08:57 +00:00
this . patches = new List < TexturePatch > ( 1 ) ;
2014-11-25 11:52:01 +00:00
//mxd
2009-04-19 18:07:22 +00:00
SetName ( name ) ;
2016-03-04 08:10:56 +00:00
this . virtualname = ( ! string . IsNullOrEmpty ( virtualpath ) ? virtualpath : "[TEXTURES]" ) + Path . AltDirectorySeparatorChar + this . name ;
2014-12-03 09:06:05 +00:00
this . isFlat = isflat ;
2009-04-19 18:07:22 +00:00
// We have no destructor
GC . SuppressFinalize ( this ) ;
}
#endregion
#region = = = = = = = = = = = = = = = = = = Methods
2014-12-03 09:06:05 +00:00
//mxd
protected override void SetName ( string name )
{
if ( ! General . Map . Config . UseLongTextureNames )
{
if ( name . Length > DataManager . CLASIC_IMAGE_NAME_LENGTH )
name = name . Substring ( 0 , DataManager . CLASIC_IMAGE_NAME_LENGTH ) ;
name = name . ToUpperInvariant ( ) ;
}
base . SetName ( name ) ;
this . shortname = this . displayname . ToUpperInvariant ( ) ;
if ( this . shortname . Length > DataManager . CLASIC_IMAGE_NAME_LENGTH )
{
this . shortname = this . shortname . Substring ( 0 , DataManager . CLASIC_IMAGE_NAME_LENGTH ) ;
}
}
2009-04-19 18:07:22 +00:00
// This adds a patch to the texture
public void AddPatch ( TexturePatch patch )
{
// Add it
2014-07-15 08:08:57 +00:00
patches . Add ( patch ) ;
2016-03-16 23:26:53 +00:00
if ( patch . LumpName = = Name ) hasPatchWithSameName = true ; //mxd
2009-04-19 18:07:22 +00:00
}
// This loads the image
protected override void LocalLoadImage ( )
{
2014-10-07 09:56:36 +00:00
// Checks
2015-12-28 15:01:53 +00:00
if ( this . IsImageLoaded | | width = = 0 | | height = = 0 ) return ;
2014-10-07 09:56:36 +00:00
2009-04-19 18:07:22 +00:00
Graphics g = null ;
lock ( this )
{
// Create texture bitmap
try
{
if ( bitmap ! = null ) bitmap . Dispose ( ) ;
bitmap = new Bitmap ( width , height , PixelFormat . Format32bppArgb ) ;
BitmapData bitmapdata = bitmap . LockBits ( new Rectangle ( 0 , 0 , width , height ) , ImageLockMode . WriteOnly , PixelFormat . Format32bppArgb ) ;
PixelColor * pixels = ( PixelColor * ) bitmapdata . Scan0 . ToPointer ( ) ;
General . ZeroMemory ( new IntPtr ( pixels ) , width * height * sizeof ( PixelColor ) ) ;
bitmap . UnlockBits ( bitmapdata ) ;
g = Graphics . FromImage ( bitmap ) ;
}
catch ( Exception e )
{
// Unable to make bitmap
2016-02-15 14:06:46 +00:00
General . ErrorLogger . Add ( ErrorType . Error , "Unable to load texture image \"" + this . Name + "\". " + e . GetType ( ) . Name + ": " + e . Message ) ;
2009-04-19 18:07:22 +00:00
loadfailed = true ;
}
2015-12-30 11:53:03 +00:00
int missingpatches = 0 ; //mxd
2016-02-15 14:06:46 +00:00
if ( patches . Count = = 0 ) //mxd
{
2016-06-13 23:37:55 +00:00
//mxd. Empty image will suffice here, I suppose...
if ( nulltexture )
{
base . LocalLoadImage ( ) ;
return ;
}
2016-02-15 14:06:46 +00:00
// No patches!
General . ErrorLogger . Add ( ErrorType . Warning , "No patches are defined for texture \"" + this . Name + "\"" ) ;
loadfailed = true ;
}
else if ( ! loadfailed )
2009-04-19 18:07:22 +00:00
{
// Go for all patches
2014-07-15 08:08:57 +00:00
foreach ( TexturePatch p in patches )
2009-04-19 18:07:22 +00:00
{
2016-02-15 14:06:46 +00:00
//mxd. Some patches (like "TNT1A0") should be skipped
2016-03-16 23:26:53 +00:00
if ( p . Skip ) continue ;
2016-02-15 14:06:46 +00:00
2009-04-19 18:07:22 +00:00
// Get the patch data stream
2016-03-14 21:53:53 +00:00
string patchlocation = string . Empty ; //mxd
2016-03-16 23:26:53 +00:00
Stream patchdata = General . Map . Data . GetPatchData ( p . LumpName , p . HasLongName , ref patchlocation ) ;
2009-04-19 18:07:22 +00:00
if ( patchdata ! = null )
{
// Copy patch data to memory
2015-12-28 15:01:53 +00:00
byte [ ] membytes = new byte [ ( int ) patchdata . Length ] ;
2016-08-29 10:06:16 +00:00
lock ( patchdata ) //mxd
{
patchdata . Seek ( 0 , SeekOrigin . Begin ) ;
patchdata . Read ( membytes , 0 , ( int ) patchdata . Length ) ;
}
2015-12-28 15:01:53 +00:00
MemoryStream mem = new MemoryStream ( membytes ) ;
2009-04-19 18:07:22 +00:00
mem . Seek ( 0 , SeekOrigin . Begin ) ;
// Get a reader for the data
2015-12-28 15:01:53 +00:00
IImageReader reader = ImageDataFormat . GetImageReader ( mem , ImageDataFormat . DOOMPICTURE , General . Map . Data . Palette ) ;
2009-04-19 18:07:22 +00:00
if ( reader is UnknownImageReader )
{
2013-12-02 15:02:01 +00:00
//mxd. Probably that's a flat?..
2015-12-28 15:01:53 +00:00
if ( General . Map . Config . MixTexturesFlats )
2014-12-03 23:15:26 +00:00
{
2013-12-02 15:02:01 +00:00
reader = ImageDataFormat . GetImageReader ( mem , ImageDataFormat . DOOMFLAT , General . Map . Data . Palette ) ;
}
2015-12-30 11:53:03 +00:00
2015-12-28 15:01:53 +00:00
if ( reader is UnknownImageReader )
2014-12-03 23:15:26 +00:00
{
2013-12-02 15:02:01 +00:00
// Data is in an unknown format!
2016-06-13 23:37:55 +00:00
if ( ! nulltexture ) General . ErrorLogger . Add ( ( optional ? ErrorType . Warning : ErrorType . Error ) , "Patch lump \"" + Path . Combine ( patchlocation , p . LumpName ) + "\" data format could not be read, while loading texture \"" + this . Name + "\"" ) ;
2015-12-30 11:53:03 +00:00
missingpatches + + ; //mxd
2013-12-02 15:02:01 +00:00
}
2009-04-19 18:07:22 +00:00
}
2013-12-02 15:02:01 +00:00
2014-12-03 23:15:26 +00:00
if ( ! ( reader is UnknownImageReader ) )
{
2009-04-19 18:07:22 +00:00
// Get the patch
mem . Seek ( 0 , SeekOrigin . Begin ) ;
Bitmap patchbmp = null ;
try { patchbmp = reader . ReadAsBitmap ( mem ) ; }
catch ( InvalidDataException )
{
// Data cannot be read!
2016-06-13 23:37:55 +00:00
if ( ! nulltexture ) General . ErrorLogger . Add ( ( optional ? ErrorType . Warning : ErrorType . Error ) , "Patch lump \"" + p . LumpName + "\" data format could not be read, while loading texture \"" + this . Name + "\"" ) ;
2015-12-30 11:53:03 +00:00
missingpatches + + ; //mxd
2009-04-19 18:07:22 +00:00
}
2015-12-30 11:53:03 +00:00
2009-04-19 18:07:22 +00:00
if ( patchbmp ! = null )
{
2015-12-30 11:53:03 +00:00
//mxd. Apply transformations from TexturePatch
patchbmp = TransformPatch ( p , patchbmp ) ;
2013-03-18 13:52:27 +00:00
2009-04-19 18:07:22 +00:00
// Draw the patch on the texture image
2016-03-16 23:26:53 +00:00
Rectangle tgtrect = new Rectangle ( p . X , p . Y , patchbmp . Size . Width , patchbmp . Size . Height ) ;
2009-04-19 18:07:22 +00:00
g . DrawImageUnscaledAndClipped ( patchbmp , tgtrect ) ;
patchbmp . Dispose ( ) ;
}
}
// Done
mem . Dispose ( ) ;
}
else
{
2015-12-30 11:53:03 +00:00
//mxd. ZDoom can use any known graphic as patch
if ( General . Map . Config . MixTexturesFlats )
{
2016-03-16 23:26:53 +00:00
ImageData img = General . Map . Data . GetTextureImage ( p . LumpName ) ;
2016-02-06 23:54:58 +00:00
if ( ! ( img is UnknownImage ) & & img ! = this )
2015-12-30 11:53:03 +00:00
{
if ( ! img . IsImageLoaded ) img . LoadImage ( ) ;
//mxd. Apply transformations from TexturePatch. We don't want to modify the original bitmap here, so make a copy
Bitmap patchbmp = TransformPatch ( p , new Bitmap ( img . GetBitmap ( ) ) ) ;
// Draw the patch on the texture image
2016-03-16 23:26:53 +00:00
Rectangle tgtrect = new Rectangle ( p . X , p . Y , patchbmp . Size . Width , patchbmp . Size . Height ) ;
2015-12-30 11:53:03 +00:00
g . DrawImageUnscaledAndClipped ( patchbmp , tgtrect ) ;
patchbmp . Dispose ( ) ;
continue ;
}
}
2009-04-19 18:07:22 +00:00
// Missing a patch lump!
2016-06-13 23:37:55 +00:00
if ( ! nulltexture ) General . ErrorLogger . Add ( ( optional ? ErrorType . Warning : ErrorType . Error ) , "Missing patch lump \"" + p . LumpName + "\" while loading texture \"" + this . Name + "\"" ) ;
2015-12-30 11:53:03 +00:00
missingpatches + + ; //mxd
2009-04-19 18:07:22 +00:00
}
}
}
// Dispose bitmap if load failed
2016-06-13 23:37:55 +00:00
if ( ! nulltexture & & ( bitmap ! = null ) & & ( loadfailed | | missingpatches > = patches . Count ) ) //mxd. We can still display texture if at least one of the patches was loaded
2009-04-19 18:07:22 +00:00
{
bitmap . Dispose ( ) ;
bitmap = null ;
2013-07-29 08:50:50 +00:00
loadfailed = true ;
2009-04-19 18:07:22 +00:00
}
// Pass on to base
base . LocalLoadImage ( ) ;
}
}
2015-12-30 11:53:03 +00:00
//mxd
private Bitmap TransformPatch ( TexturePatch p , Bitmap patchbmp )
{
//mxd. Flip
2016-03-16 23:26:53 +00:00
if ( p . FlipX | | p . FlipY )
2015-12-30 11:53:03 +00:00
{
RotateFlipType flip ;
2016-03-16 23:26:53 +00:00
if ( p . FlipX & & ! p . FlipY ) flip = RotateFlipType . RotateNoneFlipX ;
else if ( ! p . FlipX & & p . FlipY ) flip = RotateFlipType . RotateNoneFlipY ;
2016-02-22 08:04:06 +00:00
else flip = RotateFlipType . RotateNoneFlipXY ;
2015-12-30 11:53:03 +00:00
patchbmp . RotateFlip ( flip ) ;
}
//mxd. Then rotate. I do it this way because RotateFlip function rotates THEN flips, and GZDoom does it the other way around.
2016-03-16 23:26:53 +00:00
if ( p . Rotate ! = 0 )
2015-12-30 11:53:03 +00:00
{
RotateFlipType rotate ;
2016-03-16 23:26:53 +00:00
switch ( p . Rotate )
2015-12-30 11:53:03 +00:00
{
2016-02-22 08:04:06 +00:00
case 90 : rotate = RotateFlipType . Rotate90FlipNone ; break ;
case 180 : rotate = RotateFlipType . Rotate180FlipNone ; break ;
default : rotate = RotateFlipType . Rotate270FlipNone ; break ;
2015-12-30 11:53:03 +00:00
}
patchbmp . RotateFlip ( rotate ) ;
}
// Adjust patch alpha, apply tint or blend
2016-03-16 23:26:53 +00:00
if ( p . BlendStyle ! = TexturePathBlendStyle . NONE | | p . RenderStyle ! = TexturePathRenderStyle . COPY )
2015-12-30 11:53:03 +00:00
{
BitmapData bmpdata = null ;
try
{
bmpdata = patchbmp . LockBits ( new Rectangle ( 0 , 0 , patchbmp . Size . Width , patchbmp . Size . Height ) , ImageLockMode . ReadWrite , PixelFormat . Format32bppArgb ) ;
}
catch ( Exception e )
{
2016-03-16 23:26:53 +00:00
General . ErrorLogger . Add ( ErrorType . Error , "Cannot lock image \"" + p . LumpName + "\" for alpha adjustment. " + e . GetType ( ) . Name + ": " + e . Message ) ;
2015-12-30 11:53:03 +00:00
}
if ( bmpdata ! = null )
{
PixelColor * pixels = ( PixelColor * ) ( bmpdata . Scan0 . ToPointer ( ) ) ;
int numpixels = bmpdata . Width * bmpdata . Height ;
2016-03-16 23:26:53 +00:00
int patchalpha = ( int ) Math . Round ( General . Clamp ( p . Alpha , 0f , 1f ) * 255 ) ; //convert alpha to [0-255] range
2015-12-30 11:53:03 +00:00
//mxd. Blend/Tint support
2016-03-16 23:26:53 +00:00
if ( p . BlendStyle = = TexturePathBlendStyle . BLEND )
2015-12-30 11:53:03 +00:00
{
for ( PixelColor * cp = pixels + numpixels - 1 ; cp > = pixels ; cp - - )
{
2016-03-16 23:26:53 +00:00
cp - > r = ( byte ) ( ( cp - > r * p . BlendColor . r ) * PixelColor . BYTE_TO_FLOAT ) ;
cp - > g = ( byte ) ( ( cp - > g * p . BlendColor . g ) * PixelColor . BYTE_TO_FLOAT ) ;
cp - > b = ( byte ) ( ( cp - > b * p . BlendColor . b ) * PixelColor . BYTE_TO_FLOAT ) ;
2015-12-30 11:53:03 +00:00
}
}
2016-03-16 23:26:53 +00:00
else if ( p . BlendStyle = = TexturePathBlendStyle . TINT )
2015-12-30 11:53:03 +00:00
{
2016-03-16 23:26:53 +00:00
float tintammount = p . BlendColor . a * PixelColor . BYTE_TO_FLOAT ; // -0.1f;
2015-12-30 11:53:03 +00:00
if ( tintammount > 0 )
{
2016-03-16 23:26:53 +00:00
float br = p . BlendColor . r * PixelColor . BYTE_TO_FLOAT * tintammount ;
float bg = p . BlendColor . g * PixelColor . BYTE_TO_FLOAT * tintammount ;
float bb = p . BlendColor . b * PixelColor . BYTE_TO_FLOAT * tintammount ;
float invtintammount = 1.0f - tintammount ;
2015-12-30 11:53:03 +00:00
for ( PixelColor * cp = pixels + numpixels - 1 ; cp > = pixels ; cp - - )
{
2016-03-16 23:26:53 +00:00
cp - > r = ( byte ) ( ( ( cp - > r * PixelColor . BYTE_TO_FLOAT ) * invtintammount + br ) * 255.0f ) ;
cp - > g = ( byte ) ( ( ( cp - > g * PixelColor . BYTE_TO_FLOAT ) * invtintammount + bg ) * 255.0f ) ;
cp - > b = ( byte ) ( ( ( cp - > b * PixelColor . BYTE_TO_FLOAT ) * invtintammount + bb ) * 255.0f ) ;
2015-12-30 11:53:03 +00:00
}
}
}
//mxd. Apply RenderStyle
2016-03-16 23:26:53 +00:00
if ( p . RenderStyle = = TexturePathRenderStyle . BLEND )
2015-12-30 11:53:03 +00:00
{
for ( PixelColor * cp = pixels + numpixels - 1 ; cp > = pixels ; cp - - )
cp - > a = ( byte ) ( ( cp - > a * patchalpha ) * PixelColor . BYTE_TO_FLOAT ) ;
}
//mxd. We need a copy of underlying part of texture for these styles
2016-03-16 23:26:53 +00:00
else if ( p . RenderStyle ! = TexturePathRenderStyle . COPY )
2015-12-30 11:53:03 +00:00
{
// Copy portion of texture
2016-03-16 23:26:53 +00:00
int lockWidth = ( p . X + patchbmp . Size . Width > bitmap . Width ) ? bitmap . Width - p . X : patchbmp . Size . Width ;
int lockHeight = ( p . Y + patchbmp . Size . Height > bitmap . Height ) ? bitmap . Height - p . Y : patchbmp . Size . Height ;
2015-12-30 11:53:03 +00:00
Bitmap source = new Bitmap ( patchbmp . Size . Width , patchbmp . Size . Height ) ;
using ( Graphics sg = Graphics . FromImage ( source ) )
2016-03-16 23:26:53 +00:00
sg . DrawImageUnscaled ( bitmap , new Rectangle ( - p . X , - p . Y , lockWidth , lockHeight ) ) ;
2015-12-30 11:53:03 +00:00
// Lock texture
BitmapData texturebmpdata = null ;
try
{
texturebmpdata = source . LockBits ( new Rectangle ( 0 , 0 , source . Width , source . Height ) , ImageLockMode . ReadWrite , PixelFormat . Format32bppArgb ) ;
}
catch ( Exception e )
{
2016-02-15 14:06:46 +00:00
General . ErrorLogger . Add ( ErrorType . Error , "Cannot lock texture \"" + this . Name + "\" to apply render style. " + e . GetType ( ) . Name + ": " + e . Message ) ;
2015-12-30 11:53:03 +00:00
}
if ( texturebmpdata ! = null )
{
PixelColor * texturepixels = ( PixelColor * ) ( texturebmpdata . Scan0 . ToPointer ( ) ) ;
PixelColor * tcp = texturepixels + numpixels - 1 ;
2016-03-16 23:26:53 +00:00
switch ( p . RenderStyle )
2015-12-30 11:53:03 +00:00
{
2016-03-16 23:26:53 +00:00
case TexturePathRenderStyle . ADD :
2015-12-30 11:53:03 +00:00
for ( PixelColor * cp = pixels + numpixels - 1 ; cp > = pixels ; cp - - )
{
cp - > r = ( byte ) Math . Min ( 255 , cp - > r + tcp - > r ) ;
cp - > g = ( byte ) Math . Min ( 255 , cp - > g + tcp - > g ) ;
cp - > b = ( byte ) Math . Min ( 255 , cp - > b + tcp - > b ) ;
cp - > a = ( byte ) ( ( cp - > a * patchalpha ) * PixelColor . BYTE_TO_FLOAT ) ;
tcp - - ;
}
break ;
2016-03-16 23:26:53 +00:00
case TexturePathRenderStyle . SUBTRACT :
2015-12-30 11:53:03 +00:00
for ( PixelColor * cp = pixels + numpixels - 1 ; cp > = pixels ; cp - - )
{
cp - > r = ( byte ) Math . Max ( 0 , tcp - > r - cp - > r ) ;
cp - > g = ( byte ) Math . Max ( 0 , tcp - > g - cp - > g ) ;
cp - > b = ( byte ) Math . Max ( 0 , tcp - > b - cp - > b ) ;
cp - > a = ( byte ) ( ( cp - > a * patchalpha ) * PixelColor . BYTE_TO_FLOAT ) ;
tcp - - ;
}
break ;
2016-03-16 23:26:53 +00:00
case TexturePathRenderStyle . REVERSE_SUBTRACT :
2015-12-30 11:53:03 +00:00
for ( PixelColor * cp = pixels + numpixels - 1 ; cp > = pixels ; cp - - )
{
cp - > r = ( byte ) Math . Max ( 0 , cp - > r - tcp - > r ) ;
cp - > g = ( byte ) Math . Max ( 0 , cp - > g - tcp - > g ) ;
cp - > b = ( byte ) Math . Max ( 0 , cp - > b - tcp - > b ) ;
cp - > a = ( byte ) ( ( cp - > a * patchalpha ) * PixelColor . BYTE_TO_FLOAT ) ;
tcp - - ;
}
break ;
2016-03-16 23:26:53 +00:00
case TexturePathRenderStyle . MODULATE :
2015-12-30 11:53:03 +00:00
for ( PixelColor * cp = pixels + numpixels - 1 ; cp > = pixels ; cp - - )
{
cp - > r = ( byte ) ( ( cp - > r * tcp - > r ) * PixelColor . BYTE_TO_FLOAT ) ;
cp - > g = ( byte ) ( ( cp - > g * tcp - > g ) * PixelColor . BYTE_TO_FLOAT ) ;
cp - > b = ( byte ) ( ( cp - > b * tcp - > b ) * PixelColor . BYTE_TO_FLOAT ) ;
tcp - - ;
}
break ;
}
source . UnlockBits ( texturebmpdata ) ;
}
}
patchbmp . UnlockBits ( bmpdata ) ;
}
}
return patchbmp ;
}
2009-04-19 18:07:22 +00:00
#endregion
}
}