Exemplo n.º 1
0
        private int[][] extractRaw(Bitmap src)
        {
            int width  = (src.Width + 7) / 8 * 8;
            int height = (src.Height + 7) / 8 * 8;

            // original image
            const int N = 8;

            int[][] buff1 = RectangularArrays.ReturnRectangularIntArray(height, width); // watermarked image
            int[][] buff2 = RectangularArrays.ReturnRectangularIntArray(height, width); // DCT watermarked image coefficients
            int[][] b1    = RectangularArrays.ReturnRectangularIntArray(N, N);          // DCT input
            int[][] b2    = RectangularArrays.ReturnRectangularIntArray(N, N);          // DCT output

            // watermark
            const int W = 4;

            int[][] water1 = RectangularArrays.ReturnRectangularIntArray(128, 128);     // extract watermark image
            int[][] water2 = RectangularArrays.ReturnRectangularIntArray(128, 128);     // DCT watermark image coefficients
            int[][] water3 = RectangularArrays.ReturnRectangularIntArray(128, 128);     // random watermark image
            int[][] w1     = RectangularArrays.ReturnRectangularIntArray(W, W);         // dequantization output
            int[][] w2     = RectangularArrays.ReturnRectangularIntArray(W, W);         // DCT input
            int[][] w3     = RectangularArrays.ReturnRectangularIntArray(W, W);         // DCT output

            // random process
            int a, b, c, c1;

            int[] tmp  = new int[128 * 128];
            int[] tmp1 = new int[128 * 128];
            int   cc   = 0;

            // middle frequency coefficients
            // final int mfbuff1[] = new int[128 * 128];
            int[]   mfbuff1 = new int[width * height];
            int[][] mfbuff2 = RectangularArrays.ReturnRectangularIntArray(128, 128);             // 1 to 2

            // divide 8x8 block
            int k = 0, l = 0;

            // init buf1 from watermarked image src...
            for (int y = 0; y < src.Height; y++)
            {
                for (int x = 0; x < src.Width; x++)
                {
                    if (_stopped)
                    {
                        return(null);
                    }
                    Color color = new Color();
                    color = src.GetPixel(x, y);
                    double[] hsb = RGBtoHSB(color);
                    // use brightness of the pixel...
                    buff1[y][x] = (int)(hsb[2] * 255.0);
                }
            }

            for (int y = 0; y < height; y += N)
            {
                for (int x = 0; x < width; x += N)
                {
                    for (int i = y; i < y + N; i++)
                    {
                        for (int j = x; j < x + N; j++)
                        {
                            b1[k][l] = buff1[i][j];
                            l++;
                        }
                        l = 0;
                        k++;
                    }
                    k = 0;

                    DCT o1 = new DCT();
                    o1.ForwardDCT(b1, b2);

                    for (int p = y; p < y + N; p++)
                    {
                        for (int q = x; q < x + N; q++)
                        {
                            buff2[p][q] = b2[k][l];
                            l++;
                        }
                        l = 0;
                        k++;
                    }
                    k = 0;
                }
            }

            for (int i = 0; i < height; i += N)
            {
                for (int j = 0; j < width; j += N)
                {
                    mfbuff1[cc] = buff2[i + 1][j + 4];
                    cc++;
                    mfbuff1[cc] = buff2[i + 2][j + 3];
                    cc++;
                    mfbuff1[cc] = buff2[i + 3][j + 2];
                    cc++;
                    mfbuff1[cc] = buff2[i + 4][j + 1];
                    cc++;
                }
            }
            cc = 0;

            ZigZag scan = new ZigZag();

            scan.one2two(mfbuff1, mfbuff2);

            // random extracting
            Random r1 = new Random((int)randomizeEmbeddingSeed);

            for (int i = 0; i < 128; i++)
            {
                for (int j = 0; j < 128; j++)
                {
                    while (true)
                    {
                        c1 = r1.Next(128 * 128);
                        if (tmp1[c1] == 0)
                        {
                            break;
                        }
                    }
                    a            = c1 / 128;
                    b            = c1 % 128;
                    water1[a][b] = mfbuff2[i][j];
                    tmp1[c1]     = 1;
                }
            }

            k = 0;
            l = 0;
            for (int y = 0; y < 128; y += W)
            {
                for (int x = 0; x < 128; x += W)
                {
                    for (int i = y; i < y + W; i++)
                    {
                        for (int j = x; j < x + W; j++)
                        {
                            w1[k][l] = water1[i][j];
                            l++;
                        }
                        l = 0;
                        k++;
                    }
                    k = 0;

                    Qt qw2 = new Qt();
                    qw2.WaterDeQt(w1, w2);

                    DCT wm2 = new DCT(4);
                    wm2.InverseDCT(w2, w3);
                    for (int p = y; p < y + W; p++)
                    {
                        for (int q = x; q < x + W; q++)
                        {
                            water2[p][q] = w3[k][l];
                            l++;
                        }
                        l = 0;
                        k++;
                    }
                    k = 0;
                }
            }

            Random r = new Random((int)randomizeWatermarkSeed);

            for (int i = 0; i < 128; i++)
            {
                for (int j = 0; j < 128; j++)
                {
                    while (true)
                    {
                        c = r.Next(128 * 128);
                        if (tmp[c] == 0)
                        {
                            break;
                        }
                    }
                    a            = c / 128;
                    b            = c % 128;
                    water3[a][b] = water2[i][j];
                    tmp[c]       = 1;
                }
            }

            return(water3);
        }
