Image thread safe improvements
[blender.git] / source / blender / windowmanager / intern / wm_playanim.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Campbell Barton
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/windowmanager/intern/wm_playanim.c
29  *  \ingroup wm
30  *
31  * \note This file uses ghost directly and none of the WM definitions.
32  *       this could be made into its own module, alongside creator/
33  */
34
35 #include <sys/types.h>
36 #include <fcntl.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <math.h>
40
41 #ifndef WIN32
42 #  include <unistd.h>
43 #  include <sys/times.h>
44 #  include <sys/wait.h>
45 #else
46 #  include <io.h>
47 #endif
48 #include "MEM_guardedalloc.h"
49
50 #include "PIL_time.h"
51
52 #include "BLI_fileops.h"
53 #include "BLI_listbase.h"
54 #include "BLI_path_util.h"
55 #include "BLI_rect.h"
56 #include "BLI_string.h"
57 #include "BLI_utildefines.h"
58
59 #include "IMB_imbuf_types.h"
60 #include "IMB_imbuf.h"
61
62 #include "BKE_blender.h"
63 #include "BKE_global.h"
64 #include "BKE_image.h"
65
66 #include "BIF_gl.h"
67 #include "BIF_glutil.h"
68
69 #include "DNA_scene_types.h"
70 #include "ED_datafiles.h" /* for fonts */
71 #include "GHOST_C-api.h"
72 #include "BLF_api.h"
73
74 #include "wm_event_types.h"
75
76 #include "WM_api.h"  /* only for WM_main_playanim */
77
78 typedef struct PlayState {
79
80         /* playback state */
81         short direction;
82         short next;
83         short once;
84         short turbo;
85         short pingpong;
86         short noskip;
87         short sstep;
88         short pause;
89         short wait2;
90         short stopped;
91         short go;
92
93         int fstep;
94
95         /* current picture */
96         struct PlayAnimPict *picture;
97
98         /* set once at the start */
99         int ibufx, ibufy;
100         int fontid;
101
102         /* saves passing args */
103         struct ImBuf *curframe_ibuf;
104 } PlayState;
105
106 /* for debugging */
107 #if 0
108 void print_ps(PlayState *ps)
109 {
110         printf("ps:\n");
111         printf("    direction=%d,\n", (int)ps->direction);
112         printf("    next=%d,\n", ps->next);
113         printf("    once=%d,\n", ps->once);
114         printf("    turbo=%d,\n", ps->turbo);
115         printf("    pingpong=%d,\n", ps->pingpong);
116         printf("    noskip=%d,\n", ps->noskip);
117         printf("    sstep=%d,\n", ps->sstep);
118         printf("    pause=%d,\n", ps->pause);
119         printf("    wait2=%d,\n", ps->wait2);
120         printf("    stopped=%d,\n", ps->stopped);
121         printf("    go=%d,\n\n", ps->go);
122         fflush(stdout);
123 }
124 #endif
125
126 /* global for window and events */
127 typedef enum eWS_Qual {
128         WS_QUAL_LSHIFT  = (1 << 0),
129         WS_QUAL_RSHIFT  = (1 << 1),
130         WS_QUAL_SHIFT   = (WS_QUAL_LSHIFT | WS_QUAL_RSHIFT),
131         WS_QUAL_LALT    = (1 << 2),
132         WS_QUAL_RALT    = (1 << 3),
133         WS_QUAL_ALT     = (WS_QUAL_LALT | WS_QUAL_RALT),
134         WS_QUAL_LCTRL   = (1 << 4),
135         WS_QUAL_RCTRL   = (1 << 5),
136         WS_QUAL_LMOUSE  = (1 << 16),
137         WS_QUAL_MMOUSE  = (1 << 17),
138         WS_QUAL_RMOUSE  = (1 << 18),
139         WS_QUAL_MOUSE   = (WS_QUAL_LMOUSE | WS_QUAL_MMOUSE | WS_QUAL_RMOUSE)
140 } eWS_Qual;
141
142 static struct WindowStateGlobal {
143         GHOST_SystemHandle ghost_system;
144         void *ghost_window;
145
146         /* events */
147         eWS_Qual qual;
148 } g_WS = {NULL};
149
150 static void playanim_window_get_size(int *width_r, int *height_r)
151 {
152         GHOST_RectangleHandle bounds = GHOST_GetClientBounds(g_WS.ghost_window);
153         *width_r = GHOST_GetWidthRectangle(bounds);
154         *height_r = GHOST_GetHeightRectangle(bounds);
155         GHOST_DisposeRectangle(bounds);
156 }
157
158 /* implementation */
159 static void playanim_event_qual_update(void)
160 {
161         int val;
162
163         /* Shift */
164         GHOST_GetModifierKeyState(g_WS.ghost_system, GHOST_kModifierKeyLeftShift, &val);
165         if (val) g_WS.qual |=  WS_QUAL_LSHIFT;
166         else     g_WS.qual &= ~WS_QUAL_LSHIFT;
167
168         GHOST_GetModifierKeyState(g_WS.ghost_system, GHOST_kModifierKeyRightShift, &val);
169         if (val) g_WS.qual |=  WS_QUAL_RSHIFT;
170         else     g_WS.qual &= ~WS_QUAL_RSHIFT;
171
172         /* Control */
173         GHOST_GetModifierKeyState(g_WS.ghost_system, GHOST_kModifierKeyLeftControl, &val);
174         if (val) g_WS.qual |=  WS_QUAL_LCTRL;
175         else     g_WS.qual &= ~WS_QUAL_LCTRL;
176
177         GHOST_GetModifierKeyState(g_WS.ghost_system, GHOST_kModifierKeyRightControl, &val);
178         if (val) g_WS.qual |=  WS_QUAL_RCTRL;
179         else     g_WS.qual &= ~WS_QUAL_RCTRL;
180
181         /* Alt */
182         GHOST_GetModifierKeyState(g_WS.ghost_system, GHOST_kModifierKeyLeftAlt, &val);
183         if (val) g_WS.qual |=  WS_QUAL_LCTRL;
184         else     g_WS.qual &= ~WS_QUAL_LCTRL;
185
186         GHOST_GetModifierKeyState(g_WS.ghost_system, GHOST_kModifierKeyRightAlt, &val);
187         if (val) g_WS.qual |=  WS_QUAL_RCTRL;
188         else     g_WS.qual &= ~WS_QUAL_RCTRL;
189
190         /* LMB */
191         GHOST_GetButtonState(g_WS.ghost_system, GHOST_kButtonMaskLeft, &val);
192         if (val) g_WS.qual |=  WS_QUAL_LMOUSE;
193         else     g_WS.qual &= ~WS_QUAL_LMOUSE;
194
195         /* MMB */
196         GHOST_GetButtonState(g_WS.ghost_system, GHOST_kButtonMaskMiddle, &val);
197         if (val) g_WS.qual |=  WS_QUAL_MMOUSE;
198         else     g_WS.qual &= ~WS_QUAL_MMOUSE;
199
200         /* RMB */
201         GHOST_GetButtonState(g_WS.ghost_system, GHOST_kButtonMaskRight, &val);
202         if (val) g_WS.qual |=  WS_QUAL_RMOUSE;
203         else     g_WS.qual &= ~WS_QUAL_RMOUSE;
204 }
205
206 typedef struct PlayAnimPict {
207         struct PlayAnimPict *next, *prev;
208         char *mem;
209         int size;
210         char *name;
211         struct ImBuf *ibuf;
212         struct anim *anim;
213         int frame;
214         int IB_flags;
215 } PlayAnimPict;
216
217 static struct ListBase picsbase = {NULL, NULL};
218 static int fromdisk = FALSE;
219 static float zoomx = 1.0, zoomy = 1.0;
220 static double ptottime = 0.0, swaptime = 0.04;
221
222 static int pupdate_time(void)
223 {
224         static double ltime;
225         double time;
226
227         time = PIL_check_seconds_timer();
228
229         ptottime += (time - ltime);
230         ltime = time;
231         return (ptottime < 0);
232 }
233
234 static void playanim_toscreen(PlayAnimPict *picture, struct ImBuf *ibuf, int fontid, int fstep)
235 {
236
237         if (ibuf == NULL) {
238                 printf("no ibuf !\n");
239                 return;
240         }
241         if (ibuf->rect == NULL && ibuf->rect_float) {
242                 IMB_rect_from_float(ibuf);
243                 imb_freerectfloatImBuf(ibuf);
244         }
245         if (ibuf->rect == NULL)
246                 return;
247
248         GHOST_ActivateWindowDrawingContext(g_WS.ghost_window);
249
250         glRasterPos2f(0.0f, 0.0f);
251
252         glDrawPixels(ibuf->x, ibuf->y, GL_RGBA, GL_UNSIGNED_BYTE, ibuf->rect);
253
254         pupdate_time();
255
256         if (picture && (g_WS.qual & (WS_QUAL_SHIFT | WS_QUAL_LMOUSE)) && (fontid != -1)) {
257                 int sizex, sizey;
258                 float fsizex_inv, fsizey_inv;
259                 char str[32 + FILE_MAX];
260                 cpack(-1);
261                 BLI_snprintf(str, sizeof(str), "%s | %.2f frames/s", picture->name, fstep / swaptime);
262
263                 playanim_window_get_size(&sizex, &sizey);
264                 fsizex_inv = 1.0f / sizex;
265                 fsizey_inv = 1.0f / sizey;
266
267                 BLF_enable(fontid, BLF_ASPECT);
268                 BLF_aspect(fontid, fsizex_inv, fsizey_inv, 1.0f);
269                 BLF_position(fontid, 10.0f * fsizex_inv, 10.0f * fsizey_inv, 0.0f);
270                 BLF_draw(fontid, str, sizeof(str));
271         }
272
273         GHOST_SwapWindowBuffers(g_WS.ghost_window);
274 }
275
276 static void build_pict_list(char *first, int totframes, int fstep, int fontid)
277 {
278         char *mem, filepath[FILE_MAX];
279 //      short val;
280         PlayAnimPict *picture = NULL;
281         struct ImBuf *ibuf = NULL;
282         char str[32 + FILE_MAX];
283         struct anim *anim;
284
285         if (IMB_isanim(first)) {
286                 /* OCIO_TODO: support different input color space */
287                 anim = IMB_open_anim(first, IB_rect, 0, NULL);
288                 if (anim) {
289                         int pic;
290                         ibuf = IMB_anim_absolute(anim, 0, IMB_TC_NONE, IMB_PROXY_NONE);
291                         if (ibuf) {
292                                 playanim_toscreen(NULL, ibuf, fontid, fstep);
293                                 IMB_freeImBuf(ibuf);
294                         }
295
296                         for (pic = 0; pic < IMB_anim_get_duration(anim, IMB_TC_NONE); pic++) {
297                                 picture = (PlayAnimPict *)MEM_callocN(sizeof(PlayAnimPict), "Pict");
298                                 picture->anim = anim;
299                                 picture->frame = pic;
300                                 picture->IB_flags = IB_rect;
301                                 BLI_snprintf(str, sizeof(str), "%s : %4.d", first, pic + 1);
302                                 picture->name = strdup(str);
303                                 BLI_addtail(&picsbase, picture);
304                         }
305                 }
306                 else {
307                         printf("couldn't open anim %s\n", first);
308                 }
309         }
310         else {
311                 int count = 0;
312
313                 BLI_strncpy(filepath, first, sizeof(filepath));
314
315                 pupdate_time();
316                 ptottime = 1.0;
317
318                 /* O_DIRECT
319                  *
320                  * If set, all reads and writes on the resulting file descriptor will
321                  * be performed directly to or from the user program buffer, provided
322                  * appropriate size and alignment restrictions are met.  Refer to the
323                  * F_SETFL and F_DIOINFO commands in the fcntl(2) manual entry for
324                  * information about how to determine the alignment constraints.
325                  * O_DIRECT is a Silicon Graphics extension and is only supported on
326                  * local EFS and XFS file systems.
327                  */
328
329                 while (IMB_ispic(filepath) && totframes) {
330                         size_t size;
331                         int file;
332
333                         file = open(filepath, O_BINARY | O_RDONLY, 0);
334                         if (file < 0) {
335                                 /* print errno? */
336                                 return;
337                         }
338
339                         picture = (PlayAnimPict *)MEM_callocN(sizeof(PlayAnimPict), "picture");
340                         if (picture == NULL) {
341                                 printf("Not enough memory for pict struct '%s'\n", filepath);
342                                 close(file);
343                                 return;
344                         }
345                         size = BLI_file_descriptor_size(file);
346
347                         if (size < 1) {
348                                 close(file);
349                                 MEM_freeN(picture);
350                                 return;
351                         }
352
353                         picture->size = size;
354                         picture->IB_flags = IB_rect;
355
356                         if (fromdisk == FALSE) {
357                                 mem = (char *)MEM_mallocN(size, "build pic list");
358                                 if (mem == NULL) {
359                                         printf("Couldn't get memory\n");
360                                         close(file);
361                                         MEM_freeN(picture);
362                                         return;
363                                 }
364
365                                 if (read(file, mem, size) != size) {
366                                         printf("Error while reading %s\n", filepath);
367                                         close(file);
368                                         MEM_freeN(picture);
369                                         MEM_freeN(mem);
370                                         return;
371                                 }
372                         }
373                         else {
374                                 mem = NULL;
375                         }
376
377                         picture->mem = mem;
378                         picture->name = strdup(filepath);
379                         close(file);
380                         BLI_addtail(&picsbase, picture);
381                         count++;
382
383                         pupdate_time();
384
385                         if (ptottime > 1.0) {
386                                 /* OCIO_TODO: support different input color space */
387                                 if (picture->mem) {
388                                         ibuf = IMB_ibImageFromMemory((unsigned char *)picture->mem, picture->size,
389                                                                      picture->IB_flags, NULL, picture->name);
390                                 }
391                                 else {
392                                         ibuf = IMB_loadiffname(picture->name, picture->IB_flags, NULL);
393                                 }
394                                 if (ibuf) {
395                                         playanim_toscreen(picture, ibuf, fontid, fstep);
396                                         IMB_freeImBuf(ibuf);
397                                 }
398                                 pupdate_time();
399                                 ptottime = 0.0;
400                         }
401
402                         BLI_newname(filepath, +fstep);
403
404 #if 0 // XXX25
405                         while (qtest()) {
406                                 switch (qreadN(&val)) {
407                                         case ESCKEY:
408                                                 if (val) return;
409                                                 break;
410                                 }
411                         }
412 #endif
413                         totframes--;
414                 }
415         }
416         return;
417 }
418
419 static int ghost_event_proc(GHOST_EventHandle evt, GHOST_TUserDataPtr ps_void)
420 {
421         PlayState *ps = (PlayState *)ps_void;
422         GHOST_TEventType type = GHOST_GetEventType(evt);
423         int val;
424
425         // print_ps(ps);
426
427         playanim_event_qual_update();
428
429         /* convert ghost event into value keyboard or mouse */
430         val = ELEM(type, GHOST_kEventKeyDown, GHOST_kEventButtonDown);
431
432         if (ps->wait2 && ps->stopped) {
433                 ps->stopped = FALSE;
434         }
435
436         switch (type) {
437                 case GHOST_kEventKeyDown:
438                 case GHOST_kEventKeyUp:
439                 {
440                         GHOST_TEventKeyData *key_data;
441
442                         key_data = (GHOST_TEventKeyData *)GHOST_GetEventData(evt);
443                         switch (key_data->key) {
444                                 case GHOST_kKeyA:
445                                         if (val) ps->noskip = !ps->noskip;
446                                         break;
447                                 case GHOST_kKeyP:
448                                         if (val) ps->pingpong = !ps->pingpong;
449                                         break;
450                                 case GHOST_kKeyNumpad1:
451                                         if (val) swaptime = ps->fstep / 60.0;
452                                         break;
453                                 case GHOST_kKeyNumpad2:
454                                         if (val) swaptime = ps->fstep / 50.0;
455                                         break;
456                                 case GHOST_kKeyNumpad3:
457                                         if (val) swaptime = ps->fstep / 30.0;
458                                         break;
459                                 case GHOST_kKeyNumpad4:
460                                         if (g_WS.qual & WS_QUAL_SHIFT)
461                                                 swaptime = ps->fstep / 24.0;
462                                         else
463                                                 swaptime = ps->fstep / 25.0;
464                                         break;
465                                 case GHOST_kKeyNumpad5:
466                                         if (val) swaptime = ps->fstep / 20.0;
467                                         break;
468                                 case GHOST_kKeyNumpad6:
469                                         if (val) swaptime = ps->fstep / 15.0;
470                                         break;
471                                 case GHOST_kKeyNumpad7:
472                                         if (val) swaptime = ps->fstep / 12.0;
473                                         break;
474                                 case GHOST_kKeyNumpad8:
475                                         if (val) swaptime = ps->fstep / 10.0;
476                                         break;
477                                 case GHOST_kKeyNumpad9:
478                                         if (val) swaptime = ps->fstep / 6.0;
479                                         break;
480                                 case GHOST_kKeyLeftArrow:
481                                         if (val) {
482                                                 ps->sstep = TRUE;
483                                                 ps->wait2 = FALSE;
484                                                 if (g_WS.qual & WS_QUAL_SHIFT) {
485                                                         ps->picture = picsbase.first;
486                                                         ps->next = 0;
487                                                 }
488                                                 else {
489                                                         ps->next = -1;
490                                                 }
491                                         }
492                                         break;
493                                 case GHOST_kKeyDownArrow:
494                                         if (val) {
495                                                 ps->wait2 = FALSE;
496                                                 if (g_WS.qual & WS_QUAL_SHIFT) {
497                                                         ps->next = ps->direction = -1;
498                                                 }
499                                                 else {
500                                                         ps->next = -10;
501                                                         ps->sstep = TRUE;
502                                                 }
503                                         }
504                                         break;
505                                 case GHOST_kKeyRightArrow:
506                                         if (val) {
507                                                 ps->sstep = TRUE;
508                                                 ps->wait2 = FALSE;
509                                                 if (g_WS.qual & WS_QUAL_SHIFT) {
510                                                         ps->picture = picsbase.last;
511                                                         ps->next = 0;
512                                                 }
513                                                 else {
514                                                         ps->next = 1;
515                                                 }
516                                         }
517                                         break;
518                                 case GHOST_kKeyUpArrow:
519                                         if (val) {
520                                                 ps->wait2 = FALSE;
521                                                 if (g_WS.qual & WS_QUAL_SHIFT) {
522                                                         ps->next = ps->direction = 1;
523                                                 }
524                                                 else {
525                                                         ps->next = 10;
526                                                         ps->sstep = TRUE;
527                                                 }
528                                         }
529                                         break;
530
531                                 case GHOST_kKeySlash:
532                                 case GHOST_kKeyNumpadSlash:
533                                         if (val) {
534                                                 if (g_WS.qual & WS_QUAL_SHIFT) {
535                                                         if (ps->curframe_ibuf)
536                                                                 printf(" Name: %s | Speed: %.2f frames/s\n", ps->curframe_ibuf->name, ps->fstep / swaptime);
537                                                 }
538                                                 else {
539                                                         swaptime = ps->fstep / 5.0;
540                                                 }
541                                         }
542                                         break;
543                                 case GHOST_kKeyEqual:
544                                         if (val) {
545                                                 if (g_WS.qual & WS_QUAL_SHIFT) {
546                                                         ps->pause++;
547                                                         printf("pause:%d\n", ps->pause);
548                                                 }
549                                                 else {
550                                                         swaptime /= 1.1;
551                                                 }
552                                         }
553                                         break;
554                                 case GHOST_kKeyMinus:
555                                         if (val) {
556                                                 if (g_WS.qual & WS_QUAL_SHIFT) {
557                                                         ps->pause--;
558                                                         printf("pause:%d\n", ps->pause);
559                                                 }
560                                                 else {
561                                                         swaptime *= 1.1;
562                                                 }
563                                         }
564                                         break;
565                                 case GHOST_kKeyNumpad0:
566                                         if (val) {
567                                                 if (ps->once) {
568                                                         ps->once = ps->wait2 = FALSE;
569                                                 }
570                                                 else {
571                                                         ps->picture = NULL;
572                                                         ps->once = TRUE;
573                                                         ps->wait2 = FALSE;
574                                                 }
575                                         }
576                                         break;
577                                 case GHOST_kKeyEnter:
578                                 case GHOST_kKeyNumpadEnter:
579                                         if (val) {
580                                                 ps->wait2 = ps->sstep = FALSE;
581                                         }
582                                         break;
583                                 case GHOST_kKeyNumpadPeriod:
584                                         if (val) {
585                                                 if (ps->sstep) ps->wait2 = FALSE;
586                                                 else {
587                                                         ps->sstep = TRUE;
588                                                         ps->wait2 = !ps->wait2;
589                                                 }
590                                         }
591                                         break;
592                                 case GHOST_kKeyNumpadPlus:
593                                         if (val == 0) break;
594                                         zoomx += 2.0f;
595                                         zoomy += 2.0f;
596                                         /* no break??? - is this intentional? - campbell XXX25 */
597                                 case GHOST_kKeyNumpadMinus:
598                                 {
599                                         int sizex, sizey;
600                                         /* int ofsx, ofsy; */ /* UNUSED */
601
602                                         if (val == 0) break;
603                                         if (zoomx > 1.0f) zoomx -= 1.0f;
604                                         if (zoomy > 1.0f) zoomy -= 1.0f;
605                                         // playanim_window_get_position(&ofsx, &ofsy);
606                                         playanim_window_get_size(&sizex, &sizey);
607                                         /* ofsx += sizex / 2; */ /* UNUSED */
608                                         /* ofsy += sizey / 2; */ /* UNUSED */
609                                         sizex = zoomx * ps->ibufx;
610                                         sizey = zoomy * ps->ibufy;
611                                         /* ofsx -= sizex / 2; */ /* UNUSED */
612                                         /* ofsy -= sizey / 2; */ /* UNUSED */
613                                         // window_set_position(g_WS.ghost_window,sizex,sizey);
614                                         GHOST_SetClientSize(g_WS.ghost_window, sizex, sizey);
615                                         break;
616                                 }
617                                 case GHOST_kKeyEsc:
618                                         ps->go = FALSE;
619                                         break;
620                                 default:
621                                         break;
622                         }
623                         break;
624                 }
625                 case GHOST_kEventCursorMove:
626                 {
627                         if (g_WS.qual & WS_QUAL_LMOUSE) {
628                                 int sizex, sizey;
629                                 int i;
630
631                                 GHOST_TEventCursorData *cd = GHOST_GetEventData(evt);
632                                 int cx, cy;
633
634                                 GHOST_ScreenToClient(g_WS.ghost_window, cd->x, cd->y, &cx, &cy);
635
636                                 playanim_window_get_size(&sizex, &sizey);
637                                 ps->picture = picsbase.first;
638                                 /* TODO - store in ps direct? */
639                                 i = 0;
640                                 while (ps->picture) {
641                                         i++;
642                                         ps->picture = ps->picture->next;
643                                 }
644                                 i = (i * cx) / sizex;
645                                 ps->picture = picsbase.first;
646                                 for (; i > 0; i--) {
647                                         if (ps->picture->next == NULL) break;
648                                         ps->picture = ps->picture->next;
649                                 }
650                                 ps->sstep = TRUE;
651                                 ps->wait2 = FALSE;
652                                 ps->next = 0;
653                         }
654                         break;
655                 }
656                 case GHOST_kEventWindowSize:
657                 case GHOST_kEventWindowMove:
658                 {
659                         int sizex, sizey;
660
661                         playanim_window_get_size(&sizex, &sizey);
662                         GHOST_ActivateWindowDrawingContext(g_WS.ghost_window);
663
664                         glViewport(0, 0, sizex, sizey);
665                         glScissor(0, 0, sizex, sizey);
666
667                         zoomx = (float) sizex / ps->ibufx;
668                         zoomy = (float) sizey / ps->ibufy;
669                         zoomx = floor(zoomx + 0.5f);
670                         zoomy = floor(zoomy + 0.5f);
671                         if (zoomx < 1.0f) zoomx = 1.0f;
672                         if (zoomy < 1.0f) zoomy = 1.0f;
673
674                         sizex = zoomx * ps->ibufx;
675                         sizey = zoomy * ps->ibufy;
676
677                         glPixelZoom(zoomx, zoomy);
678                         glEnable(GL_DITHER);
679                         ptottime = 0.0;
680                         playanim_toscreen(ps->picture, ps->curframe_ibuf, ps->fontid, ps->fstep);
681
682                         break;
683                 }
684                 case GHOST_kEventQuit:
685                 case GHOST_kEventWindowClose:
686                 {
687                         ps->go = FALSE;
688                         break;
689                 }
690                 default:
691                         /* quiet warnings */
692                         break;
693         }
694
695         return 1;
696 }
697
698 static void playanim_window_open(const char *title, int posx, int posy, int sizex, int sizey, int start_maximized)
699 {
700         GHOST_TWindowState inital_state;
701         GHOST_TUns32 scr_w, scr_h;
702
703         GHOST_GetMainDisplayDimensions(g_WS.ghost_system, &scr_w, &scr_h);
704
705         posy = (scr_h - posy - sizey);
706
707         if (start_maximized == G_WINDOWSTATE_FULLSCREEN)
708                 inital_state = start_maximized ? GHOST_kWindowStateFullScreen : GHOST_kWindowStateNormal;
709         else
710                 inital_state = start_maximized ? GHOST_kWindowStateMaximized : GHOST_kWindowStateNormal;
711
712         g_WS.ghost_window = GHOST_CreateWindow(g_WS.ghost_system,
713                                                title,
714                                                posx, posy, sizex, sizey,
715                                                inital_state,
716                                                GHOST_kDrawingContextTypeOpenGL,
717                                                FALSE /* no stereo */, FALSE);
718 }
719
720
721 void WM_main_playanim(int argc, const char **argv)
722 {
723         struct ImBuf *ibuf = NULL;
724         char filepath[FILE_MAX];
725         GHOST_TUns32 maxwinx, maxwiny;
726         /* short c233 = FALSE, yuvx = FALSE; */ /* UNUSED */
727         int i;
728         /* This was done to disambiguate the name for use under c++. */
729         struct anim *anim = NULL;
730         int start_x = 0, start_y = 0;
731         int sfra = -1;
732         int efra = -1;
733         int totblock;
734
735         PlayState ps = {0};
736
737         /* ps.doubleb   = TRUE;*/ /* UNUSED */
738         ps.go        = TRUE;
739         ps.direction = TRUE;
740         ps.next      = TRUE;
741         ps.once      = FALSE;
742         ps.turbo     = FALSE;
743         ps.pingpong  = FALSE;
744         ps.noskip    = FALSE;
745         ps.sstep     = FALSE;
746         ps.pause     = FALSE;
747         ps.wait2     = FALSE;
748         ps.stopped   = FALSE;
749         ps.picture   = NULL;
750         /* resetmap = FALSE */
751
752         ps.fstep     = 1;
753
754         ps.fontid = -1;
755
756         while (argc > 1) {
757                 if (argv[1][0] == '-') {
758                         switch (argv[1][1]) {
759                                 case 'm':
760                                         fromdisk = TRUE;
761                                         break;
762                                 case 'p':
763                                         if (argc > 3) {
764                                                 start_x = atoi(argv[2]);
765                                                 start_y = atoi(argv[3]);
766                                                 argc -= 2;
767                                                 argv += 2;
768                                         }
769                                         else {
770                                                 printf("too few arguments for -p (need 2): skipping\n");
771                                         }
772                                         break;
773                                 case 'f':
774                                         if (argc > 3) {
775                                                 double fps = atof(argv[2]);
776                                                 double fps_base = atof(argv[3]);
777                                                 if (fps == 0.0) {
778                                                         fps = 1;
779                                                         printf("invalid fps,"
780                                                                "forcing 1\n");
781                                                 }
782                                                 swaptime = fps_base / fps;
783                                                 argc -= 2;
784                                                 argv += 2;
785                                         }
786                                         else {
787                                                 printf("too few arguments for -f (need 2): skipping\n");
788                                         }
789                                         break;
790                                 case 's':
791                                         sfra = MIN2(MAXFRAME, MAX2(1, atoi(argv[2]) ));
792                                         argc--;
793                                         argv++;
794                                         break;
795                                 case 'e':
796                                         efra = MIN2(MAXFRAME, MAX2(1, atoi(argv[2]) ));
797                                         argc--;
798                                         argv++;
799                                         break;
800                                 case 'j':
801                                         ps.fstep = MIN2(MAXFRAME, MAX2(1, atoi(argv[2])));
802                                         swaptime *= ps.fstep;
803                                         argc--;
804                                         argv++;
805                                         break;
806                                 default:
807                                         printf("unknown option '%c': skipping\n", argv[1][1]);
808                                         break;
809                         }
810                         argc--;
811                         argv++;
812                 }
813                 else {
814                         break;
815                 }
816         }
817
818         if (argc > 1) {
819                 BLI_strncpy(filepath, argv[1], sizeof(filepath));
820         }
821         else {
822                 BLI_current_working_dir(filepath, sizeof(filepath));
823                 BLI_add_slash(filepath);
824         }
825
826         if (IMB_isanim(filepath)) {
827                 /* OCIO_TODO: support different input color spaces */
828                 anim = IMB_open_anim(filepath, IB_rect, 0, NULL);
829                 if (anim) {
830                         ibuf = IMB_anim_absolute(anim, 0, IMB_TC_NONE, IMB_PROXY_NONE);
831                         IMB_close_anim(anim);
832                         anim = NULL;
833                 }
834         }
835         else if (!IMB_ispic(filepath)) {
836                 exit(1);
837         }
838
839         if (ibuf == NULL) {
840                 /* OCIO_TODO: support different input color space */
841                 ibuf = IMB_loadiffname(filepath, IB_rect, NULL);
842         }
843
844         if (ibuf == NULL) {
845                 printf("couldn't open %s\n", filepath);
846                 exit(1);
847         }
848
849 #if 0 //XXX25
850         #if !defined(WIN32) && !defined(__APPLE__)
851         if (fork()) exit(0);
852         #endif
853 #endif //XXX25
854
855         /* XXX, fixme zr */
856         {
857 //              extern void add_to_mainqueue(wmWindow *win, void *user_data, short evt, short val, char ascii);
858
859                 GHOST_EventConsumerHandle consumer = GHOST_CreateEventConsumer(ghost_event_proc, &ps);
860
861                 g_WS.ghost_system = GHOST_CreateSystem();
862                 GHOST_AddEventConsumer(g_WS.ghost_system, consumer);
863
864
865
866                 playanim_window_open("Blender:Anim", start_x, start_y, ibuf->x, ibuf->y, 0);
867 //XXX25         window_set_handler(g_WS.ghost_window, add_to_mainqueue, NULL);
868
869                 glMatrixMode(GL_PROJECTION);
870                 glLoadIdentity();
871                 glOrtho(0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f);
872                 glMatrixMode(GL_MODELVIEW);
873         }
874
875         GHOST_GetMainDisplayDimensions(g_WS.ghost_system, &maxwinx, &maxwiny);
876
877         //GHOST_ActivateWindowDrawingContext(g_WS.ghost_window);
878
879         /* initialize the font */
880         BLF_init(11, 72);
881         ps.fontid = BLF_load_mem("monospace", (unsigned char *)datatoc_bmonofont_ttf, datatoc_bmonofont_ttf_size);
882         BLF_size(ps.fontid, 11, 72);
883
884         ps.ibufx = ibuf->x;
885         ps.ibufy = ibuf->y;
886
887         if (maxwinx % ibuf->x) maxwinx = ibuf->x * (1 + (maxwinx / ibuf->x));
888         if (maxwiny % ibuf->y) maxwiny = ibuf->y * (1 + (maxwiny / ibuf->y));
889
890         glClearColor(0.0, 0.0, 0.0, 0.0);
891         glClear(GL_COLOR_BUFFER_BIT);
892
893         GHOST_SwapWindowBuffers(g_WS.ghost_window);
894
895         if (sfra == -1 || efra == -1) {
896                 /* one of the frames was invalid, just use all images */
897                 sfra = 1;
898                 efra = MAXFRAME;
899         }
900
901         build_pict_list(filepath, (efra - sfra) + 1, ps.fstep, ps.fontid);
902
903         for (i = 2; i < argc; i++) {
904                 BLI_strncpy(filepath, argv[i], sizeof(filepath));
905                 build_pict_list(filepath, (efra - sfra) + 1, ps.fstep, ps.fontid);
906         }
907
908         IMB_freeImBuf(ibuf);
909         ibuf = NULL;
910
911         pupdate_time();
912         ptottime = 0;
913
914         /* newly added in 2.6x, without this images never get freed */
915 #define USE_IMB_CACHE
916
917         while (ps.go) {
918                 if (ps.pingpong)
919                         ps.direction = -ps.direction;
920
921                 if (ps.direction == 1) {
922                         ps.picture = picsbase.first;
923                 }
924                 else {
925                         ps.picture = picsbase.last;
926                 }
927
928                 if (ps.picture == NULL) {
929                         printf("couldn't find pictures\n");
930                         ps.go = FALSE;
931                 }
932                 if (ps.pingpong) {
933                         if (ps.direction == 1) {
934                                 ps.picture = ps.picture->next;
935                         }
936                         else {
937                                 ps.picture = ps.picture->prev;
938                         }
939                 }
940                 if (ptottime > 0.0) ptottime = 0.0;
941
942                 while (ps.picture) {
943 #ifndef USE_IMB_CACHE
944                         if (ibuf != NULL && ibuf->ftype == 0) IMB_freeImBuf(ibuf);
945 #endif
946                         if (ps.picture->ibuf) {
947                                 ibuf = ps.picture->ibuf;
948                         }
949                         else if (ps.picture->anim) {
950                                 ibuf = IMB_anim_absolute(ps.picture->anim, ps.picture->frame, IMB_TC_NONE, IMB_PROXY_NONE);
951                         }
952                         else if (ps.picture->mem) {
953                                 /* use correct colorspace here */
954                                 ibuf = IMB_ibImageFromMemory((unsigned char *) ps.picture->mem, ps.picture->size,
955                                                              ps.picture->IB_flags, NULL, ps.picture->name);
956                         }
957                         else {
958                                 /* use correct colorspace here */
959                                 ibuf = IMB_loadiffname(ps.picture->name, ps.picture->IB_flags, NULL);
960                         }
961
962                         if (ibuf) {
963
964 #ifdef USE_IMB_CACHE
965                                 ps.picture->ibuf = ibuf;
966 #endif
967
968                                 BLI_strncpy(ibuf->name, ps.picture->name, sizeof(ibuf->name));
969
970                                 /* why only windows? (from 2.4x) - campbell */
971 #ifdef _WIN32
972                                 GHOST_SetTitle(g_WS.ghost_window, ps.picture->name);
973 #endif
974
975                                 while (pupdate_time()) PIL_sleep_ms(1);
976                                 ptottime -= swaptime;
977                                 playanim_toscreen(ps.picture, ibuf, ps.fontid, ps.fstep);
978                         } /* else deleten */
979                         else {
980                                 printf("error: can't play this image type\n");
981                                 exit(0);
982                         }
983
984                         if (ps.once) {
985                                 if (ps.picture->next == NULL) {
986                                         ps.wait2 = TRUE;
987                                 }
988                                 else if (ps.picture->prev == NULL) {
989                                         ps.wait2 = TRUE;
990                                 }
991                         }
992
993                         ps.next = ps.direction;
994
995
996                         {
997                                 int hasevent = GHOST_ProcessEvents(g_WS.ghost_system, 0);
998                                 if (hasevent) {
999                                         GHOST_DispatchEvents(g_WS.ghost_system);
1000                                 }
1001                         }
1002
1003                         /* XXX25 - we should not have to do this, but it makes scrubbing functional! */
1004                         if (g_WS.qual & WS_QUAL_LMOUSE) {
1005                                 ps.next = 0;
1006                         }
1007                         else {
1008                                 ps.sstep = 0;
1009                         }
1010
1011                         ps.wait2 = ps.sstep;
1012
1013                         if (ps.wait2 == 0 && ps.stopped == 0) {
1014                                 ps.stopped = TRUE;
1015                         }
1016
1017                         pupdate_time();
1018
1019                         if (ps.picture && ps.next) {
1020                                 /* always at least set one step */
1021                                 while (ps.picture) {
1022                                         if (ps.next < 0) {
1023                                                 ps.picture = ps.picture->prev;
1024                                         }
1025                                         else {
1026                                                 ps.picture = ps.picture->next;
1027                                         }
1028
1029                                         if (ps.once && ps.picture != NULL) {
1030                                                 if (ps.picture->next == NULL) {
1031                                                         ps.wait2 = TRUE;
1032                                                 }
1033                                                 else if (ps.picture->prev == NULL) {
1034                                                         ps.wait2 = TRUE;
1035                                                 }
1036                                         }
1037
1038                                         if (ps.wait2 || ptottime < swaptime || ps.turbo || ps.noskip) break;
1039                                         ptottime -= swaptime;
1040                                 }
1041                                 if (ps.picture == NULL && ps.sstep) {
1042                                         if (ps.next < 0) {
1043                                                 ps.picture = picsbase.last;
1044                                         }
1045                                         else if (ps.next > 0) {
1046                                                 ps.picture = picsbase.first;
1047                                         }
1048                                 }
1049                         }
1050                         if (ps.go == FALSE) {
1051                                 break;
1052                         }
1053                 }
1054         }
1055         ps.picture = picsbase.first;
1056         anim = NULL;
1057         while (ps.picture) {
1058                 if (ps.picture && ps.picture->anim && (anim != ps.picture->anim)) {
1059                         // to prevent divx crashes
1060                         anim = ps.picture->anim;
1061                         IMB_close_anim(anim);
1062                 }
1063
1064                 if (ps.picture->ibuf) {
1065                         IMB_freeImBuf(ps.picture->ibuf);
1066                 }
1067                 if (ps.picture->mem) {
1068                         MEM_freeN(ps.picture->mem);
1069                 }
1070
1071                 ps.picture = ps.picture->next;
1072         }
1073
1074         /* cleanup */
1075 #ifndef USE_IMB_CACHE
1076         if (ibuf) IMB_freeImBuf(ibuf);
1077 #endif
1078
1079         BLI_freelistN(&picsbase);
1080 #if 0 // XXX25
1081         free_blender();
1082 #else
1083         /* we still miss freeing a lot!,
1084          * but many areas could skip initialization too for anim play */
1085         IMB_exit();
1086         BKE_images_exit();
1087         BLF_exit();
1088 #endif
1089         GHOST_DisposeWindow(g_WS.ghost_system, g_WS.ghost_window);
1090
1091         totblock = MEM_get_memory_blocks_in_use();
1092         if (totblock != 0) {
1093                 /* prints many bAKey, bArgument's which are tricky to fix */
1094 #if 0
1095                 printf("Error Totblock: %d\n", totblock);
1096                 MEM_printmemlist();
1097 #endif
1098         }
1099 }