public PckFile(Stream pckFile, Stream tabFile,int bpp) { byte[] info = new byte[pckFile.Length]; pckFile.Read(info,0,info.Length); pckFile.Close(); this.pal=DefaultPalette; uint[] 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(); offsets[offsets.Length-1] = (uint)info.Length; images = new ArrayList(offsets.Length-1); 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]; images.Add(new PckImage(i,imgDat,pal)); if(LoadingEvent!=null) LoadingEvent(i,offsets.Length-1); } br.Close(); }
public void SaveBMP(string file,Palette pal,int bpp,int across) { int mod=1; if(Size%across==0) mod=0; int space=imgSpace; Bitmap b = new Bitmap(across*(ImgWidth+space)-space,(Size/across+mod)*(ImgHeight+space)-space); Graphics g = Graphics.FromImage(b); g.FillRectangle(new SolidBrush(pal.Colors.Entries[PckImage.TransparentIndex]),0,0,b.Width,b.Height); for(int i=0;i<Size;i++) { int x = i%across*(ImgWidth+space); int y = i/across*(ImgHeight+space); copy(this[i].Image,b,x,y); } b.Palette=pal.Colors; if(bpp==8) Bmp.Save(file,b); else Bmp.Save24(file,b); }
internal PckImage(int imageNum,byte[] bytes,Palette 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 ITile FromBmpSingle(Bitmap b, int num, Palette p) { return PckImage.FromBMP(b,num,p,0,0); }
public static PckImage 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 PckImage(num,(byte[])bytes.ToArray(typeof(byte)),pal); }
public static PckImage FromBMP24(Bitmap b,int num,Palette pal,int startX,int startY) { Bitmap img = new Bitmap(Width,Height,PixelFormat.Format24bppRgb); for(int r=0;r<Height;r++) for(int c=0;c<Width;c++) img.SetPixel(c,r,b.GetPixel(startX+c,startY+r)); return new PckImage(img,num); }
public ITile Clone(Palette pal) { byte[] b = new byte[idx.Length]; for(int i=0;i<b.Length;i++) b[i]=idx[i]; return new PckImage(imageNum,b,pal); }
public static ITile LoadSingle(Bitmap src,int num,Palette pal,Type collectionType) { MethodInfo mi = collectionType.GetMethod("FromBmpSingle"); if(mi==null) return null; else return (ITile)mi.Invoke(null,new object[]{src,num,pal}); }
public static ITile FromBmpSingle(Bitmap b, int num, Palette p) { return(PckImage.FromBMP(b, num, p, 0, 0)); }
// saves a bitmap as a 8-bit image public void SaveBMP(string file, Palette pal) { Bmp.SaveBMP(file, myFile, pal, numAcross(), 1); }
public PckViewForm() { InitializeComponent(); #region shared space information var consoleSharedSpace = new ConsoleSharedSpace(new SharedSpace()); console = consoleSharedSpace.GetNewConsole(); console.FormClosing += delegate(object sender, FormClosingEventArgs e) { e.Cancel = true; console.Hide(); }; FormClosed += (sender, e) => console.Close(); sharedSpace = SharedSpace.Instance; sharedSpace.GetObj("PckView", this); sharedSpace.GetObj("AppDir", Environment.CurrentDirectory); sharedSpace.GetObj("CustomDir", Environment.CurrentDirectory + "/custom"); sharedSpace.GetObj("SettingsDir", Environment.CurrentDirectory + "/settings"); xConsole.AddLine("Current directory: " + sharedSpace["AppDir"]); xConsole.AddLine("Custom directory: " + sharedSpace["CustomDir"].ToString()); #endregion _totalViewPck = new TotalViewPck(); _totalViewPck.Dock = DockStyle.Fill; DrawPanel.Controls.Add(_totalViewPck); _totalViewPck.View.DoubleClick += new EventHandler(doubleClick); _totalViewPck.ViewClicked += new PckViewMouseClicked(viewClicked); _totalViewPck.XCImageCollectionSet += new XCImageCollectionHandler(v_XCImageCollectionSet); _totalViewPck.ContextMenu = makeContextMenu(); SaveMenuItem.Visible = false; sharedSpace["Palettes"] = new Dictionary <string, Palette>(); palMI = new Dictionary <Palette, MenuItem>(); AddPalette(Palette.UFOBattle, miPalette); AddPalette(Palette.UFOGeo, miPalette); AddPalette(Palette.UFOGraph, miPalette); AddPalette(Palette.UFOResearch, miPalette); AddPalette(Palette.TFTDBattle, miPalette); AddPalette(Palette.TFTDGeo, miPalette); AddPalette(Palette.TFTDGraph, miPalette); AddPalette(Palette.TFTDResearch, miPalette); currPal = Palette.UFOBattle; // currPal = Palette.TFTDBattle; palMI[currPal].Checked = true; // kL_ufoPalette _totalViewPck.Pal = currPal; // kL_ufoPalette editor = new Editor(null); editor.Closing += new CancelEventHandler(editorClosing); if (editor != null) // kL_ufoPalette { editor.Palette = currPal; // kL_ufoPalette } RegistryInfo ri = new RegistryInfo(this, "PckView"); ri.AddProperty("FilterIndex"); ri.AddProperty("SelectedPalette"); if (!File.Exists("hq2xa.dll")) { miHq2x.Visible = false; } loadedTypes = new LoadOfType <IXCImageFile>(); loadedTypes.OnLoad += new LoadOfType <IXCImageFile> .TypeLoadDelegate(loadedTypes_OnLoad); sharedSpace["ImageMods"] = loadedTypes.AllLoaded; // loadedTypes.OnLoad += new LoadOfType<IXCFile>.TypeLoadDelegate(sortLoaded); loadedTypes.LoadFrom(Assembly.GetExecutingAssembly()); loadedTypes.LoadFrom(Assembly.GetAssembly(typeof(XCom.Interfaces.IXCImageFile))); if (Directory.Exists(sharedSpace["CustomDir"].ToString())) { //Console.WriteLine("Custom directory exists: " + sharedSpace["CustomDir"].ToString()); xConsole.AddLine("Custom directory exists: " + sharedSpace["CustomDir"].ToString()); foreach (string s in Directory.GetFiles(sharedSpace["CustomDir"].ToString())) { if (s.EndsWith(".dll")) { xConsole.AddLine("Loading dll: " + s); loadedTypes.LoadFrom(Assembly.LoadFrom(s)); } else if (s.EndsWith(xcProfile.PROFILE_EXT)) { foreach (xcProfile ip in ImgProfile.LoadFile(s)) { loadedTypes.Add(ip); } } } } osFilter = new OpenSaveFilter(); osFilter.SetFilter(IXCImageFile.Filter.Open); openDictionary = new Dictionary <int, IXCImageFile>(); saveDictionary = new Dictionary <int, IXCImageFile>(); osFilter.SetFilter(IXCImageFile.Filter.Open); string filter = loadedTypes.CreateFilter(osFilter, openDictionary); openFile.Filter = filter; }