-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageWindow.cpp
811 lines (766 loc) · 24.8 KB
/
ImageWindow.cpp
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
/*
* Membrane Software Reference Source License
* Version 2024 Sep 18
* This license is a legal agreement between you and Membrane Software
*
* This license agreement governs use of the accompanying source code. If you use the source code, you accept this license. If you do not accept the license, do not use the source code.
*
* DEFINITIONS
* "compilation" means to compile the code from source code to machine code.
* “non-commercial distribution” means distribution of the code or any compilation of the code, or of any other application or program containing the code or any compilation of the code, where such distribution is not intended for or directed towards commercial advantage or monetary compensation.
* "review" means to access, analyse, test and otherwise review the code as a reference
* "you" means the licensee of rights set out in this license.
*
* GRANT OF RIGHTS
* Subject to the terms of this license, we grant you a non-transferable, non-exclusive, worldwide, royalty-free license to access and use the source code solely for the purposes of review, compilation and non-commercial distribution.
*
* LIMITATIONS
* This license does not grant you any rights to use Membrane Software's name, logo, or trademarks.
*
* If you issue proceedings in any jurisdiction against Membrane Software because you consider Membrane Software has infringed copyright or any patent right in respect of the code (including any joinder or counterclaim), your license to the code is automatically terminated.
*
* This source code is provided by the copyright holders and contributors "as is" and any express or implied warranties, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose are disclaimed. In no event shall the copyright holder or contributors be liable for any direct, indirect, incidental, special, exemplary, or consequential damages (including, but not limited to, procurement of substitute goods or services; loss of use, data, or profits; or business interruption) however caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this source code, even if advised of the possibility of such damage.
*
* NO IMPLIED RIGHTS
* All rights not expressly granted by Membrane Software to you in this License Agreement are hereby reserved by Membrane Software and its suppliers. There are no implied rights in this License Agreement.
*
* GOVERNING LAW
* This EULA agreement, and any dispute arising out of or in connection with this EULA agreement, shall be governed by and construed in accordance with the laws of Washington State, United States of America.
*
* QUESTIONS OR ADDITIONAL INFORMATION
* If you have questions regarding this License Agreement, please contact Membrane Software by sending an email to support@membranesoftware.com.
*/
#include "Config.h"
#include "SDL2/SDL_image.h"
#include "App.h"
#include "ClassId.h"
#include "Log.h"
#include "SpriteId.h"
#include "UiConfiguration.h"
#include "Network.h"
#include "MediaUtil.h"
#include "TaskGroup.h"
#include "Resource.h"
#include "SharedBuffer.h"
#include "MediaReader.h"
#include "Sprite.h"
#include "SpriteGroup.h"
#include "ImageWindow.h"
constexpr const int NoResize = 0;
constexpr const int ScaleResize = 1;
constexpr const int FitResize = 2;
ImageWindow::ImageWindow (Image *imageWidget)
: Panel ()
, isLoadingSpriteDisabled (false)
, isOffscreenUnloadEnabled (false)
, isLoadSuspended (false)
, loadingSprite (NULL)
, loadingSpriteColor (0.0f, 0.0f, 0.0f)
, imageLoadType (ImageWindow::NoLoadType)
, isImageFileExternal (false)
, isVideoFileExternal (false)
, videoFrameTimestamp (0)
, imageLoadSourceWidth (0.0f)
, imageLoadSourceHeight (0.0f)
, isLoadingImageData (false)
, isLoadFailed (false)
, imageHandle (&image)
, loadingProgressRingHandle (&loadingProgressRing)
, drawAlpha (1.0f)
, isWindowSizeEnabled (false)
, windowWidth (0.0f)
, windowHeight (0.0f)
, loadingWidth (0.0f)
, loadingHeight (0.0f)
, isImageDataLoaded (false)
, shouldInvokeLoadEndCallback (false)
, imageUrlData (NULL)
, mediaReader (NULL)
, videoFrameSeekPercent (0.0f)
, videoFrameSeekTimestamp (-1)
, onLoadResizeType (NoResize)
, onLoadWidth (0.0f)
, onLoadHeight (0.0f)
, nextImageLoadType (ImageWindow::NoLoadType)
, nextIsImageFileExternal (false)
, nextIsVideoFileExternal (false)
, nextVideoFrameSeekTimestamp (-1)
, nextVideoFrameSeekPercent (0.0f)
{
classId = ClassId::ImageWindow;
loadingSprite = SpriteGroup::instance->getSprite (SpriteId::SpriteGroup_loadingImageIcon);
loadErrorSprite = SpriteGroup::instance->getSprite (SpriteId::SpriteGroup_loadingImageErrorIcon);
if (imageWidget) {
imageHandle.assign (imageWidget);
addWidget (imageWidget);
}
}
ImageWindow::~ImageWindow () {
if (mediaReader) {
mediaReader->release ();
mediaReader = NULL;
}
if (imageUrlData) {
imageUrlData->release ();
imageUrlData = NULL;
}
imageHandle.destroyAndClear ();
}
ImageWindow *ImageWindow::castWidget (Widget *widget) {
return (Widget::isWidgetClass (widget, ClassId::ImageWindow) ? (ImageWindow *) widget : NULL);
}
void ImageWindow::setWindowSize (bool enable, double windowSizeWidth, double windowSizeHeight) {
if (! enable) {
isWindowSizeEnabled = false;
}
else {
if ((windowSizeWidth >= 1.0f) && (windowSizeHeight >= 1.0f)) {
isWindowSizeEnabled = true;
windowWidth = windowSizeWidth;
windowHeight = windowSizeHeight;
}
}
reflow ();
}
bool ImageWindow::isLoaded () {
if (! image) {
return (false);
}
return (isImageDataLoaded && (! shouldInvokeLoadEndCallback));
}
bool ImageWindow::isOffscreen () {
double w, h;
if (! isOffscreenUnloadEnabled) {
return (false);
}
if (! hasScreenPosition) {
return (true);
}
w = App::instance->drawableWidth * UiConfiguration::instance->imageWindowOffscreenAreaMultiplier;
h = App::instance->drawableHeight * UiConfiguration::instance->imageWindowOffscreenAreaMultiplier;
if ((screenX >= -w) && (screenX <= w)) {
if ((screenY >= -h) && (screenY <= h)) {
return (false);
}
}
return (true);
}
void ImageWindow::setImage (Image *nextImage) {
if (image) {
image->zLevel = -1;
image->setDestroyDelay (1);
}
imageHandle.assign (nextImage);
if (image) {
image->drawAlpha = drawAlpha;
addWidget (image);
}
reflow ();
}
void ImageWindow::setFrame (int frame) {
if (! image) {
return;
}
image->setFrame (frame);
reflow ();
}
void ImageWindow::setScale (double scale) {
if (! image) {
return;
}
image->setScale (scale);
reflow ();
}
void ImageWindow::setDrawColor (bool enable, const Color &color) {
if (! image) {
return;
}
image->setDrawColor (enable, color);
reflow ();
}
void ImageWindow::setDrawAlpha (double alphaValue) {
if (alphaValue > 1.0f) {
alphaValue = 1.0f;
}
drawAlpha = alphaValue;
if (image) {
image->drawAlpha = drawAlpha;
}
}
void ImageWindow::setLoadingSize (double loadingWidthValue, double loadingHeightValue) {
loadingWidth = loadingWidthValue;
loadingHeight = loadingHeightValue;
reflow ();
}
void ImageWindow::showLoadingSprite () {
if (isLoadingSpriteDisabled) {
return;
}
loadingProgressRingHandle.destroyAndAssign (new ProgressRing (UiConfiguration::instance->progressRingSize));
loadingProgressRing->setIndeterminate (true);
add (loadingProgressRing, -1);
setImage (new Image (loadingSprite));
image->setDrawColor (true, loadingSpriteColor);
}
void ImageWindow::showLoadErrorSprite () {
loadingProgressRingHandle.destroyAndClear ();
setImage (new Image (loadErrorSprite));
image->setDrawColor (true, loadingSpriteColor);
}
void ImageWindow::loadImageFile (const StdString &filePath, bool isExternalPath) {
if (filePath.empty ()) {
return;
}
if ((imageLoadType == ImageWindow::ImageFileLoadType) && imageFilePath.equals (filePath) && (isImageFileExternal == isExternalPath)) {
return;
}
if (isLoadingImageData) {
nextImageFilePath.assign (filePath);
nextIsImageFileExternal = isExternalPath;
nextImageLoadType = ImageWindow::ImageFileLoadType;
return;
}
imageLoadType = ImageWindow::ImageFileLoadType;
imageFilePath.assign (filePath);
isImageFileExternal = isExternalPath;
imageLoadSourceWidth = 0.0f;
imageLoadSourceHeight = 0.0f;
isLoadFailed = false;
isImageDataLoaded = false;
}
void ImageWindow::loadImageUrl (const StdString &loadUrl) {
if (loadUrl.empty ()) {
return;
}
if ((imageLoadType == ImageWindow::UrlLoadType) && imageUrl.equals (loadUrl)) {
return;
}
if (isLoadingImageData) {
nextImageUrl.assign (loadUrl);
nextImageLoadType = ImageWindow::UrlLoadType;
return;
}
imageLoadType = ImageWindow::UrlLoadType;
imageUrl.assign (loadUrl);
showLoadingSprite ();
imageLoadSourceWidth = 0.0f;
imageLoadSourceHeight = 0.0f;
isLoadFailed = false;
isImageDataLoaded = false;
}
void ImageWindow::loadSeekPercentVideoFrame (const StdString &filePath, double seekPercent, bool isExternalPath) {
if (filePath.empty ()) {
return;
}
if (seekPercent < 0.0f) {
seekPercent = 0.0f;
}
if (seekPercent > 100.0f) {
seekPercent = 100.0f;
}
if ((imageLoadType == ImageWindow::VideoFrameLoadType) && videoFilePath.equals (filePath) && (isVideoFileExternal == isExternalPath) && FLOAT_EQUALS (videoFrameSeekPercent, seekPercent) && (videoFrameSeekTimestamp < 0)) {
return;
}
if (isLoadingImageData) {
nextVideoFilePath.assign (filePath);
nextIsVideoFileExternal = isExternalPath;
nextVideoFrameSeekTimestamp = -1;
nextVideoFrameSeekPercent = seekPercent;
nextImageLoadType = ImageWindow::VideoFrameLoadType;
return;
}
imageLoadType = ImageWindow::VideoFrameLoadType;
videoFilePath.assign (filePath);
videoFrameSeekPercent = seekPercent;
videoFrameSeekTimestamp = -1;
isVideoFileExternal = isExternalPath;
imageLoadSourceWidth = 0.0f;
imageLoadSourceHeight = 0.0f;
isLoadFailed = false;
isImageDataLoaded = false;
}
void ImageWindow::loadSeekTimestampVideoFrame (const StdString &filePath, int64_t seekTimestamp, bool isExternalPath) {
if (filePath.empty ()) {
return;
}
if (seekTimestamp < 0) {
seekTimestamp = 0;
}
if ((imageLoadType == ImageWindow::VideoFrameLoadType) && videoFilePath.equals (filePath) && (isVideoFileExternal == isExternalPath) && (videoFrameSeekTimestamp == seekTimestamp)) {
return;
}
if (isLoadingImageData) {
nextVideoFilePath.assign (filePath);
nextIsVideoFileExternal = isExternalPath;
nextVideoFrameSeekTimestamp = seekTimestamp;
nextImageLoadType = ImageWindow::VideoFrameLoadType;
return;
}
imageLoadType = ImageWindow::VideoFrameLoadType;
videoFilePath.assign (filePath);
videoFrameSeekTimestamp = seekTimestamp;
isVideoFileExternal = isExternalPath;
imageLoadSourceWidth = 0.0f;
imageLoadSourceHeight = 0.0f;
isLoadFailed = false;
isImageDataLoaded = false;
}
void ImageWindow::loadSourceCopy (ImageWindow *sourceImage) {
switch (sourceImage->imageLoadType) {
case ImageWindow::ImageFileLoadType: {
loadImageFile (sourceImage->imageFilePath, sourceImage->isImageFileExternal);
break;
}
case ImageWindow::UrlLoadType: {
loadImageUrl (sourceImage->imageUrl);
break;
}
case ImageWindow::VideoFrameLoadType: {
if (sourceImage->videoFrameSeekTimestamp >= 0) {
loadSeekTimestampVideoFrame (sourceImage->videoFilePath, sourceImage->videoFrameSeekTimestamp, sourceImage->isVideoFileExternal);
}
else {
loadSeekPercentVideoFrame (sourceImage->videoFilePath, sourceImage->videoFrameSeekPercent, sourceImage->isVideoFileExternal);
}
break;
}
}
}
void ImageWindow::onLoadScale (double scaleWidth, double scaleHeight) {
onLoadResizeType = ScaleResize;
onLoadWidth = scaleWidth;
onLoadHeight = scaleHeight;
}
void ImageWindow::onLoadFit (double targetWidth, double targetHeight) {
if ((targetWidth <= 0.0f) || (targetHeight <= 0.0f)) {
return;
}
onLoadResizeType = FitResize;
onLoadWidth = targetWidth;
onLoadHeight = targetHeight;
}
bool ImageWindow::isImageUrlEmpty () {
return (imageUrl.empty ());
}
void ImageWindow::reload () {
if (isLoadingImageData) {
return;
}
if (isImageDataLoaded) {
showLoadingSprite ();
isImageDataLoaded = false;
}
}
void ImageWindow::reflow () {
double w, h;
if (isWindowSizeEnabled) {
setFixedSize (true, windowWidth, windowHeight);
if (image) {
image->position.assign ((width / 2.0f) - (image->width / 2.0f), (height / 2.0f) - (image->height / 2.0f));
}
}
else if (image && (image->sprite == loadingSprite) && (loadingWidth >= 1.0f) && (loadingHeight >= 1.0f)) {
setFixedSize (true, loadingWidth, loadingHeight);
image->position.assign ((width / 2.0f) - (image->width / 2.0f), (height / 2.0f) - (image->height / 2.0f));
}
else {
w = widthPadding * 2.0f;
h = heightPadding * 2.0f;
if (image) {
image->position.assign (widthPadding, heightPadding);
w += image->width;
h += image->height;
}
setFixedSize (true, w, h);
}
if (loadingProgressRing) {
loadingProgressRing->position.assign ((width / 2.0f) - (loadingProgressRing->width / 2.0f), (height / 2.0f) - (loadingProgressRing->height / 2.0f));
}
}
void ImageWindow::doUpdate (int msElapsed) {
bool shouldload;
Panel::doUpdate (msElapsed);
if (! isLoadingImageData) {
switch (nextImageLoadType) {
case ImageWindow::ImageFileLoadType: {
loadImageFile (nextImageFilePath, nextIsImageFileExternal);
nextImageFilePath.assign ("");
nextImageLoadType = ImageWindow::NoLoadType;
break;
}
case ImageWindow::UrlLoadType: {
loadImageUrl (nextImageUrl);
nextImageUrl.assign ("");
nextImageLoadType = ImageWindow::NoLoadType;
break;
}
case ImageWindow::VideoFrameLoadType: {
if (nextVideoFrameSeekTimestamp >= 0) {
loadSeekTimestampVideoFrame (nextVideoFilePath, nextVideoFrameSeekTimestamp, nextIsVideoFileExternal);
}
else {
loadSeekPercentVideoFrame (nextVideoFilePath, nextVideoFrameSeekPercent, nextIsVideoFileExternal);
}
nextVideoFilePath.assign ("");
nextVideoFrameSeekTimestamp = -1;
nextImageLoadType = ImageWindow::NoLoadType;
break;
}
}
}
if (imageLoadType != ImageWindow::NoLoadType) {
shouldload = !(isLoadSuspended || isOffscreen () || isLoadFailed || isImageDataLoaded || isLoadingImageData);
if (! shouldload) {
if ((isLoadSuspended || isOffscreen ()) && isImageDataLoaded && (! isLoadingImageData)) {
setImage (NULL);
isImageDataLoaded = false;
}
}
else {
switch (imageLoadType) {
case ImageWindow::ImageFileLoadType: {
if (imageFilePath.empty ()) {
break;
}
isLoadingImageData = true;
eventCallback (loadStartCallback);
retain ();
App::instance->addPredrawTask (ImageWindow::createImageFromImageFile, this);
break;
}
case ImageWindow::UrlLoadType: {
if (imageUrl.empty ()) {
break;
}
showLoadingSprite ();
isLoadingImageData = true;
eventCallback (loadStartCallback);
retain ();
Network::instance->sendHttpGet (imageUrl, Network::HttpRequestCallbackContext (ImageWindow::createImageFromUrlHttpGetComplete, this));
break;
}
case ImageWindow::VideoFrameLoadType: {
if (videoFilePath.empty ()) {
break;
}
showLoadingSprite ();
isLoadingImageData = true;
eventCallback (loadStartCallback);
retain ();
TaskGroup::instance->run (TaskGroup::RunContext (ImageWindow::readVideoFrame, this));
break;
}
}
}
}
if (loadingProgressRing) {
if (! isLoadingImageData) {
loadingProgressRingHandle.destroyAndClear ();
}
}
if (shouldInvokeLoadEndCallback) {
shouldInvokeLoadEndCallback = false;
eventCallback (loadEndCallback);
}
}
void ImageWindow::createImageFromImageFile (void *itPtr) {
((ImageWindow *) itPtr)->executeCreateImageFromImageFile ();
}
void ImageWindow::executeCreateImageFromImageFile () {
SDL_RWops *rw;
SDL_Surface *surface, *scaledsurface;
SDL_Texture *texture;
Sprite *sprite;
StdString path;
double scaledw, scaledh;
if (isDestroyed) {
imageFilePath.assign ("");
endCreateImageFromImageFile ();
return;
}
surface = NULL;
if (isImageFileExternal) {
rw = SDL_RWFromFile (imageFilePath.c_str (), "r");
if (! rw) {
endCreateImageFromImageFile (StdString::createSprintf ("File open failed, SDL_RWFromFile: %s", SDL_GetError ()));
return;
}
surface = IMG_Load_RW (rw, 1);
if (! surface) {
endCreateImageFromImageFile (StdString::createSprintf ("File data parse failed, IMG_Load_RW: %s", SDL_GetError ()));
return;
}
}
else {
surface = Resource::instance->loadSurface (imageFilePath);
if (! surface) {
endCreateImageFromImageFile (StdString::createSprintf ("Failed to load image data from resources, %s", Resource::instance->lastErrorMessage.c_str ()));
return;
}
}
imageLoadSourceWidth = (double) surface->w;
imageLoadSourceHeight = (double) surface->h;
if (getOnLoadScaleSize (&scaledw, &scaledh)) {
scaledsurface = SDL_CreateRGBSurface (0, (int) floor (scaledw), (int) floor (scaledh), surface->format->BitsPerPixel, surface->format->Rmask, surface->format->Gmask, surface->format->Bmask, surface->format->Amask);
if (scaledsurface) {
SDL_BlitScaled (surface, NULL, scaledsurface, NULL);
SDL_FreeSurface (surface);
surface = scaledsurface;
}
}
path.sprintf ("*_ImageWindow_%llx_%llx", (long long int) id, (long long int) App::instance->getUniqueId ());
texture = Resource::instance->createTexture (path, surface);
SDL_FreeSurface (surface);
if (! texture) {
endCreateImageFromImageFile (StdString::createSprintf ("Failed to create render texture, %s", Resource::instance->lastErrorMessage.c_str ()));
return;
}
sprite = new Sprite ();
sprite->addTexture (texture, path);
setImage (new Image (sprite, 0, true));
isImageDataLoaded = true;
reflow ();
endCreateImageFromImageFile ();
}
void ImageWindow::endCreateImageFromImageFile (const StdString &errorMessage) {
loadErrorMessage.assign (errorMessage);
if (! loadErrorMessage.empty ()) {
isLoadFailed = true;
showLoadErrorSprite ();
Log::debug ("Image load from file failed; path=\"%s\" err=%s", imageFilePath.c_str (), errorMessage.c_str ());
}
if (loadEndCallback.callback) {
shouldInvokeLoadEndCallback = true;
}
isLoadingImageData = false;
release ();
}
void ImageWindow::createImageFromUrlHttpGetComplete (void *itPtr, const StdString &targetUrl, int statusCode, SharedBuffer *responseData) {
ImageWindow *it = (ImageWindow *) itPtr;
if (it->isDestroyed || it->isOffscreen ()) {
it->endCreateImageFromUrl ();
return;
}
if (statusCode != Network::HttpOkCode) {
it->endCreateImageFromUrl (StdString ("Non-success response from server"));
return;
}
if ((! responseData) || responseData->empty ()) {
it->endCreateImageFromUrl (StdString ("Empty response from server"));
return;
}
if (it->imageUrlData) {
it->imageUrlData->release ();
}
it->imageUrlData = responseData;
it->imageUrlData->retain ();
App::instance->addPredrawTask (ImageWindow::createImageFromUrlResponseData, it);
}
void ImageWindow::createImageFromUrlResponseData (void *itPtr) {
((ImageWindow *) itPtr)->executeCreateImageFromUrlResponseData ();
}
void ImageWindow::executeCreateImageFromUrlResponseData () {
SDL_RWops *rw;
SDL_Surface *surface, *scaledsurface;
SDL_Texture *texture;
Sprite *sprite;
StdString path;
double scaledw, scaledh;
if (isDestroyed || isOffscreen () || (! imageUrlData)) {
endCreateImageFromUrl ();
return;
}
rw = SDL_RWFromConstMem (imageUrlData->data, imageUrlData->length);
if (! rw) {
endCreateImageFromUrl (StdString::createSprintf ("Failed to create render texture, SDL_RWFromConstMem: %s", SDL_GetError ()));
return;
}
surface = IMG_Load_RW (rw, 1);
if (! surface) {
endCreateImageFromUrl (StdString::createSprintf ("Failed to create render texture, IMG_Load_RW: %s", SDL_GetError ()));
return;
}
imageLoadSourceWidth = (double) surface->w;
imageLoadSourceHeight = (double) surface->h;
if (getOnLoadScaleSize (&scaledw, &scaledh)) {
scaledsurface = SDL_CreateRGBSurface (0, (int) floor (scaledw), (int) floor (scaledh), surface->format->BitsPerPixel, surface->format->Rmask, surface->format->Gmask, surface->format->Bmask, surface->format->Amask);
if (scaledsurface) {
SDL_BlitScaled (surface, NULL, scaledsurface, NULL);
SDL_FreeSurface (surface);
surface = scaledsurface;
}
}
path.sprintf ("*_ImageWindow_%llx_%llx", (long long int) id, (long long int) App::instance->getUniqueId ());
texture = Resource::instance->createTexture (path, surface);
SDL_FreeSurface (surface);
if (! texture) {
endCreateImageFromUrl (StdString::createSprintf ("Failed to create render texture, %s", Resource::instance->lastErrorMessage.c_str ()));
return;
}
sprite = new Sprite ();
sprite->addTexture (texture, path);
setImage (new Image (sprite, 0, true));
isImageDataLoaded = true;
reflow ();
endCreateImageFromUrl ();
}
void ImageWindow::endCreateImageFromUrl (const StdString &errorMessage) {
loadErrorMessage.assign (errorMessage);
if (! loadErrorMessage.empty ()) {
isLoadFailed = true;
showLoadErrorSprite ();
Log::debug ("Image load from URL failed; url=\"%s\" err=%s", imageUrl.c_str (), errorMessage.c_str ());
}
if (imageUrlData) {
imageUrlData->release ();
imageUrlData = NULL;
}
if (loadEndCallback.callback) {
shouldInvokeLoadEndCallback = true;
}
isLoadingImageData = false;
release ();
}
void ImageWindow::readVideoFrame (void *itPtr) {
((ImageWindow *) itPtr)->executeReadVideoFrame ();
}
void ImageWindow::executeReadVideoFrame () {
double w, h;
if (mediaReader) {
mediaReader->release ();
}
mediaReader = new MediaReader ();
mediaReader->retain ();
mediaReader->setMediaPath (videoFilePath, (! isVideoFileExternal));
if (mediaReader->readMetadata () != OpResult::Success) {
endCreateImageFromVideoFrame (mediaReader->lastErrorMessage);
return;
}
imageLoadSourceWidth = mediaReader->videoFrameWidth;
imageLoadSourceHeight = mediaReader->videoFrameHeight;
if (! getOnLoadScaleSize (&w, &h)) {
w = 0.0f;
h = 0.0f;
}
if (videoFrameSeekTimestamp >= 0) {
mediaReader->setVideoFrameSeekTimestamp (videoFrameSeekTimestamp);
}
else {
mediaReader->setVideoFrameSeekPercent (videoFrameSeekPercent);
}
mediaReader->readVideoFrame ((int) w, (int) h);
if (! mediaReader->videoFrameData) {
endCreateImageFromVideoFrame (mediaReader->lastErrorMessage);
return;
}
videoFrameTimestamp = mediaReader->videoFrameTimestamp;
mediaReader->createVideoFrameTexture (ImageWindow::createVideoFrameTextureComplete, this);
}
bool ImageWindow::createVideoFrameTextureComplete (void *itPtr, MediaReader *reader, SDL_Texture *texture, const StdString &texturePath) {
ImageWindow *it = (ImageWindow *) itPtr;
Sprite *sprite;
bool textureprocessed;
textureprocessed = false;
if (texture && (! texturePath.empty ())) {
sprite = new Sprite ();
sprite->addTexture (texture, texturePath);
it->setImage (new Image (sprite, 0, true));
it->isImageDataLoaded = true;
it->reflow ();
textureprocessed = true;
}
if (! textureprocessed) {
if (it->mediaReader->lastErrorMessage.empty ()) {
it->endCreateImageFromVideoFrame (StdString ("Failed to create image texture"));
}
else {
it->endCreateImageFromVideoFrame (it->mediaReader->lastErrorMessage);
}
}
else {
it->endCreateImageFromVideoFrame ();
}
return (textureprocessed);
}
void ImageWindow::endCreateImageFromVideoFrame (const StdString &errorMessage) {
loadErrorMessage.assign (errorMessage);
if (! loadErrorMessage.empty ()) {
isLoadFailed = true;
showLoadErrorSprite ();
Log::debug ("Image load from video file failed; path=\"%s\" err=%s", videoFilePath.c_str (), errorMessage.c_str ());
}
if (mediaReader) {
mediaReader->release ();
mediaReader = NULL;
}
if (loadEndCallback.callback) {
shouldInvokeLoadEndCallback = true;
}
isLoadingImageData = false;
release ();
}
bool ImageWindow::getOnLoadScaleSize (double *destWidth, double *destHeight) {
double w, h;
if ((imageLoadSourceWidth <= 0.0f) || (imageLoadSourceHeight <= 0.0f)) {
return (false);
}
switch (onLoadResizeType) {
case ScaleResize: {
if ((onLoadWidth <= 0.0f) && (onLoadHeight <= 0.0f)) {
w = imageLoadSourceWidth;
h = imageLoadSourceHeight;
}
else if (onLoadHeight <= 0.0f) {
w = onLoadWidth;
h = (imageLoadSourceHeight * onLoadWidth) / imageLoadSourceWidth;
}
else if (onLoadWidth <= 0.0f) {
h = onLoadHeight;
w = (imageLoadSourceWidth * onLoadHeight) / imageLoadSourceHeight;
}
else {
w = onLoadWidth;
h = onLoadHeight;
}
break;
}
case FitResize: {
if ((onLoadWidth <= 0.0f) || (onLoadHeight <= 0.0f)) {
return (false);
}
if (onLoadWidth >= onLoadHeight) {
w = onLoadWidth;
h = (imageLoadSourceHeight * onLoadWidth) / imageLoadSourceWidth;
if (h > onLoadHeight) {
h = onLoadHeight;
w = (imageLoadSourceWidth * onLoadHeight) / imageLoadSourceHeight;
}
}
else {
h = onLoadHeight;
w = (imageLoadSourceWidth * onLoadHeight) / imageLoadSourceHeight;
if (w > onLoadWidth) {
w = onLoadWidth;
h = (imageLoadSourceHeight * onLoadWidth) / imageLoadSourceWidth;
}
}
break;
}
default: {
return (false);
}
}
if (w < 1.0f) {
w = 1.0f;
}
if (h < 1.0f) {
h = 1.0f;
}
if (destWidth) {
*destWidth = w;
}
if (destHeight) {
*destHeight = h;
}
return (true);
}