Beispiel #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            if (selectedImage == null)
            {
                return;
            }

            DialogResult res = openFileDialog3.ShowDialog();

            if (res == DialogResult.OK)
            {
                Bitmap bitmap = new Bitmap(selectedImage.width, selectedImage.height);

                statusLabel.Text = "Resizing image and creating bitmap ...";

                using (Graphics g = Graphics.FromImage((Image)bitmap))
                {
                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

                    g.DrawImage(Image.FromFile(openFileDialog3.FileName), 0, 0, selectedImage.width, selectedImage.height);
                }

                MemoryStream stream = new MemoryStream();
                CBitWriter   bw     = new CBitWriter();

                switch (selectedImage.flags & 0xFF)
                {
                case 2:
                    if ((selectedImage.depth & 0x4) != 0)
                    {
                        statusLabel.Text = "Encoding opaque image (might take a while) ...";
                        CR6Ti.WriteOpaque1(bitmap, bw, selectedImage.width, selectedImage.height);
                    }
                    else
                    {
                        throw new NotSupportedException("Writing Opaque2 images is not supported");
                    }
                    break;

                case 3:
                    statusLabel.Text = "Encoding transparent image (might take a while) ...";
                    CR6Ti.WriteTransparent(bitmap, bw, selectedImage.width, selectedImage.height);
                    break;

                default:
                    throw new NotSupportedException("Unknown image type");
                }

                statusLabel.Text = "Saving in byte format ...";
                bw.CopyToStream(stream);
                stream.Flush();

                selectedImage.replacement = stream.ToArray();

                selectedImage.replacementLen = (UInt32)stream.Length;

                checkBoxReplaced.Checked = true;
                button2.Enabled          = true;

                stream.Dispose();
                stream.Close();
                bitmap.Dispose();

                if ((selectedImage.flags & 0xFF) == 2 || (selectedImage.flags & 0xFF) == 3)
                {
                    Bitmap bm = new Bitmap(selectedImage.width, selectedImage.height);

                    CBitReader cr = new CBitReader(selectedImage.replacement);

                    switch (selectedImage.flags & 0xFF)
                    {
                    case 2:
                        CR6Ti.DecodeOpaque1(cr, bm, selectedImage.width, selectedImage.height, selectedImage.depth);
                        break;

                    case 3:
                        CR6Ti.DecodeTransparent(cr, bm, selectedImage.width, selectedImage.height, selectedImage.depth);
                        break;
                    }

                    pictureBox2.Image = bm;
                }
                else
                {
                    if (pictureBox2.Image != null)
                    {
                        pictureBox2.Image.Dispose();
                    }

                    pictureBox2.Image = null;
                }

                statusLabel.Text = "Ready";
            }
        }
Beispiel #2
0
        static private void WriteColorDiff2(CBitWriter writer, ref Color color, Color next, ref int dg, int alpha)
        {
            int r = color.R;
            int g = color.G;
            int b = color.B;
            int t = g + (dg * 2);

            if (t > 255)
            {
                t = 255;
            }
            else if (t < 0)
            {
                t = 0;
            }

            t -= g;

            if (t < -128)
            {
                t += 256;
            }
            if (t > 127)
            {
                t -= 256;
            }

            dg = (int)((next.G - color.G) / 2);

            if (dg < -128)
            {
                dg = -128;
            }
            if (dg > 127)
            {
                dg = 127;
            }

            r += (dg * 2);
            g += (dg * 2);
            b += (dg * 2);

            if (r > 255)
            {
                r = 255;
            }
            if (g > 255)
            {
                g = 255;
            }
            if (b > 255)
            {
                b = 255;
            }
            if (r < 0)
            {
                r = 0;
            }
            if (g < 0)
            {
                g = 0;
            }
            if (b < 0)
            {
                b = 0;
            }

            int dr2 = (next.R - r) / 2;
            int db2 = (next.B - b) / 2;

            if (dr2 < -128)
            {
                dr2 = -128;
            }
            if (dr2 > 127)
            {
                dr2 = 127;
            }
            if (db2 < -128)
            {
                db2 = -128;
            }
            if (db2 > 127)
            {
                db2 = 127;
            }

            r += (dr2 * 2);
            b += (db2 * 2);

            if (r > 255)
            {
                r = 255;
            }
            if (b > 255)
            {
                b = 255;
            }
            if (r < 0)
            {
                r = 0;
            }
            if (b < 0)
            {
                b = 0;
            }

            writer.Write((sbyte)(dg - (t / 2)));
            writer.Write((sbyte)db2);
            writer.Write((sbyte)dr2);

            color = Color.FromArgb(alpha, r & 0xFE, g & 0xFE, b & 0xFE);
        }
