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