summaryrefslogtreecommitdiff
path: root/Build/source/libs/freetype/freetype-1.5/test/blitter.c
blob: 66926e1ee46154ad5d89be050d34fbd13b3fc2b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/****************************************************************************/
/*                                                                          */
/*  The FreeType project -- a free and portable quality TrueType renderer.  */
/*                                                                          */
/*  Copyright 1996-2000 by                                                  */
/*  D. Turner, R.Wilhelm, and W. Lemberg                                    */
/*                                                                          */
/*  blitter.c: Support for blitting of bitmaps with various depth.          */
/*                                                                          */
/****************************************************************************/

#include "blitter.h"


  typedef struct TBlitter_
  {
    int  width;   /* width in pixels of the written area  */
    int  height;  /* height in pixels of the written area */

    int  xread;   /* x position of start point in read area */
    int  yread;   /* y position of start point in read area */

    int  xwrite;  /* x position of start point in write area */
    int  ywrite;  /* y position of start point in write area */

    int  right_clip;   /* amount of right clip               */

    char*  read;       /* top left corner of source map */
    char*  write;      /* top left corner of target map */

    int    read_line;  /* byte increment to go down one row in read area  */
    int    write_line; /* byte increment to go down one row in write area */

    TT_Raster_Map  source;
    TT_Raster_Map  target;

    int  source_depth;
    int  target_depth;

  } TBlitter;


  static
  int  compute_clips( TBlitter*  blit,
                      int        x_offset,
                      int        y_offset )
  {
    int  xmin, ymin, xmax, ymax, width, height, target_width;


    /* perform clipping and setup variables */
    width  = blit->source.width;
    height = blit->source.rows;

    switch ( blit->source_depth )
    {
    case 1:
      width = (width + 7) & -8;
      break;

    case 4:
      width = (width + 1) & -2;
      break;
    }

    xmin = x_offset;
    ymin = y_offset;
    xmax = xmin + width-1;
    ymax = ymin + height-1;

    /* clip if necessary */
    if ( width == 0 || height == 0                ||
         xmax < 0   || xmin >= blit->target.width ||
         ymax < 0   || ymin >= blit->target.rows  )
      return 1;

    /* set up clipping and cursors */
    blit->yread = 0;
    if ( ymin < 0 )
    {
      blit->yread  -= ymin;
      height       += ymin;
      blit->ywrite  = 0;
    }
    else
      blit->ywrite  = ymin;

    if ( ymax >= blit->target.rows )
      height -= ymax - blit->target.rows + 1;

    blit->xread = 0;
    if ( xmin < 0 )
    {
      blit->xread  -= xmin;
      width        += xmin;
      blit->xwrite  = 0;
    }
    else
      blit->xwrite  = xmin;

    target_width = blit->target.width;

    switch ( blit->target_depth )
    {
    case 1:
      target_width = (target_width + 7) & -8;
      break;
    case 4:
      target_width = (target_width + 1) & -2;
      break;
    }

    blit->right_clip = xmax - target_width + 1;
    if ( blit->right_clip > 0 )
      width -= blit->right_clip;
    else
      blit->right_clip = 0;

    blit->width  = width;
    blit->height = height;

    /* set read and write to the top-left corner of the the read */
    /* and write area before clipping.                           */

    blit->read  = (char*)blit->source.bitmap;
    blit->write = (char*)blit->target.bitmap;

    if ( blit->source.flow == TT_Flow_Up )
    {
      blit->read_line = -blit->source.cols;
      blit->read     += (blit->source.rows-1) * blit->source.cols;
    }
    else
      blit->read_line =  blit->source.cols;

    if ( blit->target.flow == TT_Flow_Up )
    {
      blit->write_line = -blit->target.cols;
      blit->write     += (blit->target.rows-1) * blit->target.cols;
    }
    else
      blit->write_line =  blit->target.cols;

    /* now go to the start line. Note that we do not move the   */
    /* x position yet, as this is dependent on the pixel format */
    blit->read  += blit->yread * blit->read_line;
    blit->write += blit->ywrite * blit->write_line;

    return 0;
  }


