pic.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "strings"
  6. )
  7. // PIC defines the image pane in a brlyt
  8. type PIC struct {
  9. Flag uint8
  10. Origin uint8
  11. Alpha uint8
  12. _ uint8
  13. PaneName [16]byte
  14. UserData [8]byte
  15. XTranslation float32
  16. YTranslation float32
  17. ZTranslation float32
  18. XRotate float32
  19. YRotate float32
  20. ZRotate float32
  21. XScale float32
  22. YScale float32
  23. Width float32
  24. Height float32
  25. TopLeftColor [4]uint8
  26. TopRightColor [4]uint8
  27. BottomLeftColor [4]uint8
  28. BottomRightColor [4]uint8
  29. MatIndex uint16
  30. NumOfUVSets uint8
  31. _ uint8
  32. }
  33. type UVSet struct {
  34. TopLeftS float32
  35. TopLeftT float32
  36. TopRightS float32
  37. TopRightT float32
  38. BottomLeftS float32
  39. BottomLeftT float32
  40. BottomRightS float32
  41. BottomRightT float32
  42. }
  43. func (r *Root) ParsePIC(data []byte) {
  44. var pic PIC
  45. err := binary.Read(bytes.NewReader(data), binary.BigEndian, &pic)
  46. if err != nil {
  47. panic(err)
  48. }
  49. // Strip the null bytes from the strings
  50. name := strings.Replace(string(pic.PaneName[:]), "\x00", "", -1)
  51. userData := strings.Replace(string(pic.UserData[:]), "\x00", "", -1)
  52. // Get the UVSets
  53. uvSets := make([]XMLUVSet, pic.NumOfUVSets)
  54. for i := 0; i < int(pic.NumOfUVSets); i++ {
  55. offset := 88 + (i * 32)
  56. var uv UVSet
  57. err := binary.Read(bytes.NewReader(data[offset:]), binary.BigEndian, &uv)
  58. if err != nil {
  59. panic(err)
  60. }
  61. set := XMLUVSet{
  62. CoordTL: STCoordinates{
  63. T: uv.TopLeftT,
  64. S: uv.TopLeftS,
  65. },
  66. CoordTR: STCoordinates{
  67. T: uv.TopRightT,
  68. S: uv.TopRightS,
  69. },
  70. CoordBL: STCoordinates{
  71. T: uv.BottomLeftT,
  72. S: uv.BottomLeftS,
  73. },
  74. CoordBR: STCoordinates{
  75. T: uv.BottomRightT,
  76. S: uv.BottomRightS,
  77. },
  78. }
  79. uvSets[i] = set
  80. }
  81. xmlData := XMLPIC{
  82. Name: name,
  83. UserData: userData,
  84. Visible: pic.Flag & 0x1,
  85. Widescreen: (pic.Flag & 0x2) >> 1,
  86. Flag: pic.Flag,
  87. Origin: Coord2D{X: float32(pic.Origin % 3), Y: float32(pic.Origin / 3)},
  88. Alpha: pic.Alpha,
  89. Padding: 0,
  90. Translate: Coord3D{X: pic.XTranslation, Y: pic.YTranslation, Z: pic.ZTranslation},
  91. Rotate: Coord3D{X: pic.XRotate, Y: pic.YRotate, Z: pic.ZRotate},
  92. Scale: Coord2D{X: pic.XScale, Y: pic.YScale},
  93. Width: pic.Width,
  94. Height: pic.Height,
  95. TopLeftColor: Color8{
  96. R: pic.TopLeftColor[0],
  97. G: pic.TopLeftColor[1],
  98. B: pic.TopLeftColor[2],
  99. A: pic.TopLeftColor[3],
  100. },
  101. TopRightColor: Color8{
  102. R: pic.TopRightColor[0],
  103. G: pic.TopRightColor[1],
  104. B: pic.TopRightColor[2],
  105. A: pic.TopRightColor[3],
  106. },
  107. BottomLeftColor: Color8{
  108. R: pic.BottomLeftColor[0],
  109. G: pic.BottomLeftColor[1],
  110. B: pic.BottomLeftColor[2],
  111. A: pic.BottomLeftColor[3],
  112. },
  113. BottomRightColor: Color8{
  114. R: pic.BottomRightColor[0],
  115. G: pic.BottomRightColor[1],
  116. B: pic.BottomRightColor[2],
  117. A: pic.BottomRightColor[3],
  118. },
  119. MatIndex: pic.MatIndex,
  120. UVSets: &XMLUVSets{Set: uvSets},
  121. }
  122. r.Panes = append(r.Panes, Children{PIC: &xmlData})
  123. }
  124. func (b *BRLYTWriter) WritePIC(pic XMLPIC) {
  125. header := SectionHeader{
  126. Type: SectionTypePIC,
  127. Size: uint32(96 + (32 * len(pic.UVSets.Set))),
  128. }
  129. var name [16]byte
  130. copy(name[:], pic.Name)
  131. var userData [8]byte
  132. copy(userData[:], pic.UserData)
  133. pane := PIC{
  134. Flag: pic.Flag,
  135. Origin: uint8(pic.Origin.X + (pic.Origin.Y * 3)),
  136. Alpha: pic.Alpha,
  137. PaneName: name,
  138. UserData: userData,
  139. XTranslation: pic.Translate.X,
  140. YTranslation: pic.Translate.Y,
  141. ZTranslation: pic.Translate.Z,
  142. XRotate: pic.Rotate.X,
  143. YRotate: pic.Rotate.Y,
  144. ZRotate: pic.Rotate.Z,
  145. XScale: pic.Scale.X,
  146. YScale: pic.Scale.Y,
  147. Width: pic.Width,
  148. Height: pic.Height,
  149. TopLeftColor: [4]uint8{pic.TopLeftColor.R, pic.TopLeftColor.G, pic.TopLeftColor.B, pic.TopLeftColor.A},
  150. TopRightColor: [4]uint8{pic.TopRightColor.R, pic.TopRightColor.G, pic.TopRightColor.B, pic.TopRightColor.A},
  151. BottomLeftColor: [4]uint8{pic.BottomLeftColor.R, pic.BottomLeftColor.G, pic.BottomLeftColor.B, pic.BottomLeftColor.A},
  152. BottomRightColor: [4]uint8{pic.BottomRightColor.R, pic.BottomRightColor.G, pic.BottomRightColor.B, pic.BottomRightColor.A},
  153. MatIndex: pic.MatIndex,
  154. NumOfUVSets: uint8(len(pic.UVSets.Set)),
  155. }
  156. write(b, header)
  157. write(b, pane)
  158. // Write the UV Sets
  159. for _, set := range pic.UVSets.Set {
  160. uvSet := UVSet{
  161. TopLeftS: set.CoordTL.S,
  162. TopLeftT: set.CoordTL.T,
  163. TopRightS: set.CoordTR.S,
  164. TopRightT: set.CoordTR.T,
  165. BottomLeftS: set.CoordBL.S,
  166. BottomLeftT: set.CoordBL.T,
  167. BottomRightS: set.CoordBR.S,
  168. BottomRightT: set.CoordBR.T,
  169. }
  170. write(b, uvSet)
  171. }
  172. }