Exemplo n.º 2
0
        private int[][] embed(Bitmap src, int[][] water1)
        {
            int width  = (src.Width + 7) / 8 * 8;
            int height = (src.Height + 7) / 8 * 8;

            // original image process
            const int N = 8;

            int[][] buff1 = RectangularArrays.ReturnRectangularIntArray(height, width); // Original image
            int[][] buff2 = RectangularArrays.ReturnRectangularIntArray(height, width); // DCT Original image coefficients
            int[][] buff3 = RectangularArrays.ReturnRectangularIntArray(height, width); // IDCT Original image coefficients
            int[][] b1    = RectangularArrays.ReturnRectangularIntArray(N, N);          // DCT input
            int[][] b2    = RectangularArrays.ReturnRectangularIntArray(N, N);          // DCT output
            int[][] b3    = RectangularArrays.ReturnRectangularIntArray(N, N);          // IDCT input
            int[][] b4    = RectangularArrays.ReturnRectangularIntArray(N, N);          // IDCT output
            // watermark image process
            const int W = 4;

            int[][] water2  = RectangularArrays.ReturnRectangularIntArray(128, 128); // random watermark image
            int[][] water3  = RectangularArrays.ReturnRectangularIntArray(128, 128); // DCT watermark image coefficients
            int[][] w1      = RectangularArrays.ReturnRectangularIntArray(W, W);     // DCT input
            int[][] w2      = RectangularArrays.ReturnRectangularIntArray(W, W);     // DCT output
            int[][] w3      = RectangularArrays.ReturnRectangularIntArray(W, W);     // quantization output
            int[][] mfbuff1 = RectangularArrays.ReturnRectangularIntArray(128, 128); // embed coefficients
            int[]   mfbuff2 = new int[width * height];                               // 2 to 1
            // random process...
            int a, b, c;

            int[] tmp = new int[128 * 128];
            // random embed...
            int c1;
            int cc = 0;

            int[] tmp1 = new int[128 * 128];
            // divide 8x8 block...
            int k = 0, l = 0;

            // init buf1 from src image...
            for (int y = 0; y < src.Height; y++)
            {
                for (int x = 0; x < src.Width; x++)
                {
                    if (_stopped)
                    {
                        return(null);
                    }
                    Color color = new Color();
                    color = src.GetPixel(x, y);
                    double[] hsb = RGBtoHSB(color);
                    // use brightness of the pixel...
                    buff1[y][x] = (int)(hsb[2] * 255.0);
                }
            }
            for (int y = 0; y < height; y += N)
            {
                for (int x = 0; x < width; x += N)
                {
                    for (int i = y; i < y + N; i++)
                    {
                        for (int j = x; j < x + N; j++)
                        {
                            b1[k][l] = buff1[i][j];
                            l++;
                        }
                        l = 0;
                        k++;
                    }
                    k = 0;
                    DCT o1 = new DCT();
                    o1.ForwardDCT(b1, b2);

                    for (int p = y; p < y + N; p++)
                    {
                        for (int q = x; q < x + N; q++)
                        {
                            buff2[p][q] = b2[k][l];
                            l++;
                        }
                        l = 0;
                        k++;
                    }
                    k = 0;
                }
            }
            Random r = new Random((int)randomizeWatermarkSeed);

            for (int i = 0; i < 128; i++)
            {
                for (int j = 0; j < 128; j++)
                {
                    while (true)
                    {
                        c = r.Next(128 * 128);
                        if (tmp[c] == 0)
                        {
                            break;
                        }
                    }
                    a            = c / 128;
                    b            = c % 128;
                    water2[i][j] = water1[a][b];
                    tmp[c]       = 1;
                }
            }

            k = 0;
            l = 0;
            for (int y = 0; y < 128; y += W)
            {
                for (int x = 0; x < 128; x += W)
                {
                    for (int i = y; i < y + W; i++)
                    {
                        for (int j = x; j < x + W; j++)
                        {
                            w1[k][l] = water2[i][j];
                            l++;
                        }
                        l = 0;
                        k++;
                    }
                    k = 0;

                    DCT wm1 = new DCT(4);

                    wm1.ForwardDCT(w1, w2);

                    Qt qw1 = new Qt();
                    qw1.WaterQt(w2, w3);

                    for (int p = y; p < y + W; p++)
                    {
                        for (int q = x; q < x + W; q++)
                        {
                            water3[p][q] = w3[k][l];
                            l++;
                        }
                        l = 0;
                        k++;
                    }
                    k = 0;
                }
            }

            // Random Embedding
            Random r1 = new Random((int)randomizeEmbeddingSeed);

            for (int i = 0; i < 128; i++)
            {
                for (int j = 0; j < 128; j++)
                {
                    while (true)
                    {
                        c1 = r1.Next(128 * 128);
                        if (tmp1[c1] == 0)
                        {
                            break;
                        }
                    }
                    a             = c1 / 128;
                    b             = c1 % 128;
                    mfbuff1[i][j] = water3[a][b];
                    tmp1[c1]      = 1;
                }
            }

            ZigZag scan = new ZigZag();

            scan.two2one(mfbuff1, mfbuff2);

            // WriteBack coefficients
            for (int i = 0; i < height; i += N)
            {
                for (int j = 0; j < width; j += N)
                {
                    buff2[i + 1][j + 4] = mfbuff2[cc];
                    cc++;
                    buff2[i + 2][j + 3] = mfbuff2[cc];
                    cc++;
                    buff2[i + 3][j + 2] = mfbuff2[cc];
                    cc++;
                    buff2[i + 4][j + 1] = mfbuff2[cc];
                    cc++;
                }
            }
            cc = 0;
            k  = 0;
            l  = 0;
            for (int y = 0; y < height; y += N)
            {
                for (int x = 0; x < width; x += N)
                {
                    for (int i = y; i < y + N; i++)
                    {
                        for (int j = x; j < x + N; j++)
                        {
                            b3[k][l] = buff2[i][j];
                            l++;
                        }
                        l = 0;
                        k++;
                    }
                    k = 0;

                    DCT o2 = new DCT();
                    o2.InverseDCT(b3, b4);

                    for (int p = y; p < y + N; p++)
                    {
                        for (int q = x; q < x + N; q++)
                        {
                            buff3[p][q] = b4[k][l];
                            l++;
                        }
                        l = 0;
                        k++;
                    }
                    k = 0;
                }
            }

            return(buff3);
        }