library_images.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. // Library Images
  12. //
  13. type LibraryImages struct {
  14. Id string
  15. Name string
  16. Asset *Asset
  17. Image []*Image
  18. }
  19. func (li *LibraryImages) Dump(out io.Writer, indent int) {
  20. if li == nil {
  21. return
  22. }
  23. fmt.Fprintf(out, "%sLibraryImages id:%s name:%s\n", sIndent(indent), li.Id, li.Name)
  24. for _, img := range li.Image {
  25. img.Dump(out, indent+step)
  26. }
  27. }
  28. //
  29. // Image
  30. //
  31. type Image struct {
  32. Id string
  33. Name string
  34. Format string
  35. Height uint
  36. Width uint
  37. Depth uint
  38. ImageSource interface{}
  39. }
  40. func (img *Image) Dump(out io.Writer, indent int) {
  41. fmt.Fprintf(out, "%sImage id:%s name:%s\n", sIndent(indent), img.Id, img.Name)
  42. ind := indent + step
  43. switch is := img.ImageSource.(type) {
  44. case InitFrom:
  45. is.Dump(out, ind)
  46. }
  47. }
  48. //
  49. //
  50. //
  51. type InitFrom struct {
  52. Uri string
  53. }
  54. func (initf *InitFrom) Dump(out io.Writer, indent int) {
  55. fmt.Fprintf(out, "%sInitFrom:%s\n", sIndent(indent), initf.Uri)
  56. }
  57. func (d *Decoder) decLibraryImages(start xml.StartElement, dom *Collada) error {
  58. li := new(LibraryImages)
  59. dom.LibraryImages = li
  60. li.Id = findAttrib(start, "id").Value
  61. li.Name = findAttrib(start, "name").Value
  62. for {
  63. child, _, err := d.decNextChild(start)
  64. if err != nil || child.Name.Local == "" {
  65. return err
  66. }
  67. if child.Name.Local == "image" {
  68. err := d.decImage(child, li)
  69. if err != nil {
  70. return err
  71. }
  72. continue
  73. }
  74. }
  75. return nil
  76. }
  77. func (d *Decoder) decImage(start xml.StartElement, li *LibraryImages) error {
  78. img := new(Image)
  79. img.Id = findAttrib(start, "id").Value
  80. img.Name = findAttrib(start, "name").Value
  81. li.Image = append(li.Image, img)
  82. for {
  83. child, data, err := d.decNextChild(start)
  84. if err != nil || child.Name.Local == "" {
  85. return err
  86. }
  87. if child.Name.Local == "init_from" {
  88. err := d.decImageSource(child, data, img)
  89. if err != nil {
  90. return err
  91. }
  92. continue
  93. }
  94. }
  95. return nil
  96. }
  97. func (d *Decoder) decImageSource(start xml.StartElement, cdata []byte, img *Image) error {
  98. img.ImageSource = InitFrom{string(cdata)}
  99. return nil
  100. }