Example #1
0
 public static IBitmap AddLayer(this IBitmap bitmap, Bytemap layer, int left = 0, int top = 0, bool dispose = false)
 {
     if (layer == null)
     {
         return(bitmap);
     }
     for (int yy = 0; yy < layer.Height; yy++)
     {
         if (top + yy >= bitmap.Height())
         {
             break;
         }
         if (bitmap.OutBoundY(top + yy))
         {
             continue;
         }
         for (int xx = 0; xx < layer.Width; xx++)
         {
             if (left + xx >= bitmap.Width())
             {
                 break;
             }
             if (layer[xx, yy] == 0 || bitmap.OutBoundX(left + xx))
             {
                 continue;
             }
             bitmap.Bitmap[left + xx, top + yy] = layer[xx, yy];
         }
     }
     if (dispose)
     {
         layer.Dispose();
     }
     return(bitmap);
 }
Example #2
0
            internal Texture(IntPtr renderer, IBitmap bitmap)
            {
                if (bitmap == null)
                {
                    // Do not load empty bitmap
                    _handle = IntPtr.Zero;
                    return;
                }

                Width  = bitmap.Width();
                Height = bitmap.Height();

                _renderer = renderer;
                _handle   = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ABGR8888, SDL_TextureAccess.SDL_TEXTUREACCESS_STREAMING, Width, Height);
                SDL_Rect rect = new SDL_Rect()
                {
                    X = 0, Y = 0, W = Width, H = Height
                };

                int[] palette  = PaletteArray(bitmap.Palette);
                bool  hasAlpha = bitmap.Palette.Entries.Any(x => x.A != 255);

                if (HasAlpha(bitmap.Palette))
                {
                    SDL_SetTextureBlendMode(_handle, SDL_BlendMode.SDL_BLENDMODE_BLEND);
                }
                if (SDL_LockTexture(_handle, ref rect, out IntPtr pixels, out int pitch) == 0)
                {
                    int[] src = bitmap.Bitmap.ToColourMap(palette);
                    Marshal.Copy(bitmap.Bitmap.ToColourMap(palette), 0, pixels, Width * Height);
                    SDL_UnlockTexture(_handle);
                }
            }
Example #3
0
        public static IBitmap DrawRectangle3D(this IBitmap bitmap, int left = 0, int top = 0, int width = -1, int height = -1, byte colourLight = 15, byte colourDark = 8)
        {
            if (width < 0)
            {
                width = bitmap.Width() - left;
            }
            if (height < 0)
            {
                height = bitmap.Height() - top;
            }
            int ww = (left + width - 1), hh = (top + height - 1);

            for (int yy = top; yy <= hh; yy++)
            {
                if (yy >= bitmap.Height())
                {
                    break;
                }
                if (bitmap.OutBoundY(yy))
                {
                    continue;
                }
                for (int xx = left; xx <= ww; xx++)
                {
                    if (xx >= bitmap.Width())
                    {
                        break;
                    }
                    if (bitmap.OutBoundX(xx))
                    {
                        continue;
                    }
                    if (yy == top || xx == ww)
                    {
                        bitmap.Bitmap[xx, yy] = colourDark;
                    }
                    else if (yy == hh || xx == left)
                    {
                        bitmap.Bitmap[xx, yy] = colourLight;
                    }
                }
            }
            return(bitmap);
        }
Example #4
0
        public static Bytemap MatchColours(this IBitmap input, Palette palette, int startIndex, int length)
        {
            Dictionary <int, int> matches = new Dictionary <int, int>();

            Colour[] pal = input.Palette.Entries.ToArray();
            Colour[] cmp = palette.Entries.ToArray();
            for (int i = 0; i < pal.Length; i++)
            {
                if (pal[i].A == 0)
                {
                    matches.Add(i, 0);
                    continue;
                }

                int entry = 0;
                int mx    = 768;
                for (int j = startIndex; j < cmp.Length && j < (startIndex + length); j++)
                {
                    int total = Math.Abs((int)pal[i].R - cmp[j].R) + Math.Abs((int)pal[i].G - cmp[j].G) + Math.Abs((int)pal[i].B - cmp[j].B);
                    if (total >= mx)
                    {
                        continue;
                    }
                    entry = j;
                    mx    = total;
                }
                matches.Add(i, entry);
            }

            Bytemap output = new Bytemap(input.Width(), input.Height());

            for (int yy = 0; yy < input.Height(); yy++)
            {
                for (int xx = 0; xx < input.Height(); xx++)
                {
                    output[xx, yy] = (byte)matches[input.Bitmap[xx, yy]];
                }
            }
            return(output);
        }
Example #5
0
 public static IBitmap Tile(this IBitmap bitmap, Bytemap layer, int left = 0, int top = 0, int width = -1, int height = -1)
 {
     if (layer == null)
     {
         return(bitmap);
     }
     if (width == -1)
     {
         width = bitmap.Width() - left;
     }
     if (height == -1)
     {
         height = bitmap.Height() - top;
     }
     for (int yy = 0; yy < height; yy++)
     {
         if (top + yy >= bitmap.Height())
         {
             break;
         }
         if (bitmap.OutBoundY(top + yy))
         {
             continue;
         }
         for (int xx = 0; xx < width; xx++)
         {
             if (left + xx >= bitmap.Width())
             {
                 break;
             }
             if (layer[xx % layer.Width, yy % layer.Height] == 0 || bitmap.OutBoundX(left + xx))
             {
                 continue;
             }
             bitmap.Bitmap[left + xx, top + yy] = layer[xx % layer.Width, yy % layer.Height];
         }
     }
     return(bitmap);
 }