library_images.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package collada
  5. import (
  6. "encoding/xml"
  7. "fmt"
  8. "io"
  9. )
  10. //
  11. // LibraryImages
  12. //
  13. type LibraryImages struct {
  14. Id string
  15. Name string
  16. Asset *Asset
  17. Image []*Image
  18. }
  19. // Dump prints out information about the LibraryImages
  20. func (li *LibraryImages) Dump(out io.Writer, indent int) {
  21. if li == nil {
  22. return
  23. }
  24. fmt.Fprintf(out, "%sLibraryImages id:%s name:%s\n", sIndent(indent), li.Id, li.Name)
  25. for _, img := range li.Image {
  26. img.Dump(out, indent+step)
  27. }
  28. }
  29. //
  30. // Image
  31. //
  32. type Image struct {
  33. Id string
  34. Name string
  35. Format string
  36. Height uint
  37. Width uint
  38. Depth uint
  39. ImageSource interface{}
  40. }
  41. // Dump prints out information about the Image
  42. func (img *Image) Dump(out io.Writer, indent int) {
  43. fmt.Fprintf(out, "%sImage id:%s name:%s\n", sIndent(indent), img.Id, img.Name)
  44. ind := indent + step
  45. switch is := img.ImageSource.(type) {
  46. case InitFrom:
  47. is.Dump(out, ind)
  48. }
  49. }
  50. //
  51. // InitFrom
  52. //
  53. type InitFrom struct {
  54. Uri string
  55. }
  56. // Dump prints out information about the InitFrom
  57. func (initf *InitFrom) Dump(out io.Writer, indent int) {
  58. fmt.Fprintf(out, "%sInitFrom:%s\n", sIndent(indent), initf.Uri)
  59. }
  60. func (d *Decoder) decLibraryImages(start xml.StartElement, dom *Collada) error {
  61. li := new(LibraryImages)
  62. dom.LibraryImages = li
  63. li.Id = findAttrib(start, "id").Value
  64. li.Name = findAttrib(start, "name").Value
  65. for {
  66. child, _, err := d.decNextChild(start)
  67. if err != nil || child.Name.Local == "" {
  68. return err
  69. }
  70. if child.Name.Local == "image" {
  71. err := d.decImage(child, li)
  72. if err != nil {
  73. return err
  74. }
  75. continue
  76. }
  77. }
  78. return nil
  79. }
  80. func (d *Decoder) decImage(start xml.StartElement, li *LibraryImages) error {
  81. img := new(Image)
  82. img.Id = findAttrib(start, "id").Value
  83. img.Name = findAttrib(start, "name").Value
  84. li.Image = append(li.Image, img)
  85. for {
  86. child, data, err := d.decNextChild(start)
  87. if err != nil || child.Name.Local == "" {
  88. return err
  89. }
  90. if child.Name.Local == "init_from" {
  91. err := d.decImageSource(child, data, img)
  92. if err != nil {
  93. return err
  94. }
  95. continue
  96. }
  97. }
  98. return nil
  99. }
  100. func (d *Decoder) decImageSource(start xml.StartElement, cdata []byte, img *Image) error {
  101. img.ImageSource = InitFrom{string(cdata)}
  102. return nil
  103. }