Exemple #1
0
        public Bitmap DrawImage(Color[] colors)
        {
            if (Width < 1 || Height < 1) {
                return null;
            }

            var image = new Bitmap(Width, Height);
            var fastBmp = new FastBitmap(image);

            fastBmp.LockImage();
            for (int y = 0, i = 0; y < Height; y++) {
                for (var x = 0; x < Width; x++) {
                    fastBmp.SetPixel(x, y, colors[(int)Cells[i++].Type]);
                }
            }
            fastBmp.UnlockImage();

            return image;
        }
Exemple #2
0
		public Bitmap DrawImage(Color[] Colors) {
			if (Width < 1 || Height < 1) {
				return null;
			}

			Bitmap Image = new Bitmap(Width, Height);
			FastBitmap fastBmp = new FastBitmap(Image);

			using (Graphics g = Graphics.FromImage(Image)) {

				fastBmp.LockImage();
				for (int y = 0, i = 0; y < Height; y++) {
					for (int x = 0; x < Width; x++) {
						fastBmp.SetPixel(x, y, Colors[(int)Cells[i++].Type]);
					}
				}
				fastBmp.UnlockImage();
			}

			return Image;
		}
            /// <summary>
            /// Decode caption from the input stream
            /// </summary>
            /// <returns>bitmap of the decoded caption</returns>
            public static Bitmap DecodeImage(PcsObject pcs, IList<OdsData> data, List<PaletteInfo> palettes)
            {
                if (pcs == null || data == null || data.Count == 0)
                    return new Bitmap(1, 1);

                int w = data[0].Size.Width;
                int h = data[0].Size.Height;

                var bm = new FastBitmap(new Bitmap(w, h));
                bm.LockImage();
                BluRaySupPalette pal = DecodePalette(palettes);

                int index = 0;
                int ofs = 0;
                int xpos = 0;

                byte[] buf = data[0].Fragment.ImageBuffer;
                index = 0;
                do
                {
                    int b = buf[index++] & 0xff;
                    if (b == 0 && index < buf.Length)
                    {
                        b = buf[index++] & 0xff;
                        if (b == 0)
                        {
                            // next line
                            ofs = (ofs / w) * w;
                            if (xpos < w)
                                ofs += w;
                            xpos = 0;
                        }
                        else
                        {
                            int size;
                            if ((b & 0xC0) == 0x40)
                            {
                                if (index < buf.Length)
                                {
                                    // 00 4x xx -> xxx zeroes
                                    size = ((b - 0x40) << 8) + (buf[index++] & 0xff);
                                    Color c = Color.FromArgb(pal.GetArgb(0));
                                    for (int i = 0; i < size; i++)
                                        PutPixel(bm, ofs++, c);
                                    xpos += size;
                                }
                            }
                            else if ((b & 0xC0) == 0x80)
                            {
                                if (index < buf.Length)
                                {
                                    // 00 8x yy -> x times value y
                                    size = (b - 0x80);
                                    b = buf[index++] & 0xff;
                                    Color c = Color.FromArgb(pal.GetArgb(b));
                                    for (int i = 0; i < size; i++)
                                        PutPixel(bm, ofs++, c);
                                    xpos += size;
                                }
                            }
                            else if ((b & 0xC0) != 0)
                            {
                                if (index < buf.Length)
                                {
                                    // 00 cx yy zz -> xyy times value z
                                    size = ((b - 0xC0) << 8) + (buf[index++] & 0xff);
                                    b = buf[index++] & 0xff;
                                    Color c = Color.FromArgb(pal.GetArgb(b));
                                    for (int i = 0; i < size; i++)
                                        PutPixel(bm, ofs++, c);
                                    xpos += size;
                                }
                            }
                            else
                            {
                                // 00 xx -> xx times 0
                                Color c = Color.FromArgb(pal.GetArgb(0));
                                for (int i = 0; i < b; i++)
                                    PutPixel(bm, ofs++, c);
                                xpos += b;
                            }
                        }
                    }
                    else
                    {
                        PutPixel(bm, ofs++, b, pal);
                        xpos++;
                    }
                } while (index < buf.Length);

                bm.UnlockImage();
                return bm.GetBitmap();
            }
        private static Bitmap CropBitmapAndUnlok(FastBitmap bmp, Color backgroundColor)
        {
            int y = 0;
            int x;
            Color c = backgroundColor;
            int backgroundArgb = backgroundColor.ToArgb();

            // Crop top
            while (y < bmp.Height && IsBackgroundColor(c, backgroundArgb))
            {
                c = bmp.GetPixel(0, y);
                if (IsBackgroundColor(c, backgroundArgb))
                {
                    for (x = 1; x < bmp.Width; x++)
                    {
                        c = bmp.GetPixelNext();
                        if (c.A != 0 && c.ToArgb() != backgroundArgb)
                            break;
                    }
                }
                if (IsBackgroundColor(c, backgroundArgb))
                    y++;
            }
            int minY = y;
            if (minY > 3)
                minY -= 3;
            else
                minY = 0;

            // Crop left
            x = 0;
            c = backgroundColor;
            while (x < bmp.Width && IsBackgroundColor(c, backgroundArgb))
            {
                for (y = minY; y < bmp.Height; y++)
                {
                    c = bmp.GetPixel(x, y);
                    if (!IsBackgroundColor(c, backgroundArgb))
                        break;
                }
                if (IsBackgroundColor(c, backgroundArgb))
                    x++;
            }
            int minX = x;
            if (minX > 3)
                minX -= 3;
            else
                minX -= 0;

            // Crop bottom
            y = bmp.Height - 1;
            c = backgroundColor;
            while (y > minY && IsBackgroundColor(c, backgroundArgb))
            {
                c = bmp.GetPixel(0, y);
                if (IsBackgroundColor(c, backgroundArgb))
                {
                    for (x = 1; x < bmp.Width; x++)
                    {
                        c = bmp.GetPixelNext();
                        if (!IsBackgroundColor(c, backgroundArgb))
                            break;
                    }
                }
                if (IsBackgroundColor(c, backgroundArgb))
                    y--;
            }
            int maxY = y + 7;
            if (maxY >= bmp.Height)
                maxY = bmp.Height - 1;

            // Crop right
            x = bmp.Width - 1;
            c = backgroundColor;
            while (x > minX && IsBackgroundColor(c, backgroundArgb))
            {
                for (y = minY; y < bmp.Height; y++)
                {
                    c = bmp.GetPixel(x, y);
                    if (!IsBackgroundColor(c, backgroundArgb))
                        break;
                }
                if (IsBackgroundColor(c, backgroundArgb))
                    x--;
            }
            int maxX = x + 7;
            if (maxX >= bmp.Width)
                maxX = bmp.Width - 1;

            bmp.UnlockImage();
            Bitmap bmpImage = bmp.GetBitmap();
            if (bmpImage.Width > 1 && bmpImage.Height > 1 && maxX - minX > 0 && maxY - minY > 0)
            {
                Bitmap bmpCrop = bmpImage.Clone(new Rectangle(minX, minY, maxX - minX, maxY - minY), bmpImage.PixelFormat);
                return bmpCrop;
            }
            return (Bitmap)bmpImage.Clone();
        }
        public void DecodeImage(byte[] buffer, int index, ClutDefinitionSegment cds)
        {
            switch (ObjectCodingMethod)
            {
                case 0:
                    var twoToFourBitColorLookup = new List<int> { 0, 1, 2, 3 };
                    var twoToEightBitColorLookup = new List<int> { 0, 1, 2, 3 };
                    var fourToEightBitColorLookup = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };

                    int pixelCode = 0;
                    int runLength = 0;
                    int dataType = buffer[index + 7];
                    int length = TopFieldDataBlockLength;

                    index += 8;
                    int start = index;
                    int x = 0;
                    int y = 0;

                    // Pre-decoding to determine image size
                    int width = 0;
                    while (index < start + TopFieldDataBlockLength)
                    {
                        index = CalculateSize(buffer, index, ref dataType, start, ref x, ref y, length, ref runLength, ref width);
                    }

                    if (width > 2000)
                    {
                        width = 2000;
                    }

                    if (y > 500)
                    {
                        y = 500;
                    }

                    Image = new Bitmap(width, y + 1);
                    fastImage = new FastBitmap(Image);
                    fastImage.LockImage();

                    x = 0;
                    y = 0;
                    index = start;
                    while (index < start + TopFieldDataBlockLength)
                    {
                        index = ProcessDataType(buffer, index, cds, ref dataType, start, twoToFourBitColorLookup, fourToEightBitColorLookup, twoToEightBitColorLookup, ref x, ref y, length, ref pixelCode, ref runLength);
                    }

                    x = 0;
                    y = 1;
                    if (BottomFieldDataBlockLength == 0)
                    {
                        index = start;
                    }
                    else
                    {
                        length = BottomFieldDataBlockLength;
                        index = start + TopFieldDataBlockLength;
                        start = index;
                    }

                    dataType = buffer[index - 1];
                    while (index < start + BottomFieldDataBlockLength - 1)
                    {
                        index = ProcessDataType(buffer, index, cds, ref dataType, start, twoToFourBitColorLookup, fourToEightBitColorLookup, twoToEightBitColorLookup, ref x, ref y, length, ref pixelCode, ref runLength);
                    }

                    fastImage.UnlockImage();
                    break;
                case 1:
                    Image = new Bitmap(1, 1);
                    NumberOfCodes = buffer[index + 3];
                    break;
            }
        }
