summaryrefslogtreecommitdiffstats
path: root/vp8/decoder/xprintf.c
blob: cb2221c150a9f6b6edbbda3b3be9a5ed3fa403ed (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
/*
 *  Copyright (c) 2010 The VP8 project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license and patent
 *  grant that can be found in the LICENSE file in the root of the source
 *  tree. All contributing project authors may be found in the AUTHORS
 *  file in the root of the source tree.
 */


/****************************************************************************
*
*   Module Title :     xprintf.cpp
*
*   Description  :     Display a printf style message on the current video frame.
*
****************************************************************************/

/****************************************************************************
*  Header Files
****************************************************************************/

#include <stdio.h>
#include <stdarg.h>
#ifdef _WIN32_WCE
#include <windows.h>
#endif
#include "xprintf.h"

/****************************************************************************
 *
 *  ROUTINE       : xprintf
 *
 *  INPUTS        : const PB_INSTANCE *ppbi : Pointer to decoder instance.
 *                  long n_pixel             : Offset into buffer to write text.
 *                  const char *format      : Format string for print.
 *                  ...                     : Variable length argument list.
 *
 *  OUTPUTS       : None.
 *
 *  RETURNS       : int: Size (in bytes) of the formatted text.
 *
 *  FUNCTION      : Display a printf style message on the current video frame.
 *
 *  SPECIAL NOTES : None.
 *
 ****************************************************************************/
int onyx_xprintf(unsigned char *ppbuffer, long n_pixel, long n_size, long n_stride, const char *format, ...)
{
    BOOL b_rc;
    va_list arglist;
    HFONT hfont, hfonto;

    int rc = 0;
    char sz_formatted[256] = "";
    unsigned char *p_dest = &ppbuffer[n_pixel];

#ifdef _WIN32_WCE
    //  Set up temporary bitmap
    HDC hdc_memory   = NULL;
    HBITMAP hbm_temp = NULL;
    HBITMAP hbm_orig = NULL;

    RECT rect;

    //  Copy bitmap to video frame
    long x;
    long y;

    //  Format text
    va_start(arglist, format);
    _vsnprintf(sz_formatted, sizeof(sz_formatted), format, arglist);
    va_end(arglist);

    rect.left   = 0;
    rect.top    = 0;
    rect.right  = 8 * strlen(sz_formatted);
    rect.bottom = 8;

    hdc_memory = create_compatible_dc(NULL);

    if (hdc_memory == NULL)
        goto Exit;

    hbm_temp = create_bitmap(rect.right, rect.bottom, 1, 1, NULL);

    if (hbm_temp == NULL)
        goto Exit;

    hbm_orig = (HBITMAP)(select_object(hdc_memory, hbm_temp));

    if (!hbm_orig)
        goto Exit;

    //  Write text into bitmap
    //  font?
    hfont = create_font(8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, VARIABLE_PITCH | FF_SWISS, "");

    if (hfont == NULL)
        goto Exit;

    hfonto = (HFONT)(select_object(hdc_memory, hbm_temp));

    if (!hfonto)
        goto Exit;

    select_object(hdc_memory, hfont);
    set_text_color(hdc_memory, 1);
    set_bk_color(hdc_memory, 0);
    set_bk_mode(hdc_memory, TRANSPARENT);

    b_rc = bit_blt(hdc_memory, rect.left, rect.top, rect.right, rect.bottom, hdc_memory, rect.left, rect.top, BLACKNESS);

    if (!b_rc)
        goto Exit;

    b_rc = ext_text_out(hdc_memory, 0, 0, ETO_CLIPPED, &rect, sz_formatted, strlen(sz_formatted), NULL);

    if (!b_rc)
        goto Exit;

    for (y = rect.top; y < rect.bottom; ++y)
    {
        for (x = rect.left; x < rect.right; ++x)
        {
            if (get_pixel(hdc_memory, x, rect.bottom - 1 - y))
                p_dest[x] = 255;
        }

        p_dest += n_stride;
    }

    rc = strlen(sz_formatted);

Exit:

    if (hbm_temp != NULL)
    {
        if (hbm_orig != NULL)
        {
            select_object(hdc_memory, hbm_orig);
        }

        delete_object(hbm_temp);
    }

    if (hfont != NULL)
    {
        if (hfonto != NULL)
            select_object(hdc_memory, hfonto);

        delete_object(hfont);
    }

    if (hdc_memory != NULL)
        delete_dc(hdc_memory);

    hdc_memory = 0;

#endif

    return rc;
}