/**************************************************************************/
/*                                                                        */
/* <Function> blit_bitmap_to_bitmap                                       */
/*                                                                        */
/**************************************************************************/

  static
  void  blit_bitmap_to_bitmap( TBlitter*  blit )
  {
    int             shift, left_clip, x, y;
    unsigned char*  read;
    unsigned char*  write;


    left_clip = ( blit->xread > 0 );
    shift     = ( blit->xwrite - blit->xread ) & 7;

    read  = (unsigned char*)blit->read  + (blit->xread >> 3);
    write = (unsigned char*)blit->write + (blit->xwrite >> 3);

    if ( shift == 0 )
    {
      y = blit->height;
      do
      {
        unsigned char*  _read  = read;
        unsigned char*  _write = write;


        x = blit->width;

        do
        {
          *_write++ |= *_read++;
          x -= 8;
        } while ( x > 0 );

        read  += blit->read_line;
        write += blit->write_line;
        y--;
      } while ( y > 0 );
    }
    else
    {
      int  first, last, count;


      first = blit->xwrite >> 3;
      last  = (blit->xwrite + blit->width-1) >> 3;

      count = last - first;

      if ( blit->right_clip )
        count++;

      y = blit->height;

      do
      {
        unsigned char*  _read  = read;
        unsigned char*  _write = write;
        unsigned char   old;
        int             shift2 = (8-shift);


        if ( left_clip )
          old = (*_read++) << shift2;
        else
          old = 0;

        x = count;
        while ( x > 0 )
        {
          unsigned char val;


          val = *_read++;
          *_write++ |= ( (val >> shift) | old );
          old = val << shift2;
          x--;
        }

        if ( !blit->right_clip )
          *_write |= old;

        read  += blit->read_line;
        write += blit->write_line;
        y--;

      } while ( y > 0 );
    }
  }


/**************************************************************************/
/*                                                                        */
/* <Function> blit_bitmap_to_pixmap8                                      */
/*                                                                        */
/**************************************************************************/

  static
  void  blit_bitmap_to_pixmap8( TBlitter*      blit,
                                unsigned char  color )
  {
    int             x, y;
    unsigned int    left_mask;
    unsigned char*  read;
    unsigned char*  write;


    read  = (unsigned char*)blit->read  + (blit->xread >> 3);
    write = (unsigned char*)blit->write +  blit->xwrite;

    left_mask = 0x80 >> (blit->xread & 7);

    y = blit->height;
    do
    {
      unsigned char*  _read  = read;
      unsigned char*  _write = write;
      unsigned int    mask   = left_mask;
      unsigned int    val    = 0;


      x = blit->width;
      do
      {
        if ( mask == 0x80 )
          val = *_read++;

        if ( val & mask )
          *_write = (unsigned char)color;

        mask >>= 1;
        if ( mask == 0 )
          mask = 0x80;

        _write++;
        x--;
      } while ( x > 0 );

      read  += blit->read_line;
      write += blit->write_line;
      y--;
    } while ( y > 0 );
  }


/**************************************************************************/
/*                                                                        */
/* <Function> blit_bitmap_to_pixmap4                                      */
/*                                                                        */
/**************************************************************************/

  static
  void  blit_bitmap_to_pixmap4( TBlitter*      blit,
                                unsigned char  color )
  {
    int             x, y, phase;
    unsigned int    left_mask;
    unsigned char*  read;
    unsigned char*  write;


    color = color & 15;

    read  = (unsigned char*)blit->read  + (blit->xread >> 3);
    write = (unsigned char*)blit->write + (blit->xwrite >> 1);

    /* now begin blit */
    left_mask = 0x80 >> (blit->xread & 7);
    phase     = blit->xwrite & 1;

    y = blit->height;
    do
    {
      unsigned char*  _read  = read;
      unsigned char*  _write = write;
      unsigned int    mask   = left_mask;
      int             _phase = phase;
      unsigned int    val    = 0;

      x = blit->width;
      do
      {
        if ( mask == 0x80 )
          val = *_read++;

        if ( val & mask )
        {
          if ( _phase )
            *_write = (*_write & 0xF0) | color;
          else
            *_write = (*_write & 0x0F) | (color << 4);
        }

        mask >>= 1;
        if ( mask == 0 )
          mask = 0x80;

        _write += _phase;
        _phase ^= 1;
        x--;
      } while ( x > 0 );

      read  += blit->read_line;
      write += blit->write_line;
      y--;
    } while ( y > 0 );
  }


