get paid to paste

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace ConsoleApplication1 {
    class Program {
        MTRandom r = new MTRandom();

        static void Main(string[] args) {
            var i = new Bitmap(8192, 8192, PixelFormat.Format32bppArgb);

            TurboArgbPixelFilter.FastFiltering(i, proc);

            i.Save(@"C:\image.jpg", ImageFormat.Jpeg);
            // Query successful (02:15.943), image.jpg = 40,415,925 bytes.
        }

        private void proc(ref byte b, ref byte g, ref byte r, ref byte a) {
            b = r.NextByte();
            g = r.NextByte();
            r = r.NextByte();
            a = r.NextByte();
        }
    }

    class TurboArgbPixelFilter {
        /// <summary>
        /// 画像のすべてのピクセルをループして指定した処理をする。
        /// </summary>
        /// <param name="bmp">処理対象の画像。PixelFormat.Format32bppArgbのみ対応。</param>
        /// <param name="proc">ピクセルごとに行う処理。1ピクセルごとに呼び出される。</param>
        public static void FastFiltering(
            Bitmap bmp, PixelProc proc) {

            if(bmp.PixelFormat != PixelFormat.Format32bppArgb) {
                throw new ArgumentException(
                    "bmpのPixelFormatはFormat32bppArgbのみ有効です。", "bmp");
            }

            BitmapData bmpData = bmp.LockBits(
                new Rectangle(Point.Empty, bmp.Size),
                ImageLockMode.ReadWrite,
                PixelFormat.Format32bppArgb);

            unsafe {
                byte* pixel = (byte*)bmpData.Scan0;
                int dataLength = bmpData.Stride * bmpData.Height;

                for(int i = 0; i < dataLength; i += 4) {
                    proc(
                        ref *(pixel++),
                        ref *(pixel++),
                        ref *(pixel++),
                        ref *(pixel++));
                }
            }

            bmp.UnlockBits(bmpData);
        }

        /// <summary>
        /// ImageからPixelFormat32pbbArgbのBitmapを作成して返します。
        /// </summary>
        /// <returns>PixelFormat32pbbArgbのBitmap</returns>
        public static Bitmap GetRegularizedBitmap(Image img) {
            Bitmap bmp = new Bitmap(img.Width, img.Height, PixelFormat.Format32bppArgb);

            using(Graphics g = Graphics.FromImage(bmp)) {
                g.DrawImage(img, 0, 0, img.Width, img.Height);
            }

            return bmp;
        }

        public delegate void PixelProc(ref byte b, ref byte g, ref byte r, ref byte a);
    }
}

Pasted: Jul 26, 2010, 12:12:19 pm
Views: 15