Beispiel #3
0
        /**
         * Transparent images
         */
        static public void WriteTransparent(Bitmap bitmap, CBitWriter writer, Int16 width, Int16 height)
        {
            Color[] widthBuffer = new Color[width];

            // Every row
            for (int y = 0; y < height; y++)
            {
                int    x               = 0;
                Color  color           = Color.FromArgb(0, 0, 0, 0);
                int    alphaRepetition = 0;
                int    ta              = 0;
                int    a               = 0;
                int    tempa           = 0;
                int    n               = 0;
                int    dg              = 0;
                int    frameRepetition = 0;
                bool   repetition      = true;
                UInt16 count           = 0;

                while (x < width)
                {
                    // ALPHA CHANNEL STUFF
                    if (--alphaRepetition < 0)
                    {
                        ta    = (int)Math.Round(bitmap.GetPixel(x, y).A * 0.125, 0);
                        ta   -= (int)Math.Round(color.A * 0.125, 0);
                        a     = FixAlpha(bitmap.GetPixel(x, y).A);
                        color = Color.FromArgb(a, color.R, color.G, color.B);

                        writer.Write((sbyte)ta);

                        if (a == 0)
                        {
                            n     = 0;
                            tempa = 0;

                            widthBuffer[x++] = Color.FromArgb(0, 0, 0, 0);

                            while (x < width)
                            {
                                tempa = FixAlpha(bitmap.GetPixel(x, y).A);

                                if (tempa != 0)
                                {
                                    break;
                                }

                                n++;
                                widthBuffer[x++] = Color.FromArgb(0, 0, 0, 0);
                            }

                            writer.Write((UInt16)n);

                            continue;
                        }

                        if (a == 255)
                        {
                            n     = 0;
                            tempa = 0;
                            int x2 = x + 1;

                            while (x2 < width)
                            {
                                tempa = FixAlpha(bitmap.GetPixel(x2, y).A);

                                if (tempa != 255)
                                {
                                    break;
                                }

                                n++;
                                x2++;
                            }

                            writer.Write((UInt16)n);
                            alphaRepetition = n;
                        }
                    }

                    // FRAME REPETITION
                    if (--frameRepetition < 0)
                    {
                        repetition = !repetition;
                        dg         = 0;

                        if (repetition)
                        {
                            writer.Write((UInt16)(count - 1));
                            frameRepetition = count - 1;
                        }
                        else
                        {
                            frameRepetition = FindNextRepetition2(bitmap, x, y, width, ref count);

                            writer.Write((UInt16)frameRepetition);
                        }
                    }

                    // MAIN PART
                    if (repetition)
                    {
                        if (alphaRepetition < frameRepetition)
                        {
                            widthBuffer[x++] = color;
                        }
                        // All repeated frames have the same alpha
                        else
                        {
                            if (frameRepetition > 0)
                            {
                                alphaRepetition -= frameRepetition;
                            }

                            for (int i = 0; i < (frameRepetition + 1); i++)
                            {
                                widthBuffer[x++] = color;
                            }

                            frameRepetition = 0;
                        }
                    }
                    else
                    {
                        // If this pixel is the same as the one in previous row - copy its RGBs
                        if (y > 0 && widthBuffer[x].R == bitmap.GetPixel(x, y).R&& widthBuffer[x].G == bitmap.GetPixel(x, y).G&& widthBuffer[x].B == bitmap.GetPixel(x, y).B)
                        {
                            writer.Write(true);
                            dg             = 0;
                            widthBuffer[x] = Color.FromArgb(color.A, widthBuffer[x].R, widthBuffer[x].G, widthBuffer[x].B);
                            color          = widthBuffer[x++];
                        }
                        // Otherwise write color difference and transform our current color
                        else
                        {
                            writer.Write(false);
                            WriteColorDiff2(writer, ref color, bitmap.GetPixel(x, y), ref dg, color.A);
                            widthBuffer[x++] = color;
                        }
                    }
                }
            }
        }
Beispiel #4
0
        /**
         * 1 opaque version
         */
        static public void WriteOpaque1(Bitmap bitmap, CBitWriter writer, Int16 width, Int16 height)
        {
            Color[] widthBuffer = new Color[width];

            // Every row
            for (int y = 0; y < height; y++)
            {
                // Clear stuff
                int    x          = 0;
                int    repetition = -1;
                UInt16 n          = 0;
                Color  color      = Color.FromArgb(0, 0, 0);
                int    dr         = 0;
                int    dg         = 0;
                int    db         = 0;

                // Every column
                while (x < width)
                {
                    // Find next duplicated pixels in this row (n is count of the pixels, repetition is relative location)
                    if (repetition == -1)
                    {
                        repetition = FindNextRepetition(bitmap, x, y, width, ref n);

                        writer.Write((UInt16)repetition);
                    }

                    // If this pixel is the same as the one in previous row - copy its RGBs
                    if (y > 0 && widthBuffer[x] == bitmap.GetPixel(x, y))
                    {
                        writer.Write(true);
                        dr    = dg = db = 0;
                        color = widthBuffer[x];
                    }
                    // Otherwise write color difference and transform our current color
                    else
                    {
                        writer.Write(false);
                        WriteColorDiff(writer, ref color, bitmap.GetPixel(x, y), ref dr, ref dg, ref db);
                    }

                    widthBuffer[x++] = color;

                    // Process repetition
                    if (repetition-- == 0 && x < width)
                    {
                        if (n == 0)
                        {
                            writer.Write((byte)0);

                            widthBuffer[x++] = color;
                        }
                        else
                        {
                            writer.Write((UInt16)(n - 1));

                            for (int i = 0; i < n; i++)
                            {
                                widthBuffer[x++] = color;
                            }
                        }

                        dr = dg = db = 0;
                    }
                }
            }
        }