public UncompressedCollection(int imgWid, int imgHei, System.IO.Stream inFile, Palette pal) { System.IO.BinaryReader sr = new System.IO.BinaryReader(inFile); int i=0; while(sr.BaseStream.Position<sr.BaseStream.Length) Add(new XCImage(sr.ReadBytes(imgWid*imgHei),imgWid,imgHei,pal,i++)); }
public PckFile GetPckFile(Palette p) { if(pckTab[p]==null) pckTab[p] = GameInfo.CachePck(basePath,basename,2,p);//new PckFile(File.OpenRead(basePath+basename+".PCK"),File.OpenRead(basePath+basename+".TAB"),2,p,screen);//GameInfo.GetPckFile(basename,basePath,p,2,screen); return (PckFile)pckTab[p]; }
internal PckImage(int imageNum,byte[] idx,Palette pal,PckFile pFile,int height,int width) { Palette=pal; pckFile = pFile; this.fileNum=imageNum; //this.imageNum=imageNum; //this.idx=idx; staticID=globalStaticID++; Height=height; Width=width; //image = new Bitmap(Width,Height,PixelFormat.Format8bppIndexed); expanded = new byte[Width*Height]; for(int i=0;i<expanded.Length;i++) expanded[i] = TransparentIndex; int ex=0; int startIdx=0; if(idx[0]!=254) ex = idx[startIdx++]*Width; for(int i=startIdx;i<idx.Length;i++) { switch(idx[i]) { case 254: //skip required pixels { if(moveIdx==-1) { moveIdx=i+1; moveVal=idx[i+1]; } ex+=idx[i+1]; i++; } break; case 255: //end of image break; default: expanded[ex++]=idx[i]; break; } } this.idx=expanded; image = Bmp.MakeBitmap8(Width,Height,expanded,pal.Colors); gray = Bmp.MakeBitmap8(Width,Height,expanded,pal.Grayscale.Colors); }
public PckFile(Stream pckFile, Stream tabFile,int bpp,Palette pal,int imgHeight,int imgWidth) { if(tabFile!=null) tabFile.Position=0; pckFile.Position=0; byte[] info = new byte[pckFile.Length]; pckFile.Read(info,0,info.Length); pckFile.Close(); this.bpp=bpp; Pal=pal; uint[] offsets; if(tabFile!=null) { offsets= new uint[(tabFile.Length/bpp)+1]; BinaryReader br = new BinaryReader(tabFile); if(bpp==2) for(int i=0;i<tabFile.Length/bpp;i++) offsets[i] = br.ReadUInt16(); else for(int i=0;i<tabFile.Length/bpp;i++) offsets[i] = br.ReadUInt32(); br.Close(); } else { offsets = new uint[2]; offsets[0]=0; } offsets[offsets.Length-1] = (uint)info.Length; for(int i=0;i<offsets.Length-1;i++) { byte[] imgDat = new byte[offsets[i+1]-offsets[i]]; for(int j=0;j<imgDat.Length;j++) imgDat[j] = info[offsets[i]+j]; Add(new PckImage(i,imgDat,pal,this,imgHeight,imgWidth)); } pckFile.Close(); if(tabFile!=null) tabFile.Close(); }
public SPKImage(Palette p, Stream s, int width, int height) { //int transparent=254; idx = new byte[width*height]; for(int i=0;i<idx.Length;i++) idx[i]=254; long pix=0; BinaryReader data = new BinaryReader(s); try { while(data.BaseStream.Position < data.BaseStream.Length) { int cas = data.ReadUInt16(); switch(cas) { case 0xFFFF: { long val = data.ReadUInt16()*2; pix+=val; break; } case 0xFFFE: { long val = data.ReadUInt16()*2; while((val--)>0) { idx[pix++] = data.ReadByte(); } break; } case 0xFFFD: { image = Bmp.MakeBitmap8(width,height,idx,p.Colors); Palette=p; data.Close(); return; } } } } catch{} image = Bmp.MakeBitmap8(width,height,idx,p.Colors); Palette=p; data.Close(); }
//protected string tileset; public XCMapDesc( string basename, string basePath, string blankPath, //string tileset, string rmpPath, string[] dependencies, Palette myPal):base(basename) { this.myPal = myPal; this.basename = basename; this.basePath = basePath; this.rmpPath = rmpPath; this.blankPath = blankPath; //this.tileset = tileset; this.dependencies = dependencies; isStatic = false; }
public override IUnit GetNewUnit(Palette p) { if(myFile==null) { PckFile myPck; try { myPck = GameInfo.CachePck(basePath,basename,4,p); } catch { myPck = GameInfo.CachePck(basePath,basename,2,p); } myFile = new Type4File(this); myFile.ImageFile=myPck; } return new HumanSoldier1((Type4File)myFile,p); }
public override IUnit GetNewUnit(Palette p) { if(myFile==null) { PckFile myPck; try { myPck = GameInfo.CachePck(basePath,basename,4,p); } catch { myPck = GameInfo.CachePck(basePath,basename,2,p); } myFile = new Type0File(this,myPck.Count); myFile.ImageFile=myPck; } AlienSoldier a = new AlienSoldier((Type0File)myFile,p); a.Name = basename; return a; }
public static void Init(Palette p, DSShared.PathInfo paths) { currentPalette = p; pckHash = new Dictionary<Palette, Dictionary<string, PckFile>>(); VarCollection vars = new VarCollection(new StreamReader(File.OpenRead(paths.ToString()))); Directory.SetCurrentDirectory(paths.Path); xConsole.Init(20); KeyVal kv = null; while((kv=vars.ReadLine())!=null) { switch (kv.Keyword) { /* mapedit */case "mapdata": tileInfo = new TilesetDesc(kv.Rest, vars); break; /* mapedit */case "images": imageInfo = new ImageInfo(kv.Rest, vars); break; case "useBlanks": Globals.UseBlanks = bool.Parse(kv.Rest); break; default: if (ParseLine != null) ParseLine(kv, vars); else xConsole.AddLine("Error in paths file: " + kv); break; } } vars.BaseStream.Close(); }
public BDYImage(Palette p,Stream s,int width, int height) { BinaryReader data = new BinaryReader(s); idx = new byte[width*height]; for(int i=0;i<idx.Length;i++) idx[i]=254; int x = 0; while(data.BaseStream.Position<data.BaseStream.Length) { int space = data.ReadByte(); byte c = data.ReadByte(); if(space>=129) { space = 256-space+1; for(int i=0;i<space;i++) idx[x++]=c; } else { idx[x++]=c; for(int i=0;i<space;i++) { c=data.ReadByte(); idx[x++]=c; } } } image = Bmp.MakeBitmap8(320,200,idx,p.Colors); Palette=p; data.Close(); }
public PckImage8 Clone(Palette pal) { byte[] b = new byte[idx.Length]; for(int i=0;i<b.Length;i++) b[i]=idx[i]; return new PckImage8(imageNum,b,pal); }
internal PckImage8(int imageNum,byte[] bytes,Palette pal) { this.pal=pal; this.imageNum=imageNum; idx=bytes; image = new Bitmap(IMAGE_WIDTH,IMAGE_HEIGHT,PixelFormat.Format8bppIndexed); byte[] expanded = new byte[IMAGE_WIDTH*IMAGE_HEIGHT]; for(int i=0;i<expanded.Length;i++) expanded[i] = TRANSPARENT_COLOR_INDEX; int ex = idx[0]*IMAGE_WIDTH; for(int i=1;i<idx.Length;i++) { switch(idx[i]) { case 254: //skip required pixels ex+=idx[i+1]; i++; break; case 255: //end of image break; default: expanded[ex++]=idx[i]; break; } } Rectangle rect = new Rectangle(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT); BitmapData bitmapData = image.LockBits(rect,ImageLockMode.WriteOnly,PixelFormat.Format8bppIndexed); // Write to the temporary buffer that is provided by LockBits. // Copy the pixels from the source image in this loop. // Because you want an index, convert RGB to the appropriate // palette index here. IntPtr pixels = bitmapData.Scan0; unsafe { // Get the pointer to the image bits. // This is the unsafe operation. byte * pBits; if (bitmapData.Stride > 0) pBits = (byte *)pixels.ToPointer(); else // If the Stide is negative, Scan0 points to the last // scanline in the buffer. To normalize the loop, obtain // a pointer to the front of the buffer that is located // (Height-1) scanlines previous. pBits = (byte *)pixels.ToPointer() + bitmapData.Stride*(IMAGE_HEIGHT-1); uint stride = (uint)Math.Abs(bitmapData.Stride); ex=0; for ( uint row = 0; row < IMAGE_HEIGHT; ++row ) for ( uint col = 0; col < IMAGE_WIDTH; ++col ) { // The destination pixel. // The pointer to the color index byte of the // destination; this real pointer causes this // code to be considered unsafe. byte * p8bppPixel = pBits + row*stride + col; *p8bppPixel = expanded[ex++]; } } image.UnlockBits(bitmapData); image.Palette = pal.Colors; }
public static PckImage8 FromBMP(Bitmap b,int num,Palette pal,int startX,int startY) { int count=0; bool flag=true; ArrayList bytes = new ArrayList(); ArrayList entries = new ArrayList(b.Palette.Entries); //bool trans = pal.Transparent; //pal.Transparent=false; for(int r=startY;r<startY+PckImage.IMAGE_HEIGHT;r++) for(int c=startX;c<startX+PckImage.IMAGE_WIDTH;c++) { byte idx = (byte)entries.IndexOf(b.GetPixel(c,r)); if(idx==PckImage.TRANSPARENT_COLOR_INDEX) count++; else { if(count!=0) { if(flag) { bytes.Add((byte)(count/PckImage.IMAGE_WIDTH)); count = (byte)(count%PckImage.IMAGE_WIDTH); flag=false; //Console.WriteLine("count, lines: {0}, cells {1}",count/PckImage.IMAGE_WIDTH,count%PckImage.IMAGE_WIDTH); } bytes.Add((byte)PckImage.TRANSPARENT_COLOR_INDEX); bytes.Add((byte)count); count=0; } bytes.Add((byte)idx); } } while(count>=255) { bytes.Add((byte)254); bytes.Add((byte)255); count-=255; } bytes.Add((byte)255); //pal.Transparent=trans; return new PckImage8(num,(byte[])bytes.ToArray(typeof(byte)),pal); }
public PckFile( Stream pckFile, Stream tabFile, int bpp, Palette pal, int imgHeight, int imgWidth) { if (tabFile != null) { tabFile.Position = 0; } pckFile.Position = 0; byte[] info = new byte[pckFile.Length]; pckFile.Read(info, 0, info.Length); this.bpp = bpp; Pal = pal; uint[] offsets; if (tabFile != null) { offsets = new uint[(tabFile.Length / bpp) + 1]; BinaryReader br = new BinaryReader(tabFile); if (bpp == 2) { for (int i = 0; i < tabFile.Length / bpp; i++) { offsets[i] = br.ReadUInt16(); } } else { for (int i = 0; i < tabFile.Length / bpp; i++) { offsets[i] = br.ReadUInt32(); } } br.Close(); } else { offsets = new uint[2]; offsets[0] = 0; } offsets[offsets.Length - 1] = (uint)info.Length; for (int i = 0; i < offsets.Length - 1; i++) { byte[] imgDat = new byte[offsets[i + 1] - offsets[i]]; for (int j = 0; j < imgDat.Length; j++) { imgDat[j] = info[offsets[i] + j]; } Add(new PckImage( i, imgDat, pal, this, imgHeight, imgWidth)); } }
public AlienSoldier(Type0File file,Palette p):base(file,p) { images=file; }
public McdFile GetMcdFile(Palette p) { if(mcdTab[p]==null) mcdTab[p] = new McdFile(basename,basePath,GetPckFile(p));//GameInfo.GetMcdFile(basename,basePath,GetPckFile(p)); return (McdFile)mcdTab[p]; }
public Type1Unit(Type1File file,Palette p):base(file,p) { images=file; //curr = DXTimer.RandInt()%bob.Length; }
public static PckFile CachePck(string basePath,string basename,int bpp, Palette p) { if (pckHash == null) pckHash = new Dictionary<Palette, Dictionary<string, PckFile>>(); if (!pckHash.ContainsKey(p)) pckHash.Add(p, new Dictionary<string, PckFile>()); //if(pckHash[p][basePath+basename]==null) if(!pckHash[p].ContainsKey(basePath+basename)) pckHash[p].Add(basePath+basename,new PckFile(File.OpenRead(basePath+basename+".PCK"),File.OpenRead(basePath+basename+".TAB"),bpp,p)); return pckHash[p][basePath+basename]; }
public Type6Unit(Type6File file,Palette p):base(file,p) { images=file; //curr = DXTimer.RandInt()%8; big=true; }
public HumanSoldier2(Type4File file, Palette p) : base(file, p) { images = file; }
public PckFile(Stream pckFile, Stream tabFile,int bpp,Palette pal):this(pckFile,tabFile,bpp,pal,40,32) {}
public Type2Unit(Type2File file,Palette p):base(file,p) { images=file; }
/// <summary> /// Loads a given spriteset for UFO or TFTD. This could go in Descriptor /// except the XCOM cursor-sprites load w/out a descriptor. So do the /// 'ExtraSprites'. /// @note Both UFO and TFTD use 2-byte Tab-offsetLengths for 32x40 terrain pcks /// (TFTD unitsprites use 4-byte Tab-offsetLengths although Bigobs 32x48 uses 2-byte) /// (the UFO cursor uses 2-byte but the TFTD cursor uses 4-byte) /// </summary> /// <param name="terrain">the terrain file w/out extension</param> /// <param name="dirTerrain">path to the directory of the terrain file</param> /// <param name="offsetLength"></param> /// <param name="pal"></param> /// <returns>a SpriteCollection containing all the sprites for a given Terrain</returns> public static SpriteCollection LoadSpriteset( string terrain, string dirTerrain, int offsetLength, Palette pal) { //LogFile.WriteLine(""); //LogFile.WriteLine("ResourceInfo.LoadSpriteset"); if (!String.IsNullOrEmpty(dirTerrain)) { //LogFile.WriteLine(". dirTerrain= " + dirTerrain); //LogFile.WriteLine(". terrain= " + terrain); var pfSpriteset = Path.Combine(dirTerrain, terrain); //LogFile.WriteLine(". pfSpriteset= " + pfSpriteset); string pfePck = pfSpriteset + GlobalsXC.PckExt; string pfeTab = pfSpriteset + GlobalsXC.TabExt; if (File.Exists(pfePck) && File.Exists(pfeTab)) { if (!_palSpritesets.ContainsKey(pal)) { _palSpritesets.Add(pal, new Dictionary <string, SpriteCollection>()); } var spritesets = _palSpritesets[pal]; if (ReloadSprites) // used by XCMainWindow.OnPckSavedEvent <- TileView's pck-editor { //LogFile.WriteLine(". ReloadSprites"); if (spritesets.ContainsKey(pfSpriteset)) { spritesets.Remove(pfSpriteset); } } if (!spritesets.ContainsKey(pfSpriteset)) { //LogFile.WriteLine(". . key not found in spriteset dictionary -> add new SpriteCollection"); using (var fsPck = File.OpenRead(pfePck)) using (var fsTab = File.OpenRead(pfeTab)) { var spriteset = new SpriteCollection( fsPck, fsTab, offsetLength, pal); if (spriteset.Borked) { MessageBox.Show( "The quantity of sprites in the PCK file does not match the" + " quantity of sprites expected by the TAB file." + Environment.NewLine + Environment.NewLine + pfePck + Environment.NewLine + pfeTab, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0); } spritesets.Add(pfSpriteset, spriteset); // NOTE: Add the spriteset even if it is Borked. } } // else WARN: Spriteset already found in the collection. return(_palSpritesets[pal][pfSpriteset]); } MessageBox.Show( "Can't find files for the spriteset" + Environment.NewLine + Environment.NewLine + pfePck + Environment.NewLine + pfeTab + Environment.NewLine + Environment.NewLine + "Open the Map in the TilesetEditor and re-assign the basepath" + " for the TERRAIN folder of the .PCK and .TAB files.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, 0); } return(null); }
/* private const int LeftArmStill = 0; private const int RightArmStill = 8; private const int LegsStillStand=16; private const int LegsStillCrouch=24; private const int UnderwaterHead = 32; private const int LeftArmMove = 40; private const int RightArmMove = 48; private const int LegsMove = 56; private const int LeftArmArmed2 = 232; private const int RightArmArmed2 = 240; private const int RightArmArmed1 = 248; private const int DeathFrames = 3; private const int DeathAboveWater = 256; private const int DeathUnderWater = 259; private const int FemaleHead = 262; private const int MaleHead = 270; private const int ZombieHead = 278;*/ #endregion public HumanSoldier1(Type4File file,Palette p):base(file,p) { images=file; }
public Type7Unit(Type7File file, Palette p) : base(file, p) { images = file; //curr = DXTimer.RandInt()%8; big = true; }
/// <summary> /// Loads a previously saved sprite sheet as a generic collection to be saved later /// </summary> /// <param name="b">bitmap containing sprites</param> /// <returns></returns> public static XCImageCollection Load(Bitmap b,Palette pal,int imgWid,int imgHei,int space) { XCImageCollection list = new XCImageCollection(); int cols = (b.Width+space)/(imgWid+space); int rows = (b.Height+space)/(imgHei+space); int num=0; //Console.WriteLine("Image: {0},{1} -> {2},{3}",b.Width,b.Height,cols,rows); for(int i=0;i<cols*rows;i++) { int x = (i%cols)*(imgWid+space); int y = (i/cols)*(imgHei+space); //Console.WriteLine("{0}: {1},{2} -> {3}",num,x,y,PckImage.Width); list.Add(LoadTile(b,num++,pal,x,y,imgWid,imgHei)); FireLoadingEvent(num,rows*cols); } list.Pal=pal; return list; }
public AlienSoldier(Type0File file, Palette p) : base(file, p) { images = file; }
public Type2Unit(Type2File file, Palette p) : base(file, p) { images = file; }
public Type3Unit(Type3File file,Palette p):base(file,p) { images=file; //curr = DXTimer.RandInt()%8; }
public static McdFile GetMcdFile(string imageSet,Palette p) { return imageInfo.Images[imageSet].GetMcdFile(p); }
internal PckImage( int imageNum, byte[] idx, Palette pal, PckFile pFile, int height, int width) { Palette = pal; pckFile = pFile; this.fileNum = imageNum; // this.imageNum = imageNum; // this.idx = idx; staticID = globalStaticID++; Height = height; Width = width; // image = new Bitmap(Width,Height,PixelFormat.Format8bppIndexed); expanded = new byte[Width * Height]; for (int i = 0; i < expanded.Length; i++) { expanded[i] = TransparentIndex; } int ex = 0; int startIdx = 0; if (idx[0] != 254) { ex = idx[startIdx++] * Width; } for (int i = startIdx; i < idx.Length; i++) { switch (idx[i]) { case 254: // skip required pixels if (moveIdx == -1) { moveIdx = i + 1; moveVal = idx[i + 1]; } ex += idx[i + 1]; i++; break; case 255: // end of image break; default: expanded[ex++] = idx[i]; break; } } this.idx = expanded; image = Bmp.MakeBitmap8( Width, Height, expanded, pal.Colors); gray = Bmp.MakeBitmap8( Width, Height, expanded, pal.Grayscale.Colors); }
public static Palette GetPalette(string name) { if(palHash[name]==null) { Assembly thisAssembly = Assembly.GetExecutingAssembly(); try { palHash[name] = new Palette(thisAssembly.GetManifestResourceStream(embedPath+name+".pal")); } catch{palHash[name]=null;} } return (Palette)palHash[name]; }
/// <summary> /// Saves an image collection as a bmp sprite sheet /// </summary> /// <param name="file">output file name</param> /// <param name="collection">image collection</param> /// <param name="pal">Palette to color the images with</param> /// <param name="across">number of columns to use for images</param> public static void SaveBMP(string file,XCImageCollection collection,Palette pal,int across,int space) { if (collection.Count == 1) across = 1; int mod=1; if(collection.Count%across==0) mod=0; Bitmap b = MakeBitmap(across*(collection.IXCFile.ImageSize.Width+space)-space,(collection.Count/across+mod)*(collection.IXCFile.ImageSize.Height+space)-space,pal.Colors); for(int i=0;i<collection.Count;i++) { int x = i%across*(collection.IXCFile.ImageSize.Width+space); int y = i/across*(collection.IXCFile.ImageSize.Height+space); Draw(collection[i].Image,b,x,y); } Save(file,b); }
/// <summary> /// Helper for LoadSpriteset() /// also called by PckViewForm.OnAddSpriteClick() and .OnReplaceSpriteClick() /// </summary> /// <param name="bitmap"></param> /// <param name="terrainId"></param> /// <param name="pal"></param> /// <param name="x"></param> /// <param name="y"></param> /// <param name="width"></param> /// <param name="height"></param> /// <returns></returns> public static XCImage CreateSprite( Bitmap bitmap, int terrainId, Palette pal, int x, int y, int width, int height) { //LogFile.WriteLine("BitmapService.CreateSprite"); var bindata = new byte[width * height]; // image data in 8-bpp form var rect = new Rectangle( x, y, width, height); var locked = bitmap.LockBits( rect, ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed); var pixels = locked.Scan0; unsafe { byte *bits; if (locked.Stride > 0) { bits = (byte *)pixels.ToPointer(); } else { bits = (byte *)pixels.ToPointer() + locked.Stride * (bitmap.Height - 1); } uint stride = (uint)Math.Abs(locked.Stride); int i = 0; for (uint row = 0; row != height; ++row) { for (uint col = 0; col != width; ++col) { // bindata[i++] = *(bits + row * stride + col); // bork. byte palId = *(bits + row * stride + col); switch (palId) { case PckImage.SpriteStopByte: // convert #255 transparency to #0. palId = 0; break; case PckImage.SpriteTransparencyByte: // drop #254 transparency-marker down to #253. palId = 253; break; } bindata[i++] = palId; //LogFile.WriteLine(". " + (i-1) + ":" + palId); } } } bitmap.UnlockBits(locked); return(new XCImage(bindata, width, height, pal, terrainId)); }
public static XCImage LoadTile(Bitmap src,int imgNum,Palette p, int startX, int startY, int imgWid, int imgHei) { //image data in 8-bit form byte[] data = new byte[imgWid*imgHei]; Rectangle srcRect = new Rectangle(startX, startY, imgWid,imgHei); BitmapData srcData = src.LockBits(srcRect,ImageLockMode.ReadOnly,PixelFormat.Format8bppIndexed); IntPtr srcPixels = srcData.Scan0; unsafe { byte * sBits; if (srcData.Stride > 0) sBits = (byte *)srcPixels.ToPointer(); else sBits = (byte *)srcPixels.ToPointer() + srcData.Stride*(src.Height-1); uint sStride = (uint)Math.Abs(srcData.Stride); for ( uint row = 0,i=0; row < imgHei; row++ ) for ( uint col = 0; col < imgWid; col++ ) data[i++] = *(sBits + row*sStride + col); } src.UnlockBits(srcData); return new XCImage(data,imgWid,imgHei,p,imgNum); }
internal PckImage(int imageNum,byte[] idx,Palette pal,PckFile pFile):this(imageNum,idx,pal,pFile,40,32){}