Exemple #6
0
		public Bitmap GetImageTransparentRgba(int index) {
			if (ImagesRgba == null || ImagesRgba.Count <= index) {
				return null;
			}
			if (IsDrawnRgba(index) == false) {
				DrawRgbaImage(index);
			}

			Bitmap img = ImagesRgba[index].Image.Clone() as Bitmap;
			Color bg = Color.Fuchsia;
			FastBitmap fb = new FastBitmap(img);

			fb.LockImage();
			for (int x = 0; x < img.Width; x++) {
				for (int y = 0; y < img.Height; y++) {
					if (fb.GetPixel(x, y) == bg) {
						fb.SetPixel(x, y, Color.Transparent);
					}
				}
			}
			fb.UnlockImage();

			return img;
		}
Exemple #7
0
		public Bitmap GetImageTransparentPal(int index) {
			if (ImagesPal == null || ImagesPal.Count <= index) {
				return null;
			}
			if (IsDrawnPal(index) == false) {
				if (DrawPalImage(index) == false) {
					// TODO: Exception?
					return null;
				}
			}

			Bitmap img = ImagesPal[index].Image.Clone() as Bitmap;
			Color bg = Palette[0];
			FastBitmap fb = new FastBitmap(img);

			fb.LockImage();
			for (int x = 0; x < img.Width; x++) {
				for (int y = 0; y < img.Height; y++) {
					if (fb.GetPixel(x, y) == bg) {
						fb.SetPixel(x, y, Color.Transparent);
					}
				}
			}
			fb.UnlockImage();

			return img;
		}