/**************************************************************************/
/*                                                                        */
/* <Function> blit_bitmap_to_pixmap16                                     */
/*                                                                        */
/**************************************************************************/

  static
  void  blit_bitmap_to_pixmap16( TBlitter*       blit,
                                 unsigned short  color )
  {
    int              x, y;
    unsigned int     left_mask;
    unsigned char*   read;
    unsigned short*  write;


    read  = (unsigned char*)blit->read + (blit->xread >> 3);
    write = (unsigned short*)(blit->write + blit->xwrite*2);

    left_mask = 0x80 >> (blit->xread & 7);

    y = blit->height;
    do
    {
      unsigned char*   _read  = read;
      unsigned short*  _write = write;
      unsigned int     mask   = left_mask;
      unsigned int     val    = 0;

      x = blit->width;
      do
      {
        if ( mask == 0x80 )
          val = *_read++;

        if ( val & mask )
          *_write = color;

        mask >>= 1;
        if ( mask == 0 )
          mask = 0x80;

        _write++;
        x--;
      } while ( x > 0 );

      read  += blit->read_line;
      write += blit->write_line;
      y--;
    } while ( y > 0 );
  }


  /* <Function>                                                          */
  /*    Blit_Bitmap                                                      */
  /*                                                                     */
  /* <Description>                                                       */
  /*    blit a source bitmap to a target bitmap or pixmap                */
  /*                                                                     */
  /* <Input>                                                             */
  /*    target       :: target bitmap or pixmap                          */
  /*    source       :: source bitmap (depth must be 1)                  */
  /*    target_depth :: pixel bit depth of target map                    */
  /*    x_offset     :: horizontal offset of source in target            */
  /*    y_offset     :: vertical offset of source in target              */
  /*    color        :: color to use when blitting to color pixmap       */
  /*                                                                     */
  /* <Return>                                                            */
  /*    error code. 0 means success                                      */
  /*                                                                     */
  /* <Note>                                                              */
  /*    an error occurs when the target bit depth isn't supported, or    */
  /*    if the source's bit depth isn't 1.                               */
  /*                                                                     */
  /*    the offsets are relative to the top-left corner of the target    */
  /*    map. Positive y are downwards.                                   */
  /*                                                                     */
  extern
  int  Blit_Bitmap( TT_Raster_Map*  target,
                    TT_Raster_Map*  source,
                    int             target_depth,
                    int             x_offset,
                    int             y_offset,
                    int             color )
  {
    TBlitter  blit;


    if ( !target || !source )
      return -1;

    blit.source       = *source;
    blit.target       = *target;
    blit.source_depth = 1;
    blit.target_depth = target_depth;

    /* set up blitter and compute clipping. Return immediately if needed */
    if ( compute_clips( &blit, x_offset, y_offset ) )
      return 0;

    /* now perform the blit */
    switch ( target_depth )
    {
    case 1:
      blit_bitmap_to_bitmap( &blit );
      break;

    case 4:
      blit_bitmap_to_pixmap4( &blit, (unsigned char)color );
      break;

    case 8:
      blit_bitmap_to_pixmap8( &blit, (unsigned char)color );
      break;

    case 16:
      blit_bitmap_to_pixmap16( &blit, (unsigned short)color );
      break;

    default:
      return -2;
    }

    return 0;
  }


/* End */