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