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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package tga

import (
        "bytes"
        //"code.google.com/p/webp"
        "encoding/binary"
        "image"
        "image/draw"
        _ "image/gif"
        _ "image/jpeg"
        _ "image/png"
        "os"
)

//data type
const ( // I = image, U = uncompressed, CM= ColorMap, RGB = RedGreenBlue
        // RL= Run Length Algo. D=Delta algo, H=Huffman algo, 4PQ = 4-pass quadtree
        nONE       byte = 0
        uCMI       byte = 1
        uRGBI      byte = 2
        uBWI       byte = 3
        rLCMI      byte = 9
        rLRGBI     byte = 10
        cBWI       byte = 11
        cCMHDRL    byte = 32
        cCMHDRL4PQ byte = 33
)

func Encode(fn string, img image.Image) *bytes.Buffer {
        var buf bytes.Buffer
        b := img.Bounds()
        idField := make([]byte, len(fn))
        copy(idField, fn)
        width := make([]byte, 2)
        height := make([]byte, 2)
        binary.LittleEndian.PutUint16(width, uint16(b.Dx()))
        binary.LittleEndian.PutUint16(height, uint16(b.Dy()))

        /*
                Header for the TGA 1 spec.
                More information can be found at http://local.wasp.uwa.edu.au/~pbourke/dataformats/tga/
        */
        header := make([]byte, 18)
        header[0] = byte(len(fn)) //Length of file descriptor field
        header[1] = 0             //color map mode
        header[2] = uRGBI         //datatype. Look above for more options.
        header[3] = 0             //colormap origin
        header[4] = 0
        header[5] = 0 //colormap length
        header[7] = 0 //colormap depth
        header[8] = 0 //x origin. 0 denotes lower left origin
        header[9] = 0
        header[10] = 0 //y origin. 0 denotes lower left origin
        header[11] = 0
        header[12] = width[0]  // image width in pixels. Little Edian low bit
        header[13] = width[1]  // Width High bit.
        header[14] = height[0] //image height in pixels.  Little Edian low bit
        header[15] = height[1] //Height High bit.
        header[16] = 32        //bits per pixel. 32 for full color plus alpha.
        header[17] = 8         // 0-3 bit is 1 - 8 attribute bits per pixel (alpha), bit 4 is reserved, 
        //bit 5 sets screen origin.  bit 6 and 7 data storage interleaving flag. 

        rgbaImg, ok := img.(*image.NRGBA)
        if !ok {
                println("Image must be NRGBA")
                return nil
        }

        buf.Write(header)
        buf.Write(idField)

        data := make([]byte, b.Dx()*b.Dy()*4)
        lineLen := b.Dx() * 4
        dest := len(data) - lineLen
        for src := 0; src < len(rgbaImg.Pix); {
                copy(data[dest:dest+lineLen], rgbaImg.Pix[src:src+rgbaImg.Stride])
                dest -= lineLen
                src += rgbaImg.Stride
        }
        for x := 0; x < len(data); {

                buf.WriteByte(data[x+2])
                buf.WriteByte(data[x+1])
                buf.WriteByte(data[x+0])
                buf.WriteByte(data[x+3])
                x += 4

        }
        return &buf

}

func Create(fn string) (data *bytes.Buffer, err error) {
        f, err := os.Open(fn)
        if err != nil {
                return
        }
        defer f.Close()

        img, format, err := image.Decode(f)
        if err != nil {
                return
        }
        if format != "png" {
                b := img.Bounds()
                m := image.NewNRGBA(b)
                draw.Draw(m, b, img, b.Min, draw.Src)
                data = Encode(fn, m)
        } else {
                data = Encode(fn, img)
        }
        return
}

/*func WriteToFile(tga []byte, fn string, overwrite bool) error {
        fp, _ := os.Getwd()
        err := os.Chdir(fp + "tga_img")
        if err != nil {
                NoDir := os.IsNotExist(err)
                println("tga_img doesnt exist: ", NoDir)
                if NoDir {
                        err := os.Mkdir("tga_img", os.ModePerm)
                        if err != nil {
                                return err
                        }
                        err = os.Chdir(fp + "/tga_img")
                        if err != nil {
                                return err
                        }
                } else {
                        return err
                }
        }
        file, err := os.Create(fn + ".tga")
        if err != nil {
                exists := os.IsExist(err)
                if exists {
                        if !overwrite {
                                err = os.Rename(fn+".tga", fn+time.Now().String()+".tga")
                                if err != nil {
                                        return err
                                }
                        } else {
                                err = os.Remove(fn + ".tga")
                                if err != nil {
                                        return err
                                }
                        }
                        file, err = os.Create(fn + ".tga")

                }
        }
        n, err := file.Write(tga)
        if err != nil {
                return err
        }
        println("tga written to disk: wrote: ", n, " bytes")
        return nil

}*/
Hide details
Change log
9d0a6c4bbb9e by mordeus <mordeus@gocos2d.org> on Jul 13, 2012   Diff
mend
Go to: 	
Double click a line to add a comment
Older revisions
 f050a8f6d61e by mordeus <mordeus@gocos2d.org> on Jul 13, 2012   Diff 
 85e39bde85b7 by mordeus <mordeus@gocos2d.org> on Jul 13, 2012   Diff 
All revisions of this file
File info
Size: 3718 bytes, 160 lines
View raw file