]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/pixops/timescale.c
945cf8003e3562ebdbddfa8c7243429a3fb64525
[~andy/gtk] / gdk-pixbuf / pixops / timescale.c
1 /*
2  * Copyright (C) 2000 Red Hat, Inc
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library 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 GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 #include <config.h>
20 #include <glib.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24
25 #include "pixops.h"
26
27 static GTimeVal start_time;
28
29 void start_timing (void)
30 {
31   g_get_current_time (&start_time);
32 }
33
34 double
35 stop_timing (const char *test, int iterations, int bytes)
36 {
37   GTimeVal stop_time;
38   double msecs;
39   
40   g_get_current_time (&stop_time);
41   if (stop_time.tv_usec < start_time.tv_usec)
42     {
43       stop_time.tv_usec += 1000000;
44       stop_time.tv_sec -= 1;
45     }
46
47   msecs = (stop_time.tv_sec - start_time.tv_sec) * 1000. +
48           (stop_time.tv_usec - start_time.tv_usec) / 1000.;
49
50   printf("%s%d\t%.1f\t\t%.2f\t\t%.2f\n",
51          test, iterations, msecs, msecs / iterations, ((double)bytes * iterations) / (1000*msecs));
52
53   return ((double)bytes * iterations) / (1000*msecs);
54 }
55
56 void
57 init_array (double times[3][3][4])
58 {
59   int i, j, k;
60   
61   for (i=0; i<3; i++)
62     for (j=0; j<3; j++)
63       for (k=0; k<4; k++)
64         times[i][j][k] = -1;
65 }
66
67 void
68 dump_array (double times[3][3][4])
69 {
70   int i, j;
71   
72   printf("        3\t4\t4a\n");
73   for (i=0; i<3; i++)
74     {
75       for (j=0; j<4; j++)
76         {
77           if (j == 0)
78             switch (i)
79               {
80               case 0:
81                 printf("3  ");
82                 break;
83               case 1:
84                 printf("4  ");
85                 break;
86               case 2:
87                 printf("4a ");
88                 break;
89               }
90           else
91             printf("   ");
92
93           printf("%6.2f  %6.2f   %6.2f",
94                  times[i][0][j], times[i][1][j], times[i][2][j]);
95
96           switch (j)
97             {
98             case PIXOPS_INTERP_NEAREST:
99               printf ("  NEAREST\n");
100               break;
101             case PIXOPS_INTERP_TILES:
102               printf ("  TILES\n");
103               break;
104             case PIXOPS_INTERP_BILINEAR:
105               printf ("  BILINEAR\n");
106               break;
107             case PIXOPS_INTERP_HYPER:
108               printf ("  HYPER\n");
109               break;
110             }
111         }
112     }
113   printf("\n");
114 }
115
116 #define ITERS 10
117
118 int main (int argc, char **argv)
119 {
120   int src_width, src_height, dest_width, dest_height;
121   unsigned char *src_buf, *dest_buf;
122   int src_index, dest_index;
123   int i;
124   double scale_times[3][3][4];
125   double composite_times[3][3][4];
126   double composite_color_times[3][3][4];
127
128   if (argc == 5)
129     {
130       src_width = atoi(argv[1]);
131       src_height = atoi(argv[2]);
132       dest_width = atoi(argv[3]);
133       dest_height = atoi(argv[4]);
134     }
135   else if (argc == 1)
136     {
137       src_width = 343;
138       src_height = 343;
139       dest_width = 711;
140       dest_height = 711;
141     }
142   else
143     {
144       fprintf (stderr, "Usage: scale [src_width src_height dest_width dest_height]\n");
145       exit(1);
146     }
147
148
149   printf ("Scaling from (%d, %d) to (%d, %d)\n\n", src_width, src_height, dest_width, dest_height);
150
151   init_array (scale_times);
152   init_array (composite_times);
153   init_array (composite_color_times);
154
155   for (src_index = 0; src_index < 3; src_index++)
156     for (dest_index = 0; dest_index < 3; dest_index++)
157       {
158         int src_channels = (src_index == 0) ? 3 : 4;
159         int src_has_alpha = (src_index == 2);
160         int dest_channels = (dest_index == 0) ? 3 : 4;
161         int dest_has_alpha = (dest_index == 2);
162         
163         int src_rowstride = (src_channels*src_width + 3) & ~3;
164         int dest_rowstride = (dest_channels *dest_width + 3) & ~3;
165
166         int filter_level;
167
168         src_buf = g_malloc(src_rowstride * src_height);
169         memset (src_buf, 0x80, src_rowstride * src_height);
170         
171         dest_buf = g_malloc(dest_rowstride * dest_height);
172         memset (dest_buf, 0x80, dest_rowstride * dest_height);
173
174         for (filter_level = PIXOPS_INTERP_NEAREST ; filter_level <= PIXOPS_INTERP_HYPER; filter_level++)
175           {
176             printf ("src_channels = %d (%s); dest_channels = %d (%s); filter_level=",
177                     src_channels, src_has_alpha ? "alpha" : "no alpha",
178                     dest_channels, dest_has_alpha ? "alpha" : "no alpha");
179             switch (filter_level)
180               {
181               case PIXOPS_INTERP_NEAREST:
182                 printf ("PIXOPS_INTERP_NEAREST\n");
183                 break;
184               case PIXOPS_INTERP_TILES:
185                 printf ("PIXOPS_INTERP_TILES\n");
186                 break;
187               case PIXOPS_INTERP_BILINEAR:
188                 printf ("PIXOPS_INTERP_BILINEAR\n");
189                 break;
190               case PIXOPS_INTERP_HYPER:
191                 printf ("PIXOPS_INTERP_HYPER\n");
192                 break;
193               }
194
195             printf("\t\t\titers\ttotal\t\tmsecs/iter\tMpixels/sec\t\n");
196
197
198             if (!(src_has_alpha && !dest_has_alpha))
199               {
200                 start_timing ();
201                 for (i = 0; i < ITERS; i++)
202                   {
203                     _pixops_scale (dest_buf, 0, 0, dest_width, dest_height, dest_rowstride, dest_channels, dest_has_alpha,
204                                    src_buf, src_width, src_height, src_rowstride, src_channels, src_has_alpha,
205                                    (double)dest_width / src_width, (double)dest_height / src_height,
206                                    filter_level);
207                   }
208                 scale_times[src_index][dest_index][filter_level] =
209                   stop_timing ("   scale\t\t", ITERS, dest_height * dest_width);
210               }
211
212             start_timing ();
213             for (i = 0; i < ITERS; i++)
214               {
215                 _pixops_composite (dest_buf, 0, 0, dest_width, dest_height, dest_rowstride, dest_channels, dest_has_alpha,
216                                    src_buf, src_width, src_height, src_rowstride, src_channels, src_has_alpha,
217                                    (double)dest_width / src_width, (double)dest_height / src_height,
218                                    filter_level, 255);
219               }
220             composite_times[src_index][dest_index][filter_level] =
221               stop_timing ("   composite\t\t", ITERS, dest_height * dest_width);
222
223             start_timing ();
224             for (i = 0; i < ITERS; i++)
225               {
226                 _pixops_composite_color (dest_buf, 0, 0, dest_width, dest_height, dest_rowstride, dest_channels, dest_has_alpha,
227                                          src_buf, src_width, src_height, src_rowstride, src_channels, src_has_alpha,
228                                          (double)dest_width / src_width, (double)dest_height / src_height,
229                                          filter_level, 255, 0, 0, 16, 0xaaaaaa, 0x555555);
230               }
231             composite_color_times[src_index][dest_index][filter_level] =
232               stop_timing ("   composite color\t", ITERS, dest_height * dest_width);
233
234             printf ("\n");
235           }
236         printf ("\n");
237
238         g_free (src_buf);
239         g_free (dest_buf);
240       }
241
242   printf ("SCALE\n=====\n\n");
243   dump_array (scale_times);
244
245   printf ("COMPOSITE\n=========\n\n");
246   dump_array (composite_times);
247
248   printf ("COMPOSITE_COLOR\n===============\n\n");
249   dump_array (composite_color_times);
250   return 0;
251 }