UpdateGif.vb
''
'' This code is part of Document Solutions for Imaging demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.IO
Imports System.Drawing
Imports System.Collections.Generic
Imports System.Linq
Imports System.Numerics
Imports GrapeCity.Documents.Drawing
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Imaging

'' This sample loads an existing GIF and modifies its frames.
'' Specifically, it applies a color matrix to change the colors,
'' and flips the images horizontally.
'' The GIF loaded in this sample is the one produced by IndexedGif.
Public Class UpdateGif
    Function GenerateImageStream(
                ByVal targetMime As String,
                ByVal pixelSize As Size,
                ByVal dpi As Single,
                ByVal opaque As Boolean,
                Optional sampleParams As String() = Nothing) As Stream

        If Not targetMime = MimeTypes.GIF Then
            Throw New Exception("This sample only supports GIF output format.")
        End If

        '' Matrix to change colors to greenish blues:
        Dim colorMatrix = New ColorMatrix5x4() With
            {
                .M11 = 0.2F,
                .M12 = 0.3F,
                .M22 = 0.5F
            }

        Dim ms = New MemoryStream()
        '' Read source GIF, write modified one (change palette And flip And rotate frames)
        Using gr = New GcGifReader(Path.Combine("Resources", "Gifs", "goldfish-indexed.gif")), gw = New GcGifWriter(ms)

            Dim pal = gr.GetGlobalPalette()
            '' This sample will only work with GIFs that have a global palette
            If pal Is Nothing Then
                Throw New Exception("Source GIF does not have a global palette.")
            End If
            '' Use color matrix to update the palette
            Using palBmp = New GcBitmap(pal, pal.Length, 1, True)
                palBmp.ApplyColorMatrix(colorMatrix)
            End Using

            '' Set target paletter And other properties
            gw.GlobalPalette = pal
            gw.LogicalScreenWidth = gr.LogicalScreenWidth
            gw.LogicalScreenHeight = gr.LogicalScreenHeight
            gw.PixelAspectRatio = gr.PixelAspectRatio
            gw.AllowAddingTransparentColor = False
            Using tbmp = New GcBitmap()
                For i = 0 To gr.Frames.Count - 1
                    Dim frame = gr.Frames(i)
                    frame.ToGcBitmap(tbmp, i - 1)

                    '' Flip the image horizontally (this will reverse the 'rotation' of the fish):
                    Using bmp = tbmp.FlipRotate(FlipRotateAction.FlipHorizontal)
                        '' Apply the color matrix And append the frame to the target GIF:
                        bmp.ApplyColorMatrix(colorMatrix)
                        gw.AppendFrame(bmp, pal, DitheringMethod.NoDithering, 0, 0, GifDisposalMethod.DoNotDispose, frame.DelayTime, False)
                    End Using
                Next
            End Using
        End Using
        ms.Seek(0, SeekOrigin.Begin)
        Return ms
    End Function

    Public ReadOnly Property DefaultMime() As String
        Get
            Return MimeTypes.GIF
        End Get
    End Property
End Class