File: csv2bmp.cs

package info (click to toggle)
cadencii 3.3.9%2Bsvn20110818.r1732-5
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 35,880 kB
  • sloc: cs: 160,836; java: 42,449; cpp: 7,605; ansic: 1,728; perl: 1,087; makefile: 236; php: 142; xml: 117; sh: 21
file content (121 lines) | stat: -rw-r--r-- 4,010 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections.Generic;

class csv2bmp{
    public static void Main( string[] args )
    {
        byte[,] r = null;
        byte[,] g = null;
        byte[,] b = null;
        byte[,] a = null;
        string current = "";
        string file = "";
        foreach( string s in args ){
            if( s.StartsWith( "-" ) ){
                current = s;
            }else{
                if( current == "-a" ){
                    a = load_csv( s );
                }else if( current == "-r" ){
                    r = load_csv( s );
                }else if( current == "-g" ){
                    g = load_csv( s );
                }else if( current == "-b" ){
                    b = load_csv( s );
                }else if( current == "-o" ){
                    file = s;
                }
                current = "";
            }
        }
        if( file == "" ){
            Console.WriteLine( "error; output file not specified" );
            return;
        }
        int w = 0;
        int h = 0;
        int rx = (r != null) ? r.GetLength( 0 ) : 0;
        int ry = (r != null) ? r.GetLength( 1 ) : 0;
        int gx = (g != null) ? g.GetLength( 0 ) : 0;
        int gy = (g != null) ? g.GetLength( 1 ) : 0;
        int bx = (b != null) ? b.GetLength( 0 ) : 0;
        int by = (b != null) ? b.GetLength( 1 ) : 0;
        int ax = (a != null) ? a.GetLength( 0 ) : 0;
        int ay = (a != null) ? a.GetLength( 1 ) : 0;
        if( r != null ){
            w = Math.Max( w, rx );
            h = Math.Max( h, ry );
        }
        if( g != null ){
            w = Math.Max( w, gx );
            h = Math.Max( h, gy );
        }
        if( b != null ){
            w = Math.Max( w, bx );
            h = Math.Max( h, by );
        }
        if( a != null ){
            w = Math.Max( w, ax );
            h = Math.Max( h, ay );
        }
        if( w <= 0 || h <= 0 ){
            Console.WriteLine( "error; width=" + w + "; height=" + h );
            return;
        }
        Bitmap bmp = new Bitmap( w, h, PixelFormat.Format32bppArgb );
        for( int j = 0; j < h; j++ ){
            for( int i = 0; i < w; i++ ){
                byte red = 0;
                if( r != null && i < rx && j < ry ){
                    red = r[i, j];
                }
                byte green = 0;
                if( g != null && i < gx && j < gy ){
                    green = g[i, j];
                }
                byte blue = 0;
                if( b != null && i < bx && j < by ){
                    blue = b[i, j];
                }
                byte alpha = 0;
                if( a != null && i < ax && j < ay ){
                    alpha = a[i, j];
                }
                Color c = Color.FromArgb( alpha, red, green, blue );
                bmp.SetPixel( i, j, c );
            }
        }
        bmp.Save( file, ImageFormat.Png );
    }

    public static byte[,] load_csv( string file )
    {
        int lines = 0;
        int max_colum = 0;
        List<byte[]> t = new List<byte[]>();
        using( StreamReader sr = new StreamReader( file ) ){
            string line = "";
            while( (line = sr.ReadLine()) != null ){
                string[] spl = line.Split( ',' );
                byte[] l = new byte[spl.Length];
                for( int i = 0; i < l.Length; i++ ){
                    l[i] = byte.Parse( spl[i] );
                }
                t.Add( l );
                max_colum = Math.Max( max_colum, spl.Length );
                lines++;
            }
        }
        byte[,] ret = new byte[max_colum, lines];
        for( int j = 0; j < lines; j++ ){
            byte[] src = t[j];
            for( int i = 0; i < src.Length; i++ ){
                ret[i, j] = src[i];
            }
        }
        return ret;
    }
}