11
2- import { Theme } from "../colors/Theme" ;
32import { Button } from "../components/Button" ;
4- import { ImagePreview } from "../components/ImagePreview" ;
5- import { TextLabel } from "../components/TextLabel" ;
6- import { IOutlet } from "../core/IOutlet" ;
3+ //import { ImagePreview } from "../components/ImagePreview";
4+ import { TextLabel } from "../components/TextLabel" ;
75import { Layout , Row } from "../layout/Layout" ;
8- import { LayoutElement } from "../layout/LayoutElement" ;
9- import { Input } from "./Input" ;
6+ import { LayoutElement } from "../layout/LayoutElement" ;
107
118export class TextureProperty extends LayoutElement {
129
1310 private initial : Layout ;
1411 private overwritten : Layout ;
1512 private _imageDisplayLayout : Layout ;
13+ private currentLoadedImage ? : {
14+ url :string ,
15+ mime :string ,
16+ dispose :VoidFunction
17+ } ;
1618
1719 /**
1820 * The path/url used to load the image. In case of a file selected from disk this will only be the filename.
@@ -23,7 +25,8 @@ export class TextureProperty extends LayoutElement {
2325 private _isFromDisk = false ;
2426 get isFromDisk ( ) { return this . _isFromDisk }
2527
26- private imgPreview :ImagePreview ;
28+ //private imgPreview:ImagePreview;
29+ private filenameLabel :TextLabel ;
2730
2831 get imagePath ( ) {
2932 return this . _filePath ;
@@ -34,6 +37,10 @@ export class TextureProperty extends LayoutElement {
3437 this . loadImage ( imageSrc )
3538 }
3639
40+ get imageType ( ) {
41+ return this . currentLoadedImage ?. mime ;
42+ }
43+
3744
3845 constructor ( ) {
3946 super ( )
@@ -47,15 +54,16 @@ export class TextureProperty extends LayoutElement {
4754 justify :"space-around"
4855 } ) ;
4956
50- this . imgPreview = new ImagePreview ( ) ;
57+ //this.imgPreview = new ImagePreview();
58+ this . filenameLabel = new TextLabel ( "" ) ;
5159
5260 //"column","start","stretch",
5361 this . _imageDisplayLayout = new Layout ( [
5462
5563 //"row","space-around","center",
5664 new Layout ( [
5765
58- this . imgPreview ,
66+ this . filenameLabel ,
5967 new Button ( "X" , ( ) => this . reset ( ) ) ,
6068
6169 ] , {
@@ -86,7 +94,7 @@ export class TextureProperty extends LayoutElement {
8694 return new Promise < File | null > ( ( resolve ) => {
8795 const input = document . createElement ( 'input' ) ;
8896 input . type = 'file' ;
89- input . accept = 'image/*' ; // Restrict to image files
97+ input . accept = 'image/*,.exr,image/x-exr ' ; // Restrict to image files
9098 input . style . display = 'none' ;
9199
92100 input . addEventListener ( 'change' , ( event : Event ) => {
@@ -117,18 +125,37 @@ export class TextureProperty extends LayoutElement {
117125 const reader = new FileReader ( ) ;
118126
119127 reader . onload = e => {
120- const dataURL = e . target ! . result as string ; // dataURL is a string
121-
122- this . loadImage ( dataURL ) ;
128+ const arrayBuffer = e . target ! . result as ArrayBuffer ;
129+ // Create a Blob from the ArrayBuffer
130+ const imageBlob = new Blob ( [ arrayBuffer ] , { type : file . type } ) ; // Use original file type
131+
132+ // Create an Object URL from the Blob
133+ const imageURL = URL . createObjectURL ( imageBlob ) ;
134+
135+ this . currentLoadedImage = {
136+ url : imageURL ,
137+ mime : file . type ,
138+ dispose : ( ) => URL . revokeObjectURL ( imageURL )
139+ }
140+ //image/x-exr
141+
142+ this . loadImage ( imageURL ) ;
123143 } ;
124144
125- reader . readAsDataURL ( file ) ;
145+ reader . onerror = ( e ) => {
146+ console . error ( "FileReader error:" , e ) ;
147+ alert ( "Oops! Error loading the image..." )
148+ } ;
149+
150+ reader . readAsArrayBuffer ( file ) ;
126151 }
127152
128153 protected loadImage ( url :string ) {
129154
130155 this . _imageSrc = url ;
131- this . imgPreview . show ( url ) ;
156+ //this.imgPreview.show( url );
157+ this . filenameLabel . label = this . imagePath ! ;
158+
132159 this . layout = this . _imageDisplayLayout ;
133160 this . singleLine = false ;
134161
@@ -150,11 +177,19 @@ export class TextureProperty extends LayoutElement {
150177
151178
152179 protected reset ( ) {
153- this . imgPreview . reset ( ) ;
180+
181+ this . dispose ( ) ;
182+
183+ // this.imgPreview.reset();
154184 this . layout = this . initial ;
155185 this . singleLine = true ;
156186
157187 this . root . update ( )
158188 }
189+
190+ dispose ( ) {
191+ this . currentLoadedImage ?. dispose ( ) ;
192+ this . currentLoadedImage = undefined ;
193+ }
159194
160195}
0 commit comments