File: image2src.cs

package info (click to toggle)
cadencii 3.3.9%2Bsvn20110818.r1732-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 35,940 kB
  • sloc: cs: 160,836; java: 42,449; javascript: 7,966; cpp: 7,605; ansic: 1,728; perl: 1,087; makefile: 234; php: 142; xml: 117; sh: 21
file content (60 lines) | stat: -rw-r--r-- 2,112 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
using System;
using System.IO;

class image2src
{

    public static void Main( string[] args )
    {
        if ( args.Length <= 0 ) {
            Console.WriteLine( "error; input file was not specified" );
            return;
        }

        string file = args[0];
        if ( !File.Exists( file ) ) {
            Console.WriteLine( "error; file not found" );
            return;
        }

        string name = Path.GetFileNameWithoutExtension( file );
        Console.WriteLine( "Image " + name + "()" );
        Console.WriteLine( "{" );
        Console.WriteLine( "    try{" );
        Console.Write( "        return ImageIO.read( new ByteArrayInputStream( new byte[]{" );
        using ( FileStream fs = new FileStream( file, FileMode.Open, FileAccess.Read ) ) {
            const int BUFLEN = 1024;
            const int COLUMN = 10;
            byte[] buf = new byte[BUFLEN];
            long length = fs.Length;
            long remain = length;
            int count = COLUMN - 1;
            while ( remain > 0 ) {
                int amount = (remain > BUFLEN) ? BUFLEN : (int)remain;
                fs.Read( buf, 0, amount );
                for ( int i = 0; i < amount; i++ ) {
                    string s = "(byte)" + buf[i] + ",";
                    count++;
                    if( count != COLUMN - 1 ){
                        for ( int j = s.Length; j < "(byte)255, ".Length; j++ ) {
                            s += " ";
                        }
                    }
                    if ( count == COLUMN ) {
                        Console.WriteLine();
                        Console.Write( "            " );
                        count = 0;
                    }
                    Console.Write( s );
                }
                remain -= amount;
            }
        }
        Console.WriteLine( "} ) );" );
        Console.WriteLine( "    }catch( Exception ex ){" );
        Console.WriteLine( "        return null;" );
        Console.WriteLine( "    }" );
        Console.WriteLine( "}" );
    }

}