Exemple #8
0
		public bool DrawRgbaImage(int imageIndex) {
			if (ImagesRgba == null || ImagesRgba.Count <= imageIndex) {
				return false;
			}
			if (imageIndex < 0 || imageIndex >= ImagesRgba.Count) {
				return false;
			}

			RoSpriteImageRgba sprImg = ImagesRgba[imageIndex];
			if (sprImg == null || sprImg.Data == null || sprImg.Data.Length == 0 || sprImg.Width < 1 || sprImg.Height < 1) {
				return false;
			}

			sprImg.Image = new Bitmap(sprImg.Width, sprImg.Height);
			FastBitmap fb = new FastBitmap(sprImg.Image);

			int index = 0, alpha = 0, red = 0, green = 0, blue = 0;
			Color col;
			fb.LockImage();
			for (int y = 0; y < sprImg.Height; y++) {
				for (int x = 0; x < sprImg.Width; x++, index += 4) {
					// A B G R
					alpha = sprImg.Data[index];
					blue = sprImg.Data[index + 1];
					green = sprImg.Data[index + 2];
					red = sprImg.Data[index + 3];
					col = Color.FromArgb(alpha, red, green, blue);
					fb.SetPixel(x, y, col);
				}
			}
			fb.UnlockImage();

			return true;
		}
Exemple #9
0
		public bool DrawPalImage(int imageIndex) {
			if (ImagesPal == null || ImagesPal.Count <= imageIndex) {
				return false;
			}
			if (imageIndex < 0 || imageIndex >= ImagesPal.Count) {
				return false;
			}

			RoSpriteImagePal sprImg = ImagesPal[imageIndex];
			if (Version >= 0x201 && sprImg.Decoded == false) {
				sprImg.Data = RLE.Decode(sprImg.Data);
				sprImg.Decoded = true;
			}
			if (sprImg.Data == null || sprImg.Data.Length == 0 || sprImg.Width < 1 || sprImg.Height < 1) {
				return false;
			}

			sprImg.Image = new Bitmap(sprImg.Width, sprImg.Height);
			FastBitmap fb = new FastBitmap(sprImg.Image);

			int index;
			fb.LockImage();
			for (int x = 0; x < sprImg.Width; x++) {
				for (int y = 0; y < sprImg.Height; y++) {
					index = (x + (y * sprImg.Width));
					if (index >= sprImg.Data.Length) {
						fb.SetPixel(x, y, Color.Transparent);
						continue;
					}
					fb.SetPixel(x, y, Palette[sprImg.Data[index]]);
				}
			}
			fb.UnlockImage();

			return true;
		}
Exemple #10
0
        public Bitmap GetImageTransparentRgba(int index)
        {
            if (ImagesRgba == null || ImagesRgba.Count <= index) {
                return null;
            }
            if (IsDrawnRgba(index) == false) {
                DrawRgbaImage(index);
            }

            var img = ImagesRgba[index].Image.Clone() as Bitmap;
            if (img == null) {
                throw new Exception("Invalid rgba image on index #" + index);
            }
            var bg = Color.Fuchsia;
            var fb = new FastBitmap(img);

            fb.LockImage();
            for (var x = 0; x < img.Width; x++) {
                for (var y = 0; y < img.Height; y++) {
                    if (fb.GetPixel(x, y) == bg) {
                        fb.SetPixel(x, y, Color.Transparent);
                    }
                }
            }
            fb.UnlockImage();

            return img;
        }
Exemple #11
0
        public Bitmap GetImageTransparentPal(int index)
        {
            if (ImagesPal == null || ImagesPal.Count <= index) {
                return null;
            }
            if (IsDrawnPal(index) == false) {
                if (DrawPalImage(index) == false) {
                    throw new Exception("Failed to draw pal-image on index #" + index);
                }
            }

            var img = ImagesPal[index].Image.Clone() as Bitmap;
            if (img == null) {
                throw new Exception("Invalid pal image on index #" + index);
            }
            var bg = Palette[0];
            var fb = new FastBitmap(img);

            fb.LockImage();
            for (var x = 0; x < img.Width; x++) {
                for (var y = 0; y < img.Height; y++) {
                    if (fb.GetPixel(x, y) == bg) {
                        fb.SetPixel(x, y, Color.Transparent);
                    }
                }
            }
            fb.UnlockImage();

            return img;
        }