~oem-solutions-group/unity-2d/clutter-1.0

« back to all changes in this revision

Viewing changes to clutter/cogl/common/cogl-bitmap.c

  • Committer: Bazaar Package Importer
  • Author(s): Emilio Pozuelo Monfort
  • Date: 2010-03-21 13:27:56 UTC
  • mto: (2.1.3 experimental)
  • mto: This revision was merged to the branch mainline in revision 8.
  • Revision ID: james.westby@ubuntu.com-20100321132756-nf8yd30yxo3zzwcm
Tags: upstream-1.2.2
ImportĀ upstreamĀ versionĀ 1.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Cogl
3
 
 *
4
 
 * An object oriented GL/GLES Abstraction/Utility Layer
5
 
 *
6
 
 * Copyright (C) 2007,2008,2009 Intel Corporation.
7
 
 *
8
 
 * This library is free software; you can redistribute it and/or
9
 
 * modify it under the terms of the GNU Lesser General Public
10
 
 * License as published by the Free Software Foundation; either
11
 
 * version 2 of the License, or (at your option) any later version.
12
 
 *
13
 
 * This library is distributed in the hope that it will be useful,
14
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 
 * Lesser General Public License for more details.
17
 
 *
18
 
 * You should have received a copy of the GNU Lesser General Public
19
 
 * License along with this library; if not, write to the
20
 
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21
 
 * Boston, MA 02111-1307, USA.
22
 
 */
23
 
 
24
 
#ifdef HAVE_CONFIG_H
25
 
#include "config.h"
26
 
#endif
27
 
 
28
 
#include "cogl.h"
29
 
#include "cogl-internal.h"
30
 
#include "cogl-bitmap-private.h"
31
 
 
32
 
#include <string.h>
33
 
 
34
 
static void _cogl_bitmap_free (CoglBitmap *bmp);
35
 
 
36
 
COGL_HANDLE_DEFINE (Bitmap, bitmap);
37
 
 
38
 
static void
39
 
_cogl_bitmap_free (CoglBitmap *bmp)
40
 
{
41
 
  g_free (bmp->data);
42
 
  g_free (bmp);
43
 
}
44
 
 
45
 
gint
46
 
_cogl_get_format_bpp (CoglPixelFormat format)
47
 
{
48
 
  gint bpp_lut[] = {
49
 
    0, /* invalid  */
50
 
    1, /* A_8      */
51
 
    3, /* 888      */
52
 
    4, /* 8888     */
53
 
    2, /* 565      */
54
 
    2, /* 4444     */
55
 
    2, /* 5551     */
56
 
    2, /* YUV      */
57
 
    1  /* G_8      */
58
 
  };
59
 
 
60
 
  return bpp_lut [format & COGL_UNORDERED_MASK];
61
 
}
62
 
 
63
 
gboolean
64
 
_cogl_bitmap_convert_and_premult (const CoglBitmap *bmp,
65
 
                                  CoglBitmap       *dst_bmp,
66
 
                                  CoglPixelFormat   dst_format)
67
 
{
68
 
  CoglBitmap  tmp_bmp = *bmp;
69
 
  CoglBitmap  new_bmp = *bmp;
70
 
  gboolean    new_bmp_owner = FALSE;
71
 
 
72
 
  /* Is base format different (not considering premult status)? */
73
 
  if ((bmp->format & COGL_UNPREMULT_MASK) !=
74
 
      (dst_format & COGL_UNPREMULT_MASK))
75
 
    {
76
 
      /* Try converting using imaging library */
77
 
      if (!_cogl_bitmap_convert (&new_bmp, &tmp_bmp, dst_format))
78
 
        {
79
 
          /* ... or try fallback */
80
 
          if (!_cogl_bitmap_fallback_convert (&new_bmp, &tmp_bmp, dst_format))
81
 
            return FALSE;
82
 
        }
83
 
 
84
 
      /* Update bitmap with new data */
85
 
      new_bmp = tmp_bmp;
86
 
      new_bmp_owner = TRUE;
87
 
    }
88
 
 
89
 
  /* Do we need to unpremultiply */
90
 
  if ((bmp->format & COGL_PREMULT_BIT) > 0 &&
91
 
      (dst_format & COGL_PREMULT_BIT) == 0)
92
 
    {
93
 
      /* Try unpremultiplying using imaging library */
94
 
      if (!_cogl_bitmap_unpremult (&new_bmp, &tmp_bmp))
95
 
        {
96
 
          /* ... or try fallback */
97
 
          if (!_cogl_bitmap_fallback_unpremult (&new_bmp, &tmp_bmp))
98
 
            {
99
 
              if (new_bmp_owner)
100
 
                g_free (new_bmp.data);
101
 
 
102
 
              return FALSE;
103
 
            }
104
 
        }
105
 
 
106
 
      /* Update bitmap with new data */
107
 
      if (new_bmp_owner)
108
 
        g_free (new_bmp.data);
109
 
 
110
 
      new_bmp = tmp_bmp;
111
 
      new_bmp_owner = TRUE;
112
 
    }
113
 
 
114
 
  /* Do we need to premultiply */
115
 
  if ((bmp->format & COGL_PREMULT_BIT) == 0 &&
116
 
      (dst_format & COGL_PREMULT_BIT) > 0)
117
 
    {
118
 
      /* Try premultiplying using imaging library */
119
 
      if (!_cogl_bitmap_premult (&new_bmp, &tmp_bmp))
120
 
        {
121
 
          /* ... or try fallback */
122
 
          if (!_cogl_bitmap_fallback_premult (&new_bmp, &tmp_bmp))
123
 
            {
124
 
              if (new_bmp_owner)
125
 
                g_free (new_bmp.data);
126
 
 
127
 
              return FALSE;
128
 
            }
129
 
        }
130
 
 
131
 
      /* Update bitmap with new data */
132
 
      if (new_bmp_owner)
133
 
        g_free (new_bmp.data);
134
 
 
135
 
      new_bmp = tmp_bmp;
136
 
      new_bmp_owner = TRUE;
137
 
    }
138
 
 
139
 
  /* Output new bitmap info */
140
 
  *dst_bmp = new_bmp;
141
 
 
142
 
  return TRUE;
143
 
}
144
 
 
145
 
void
146
 
_cogl_bitmap_copy_subregion (CoglBitmap *src,
147
 
                             CoglBitmap *dst,
148
 
                             gint        src_x,
149
 
                             gint        src_y,
150
 
                             gint        dst_x,
151
 
                             gint        dst_y,
152
 
                             gint        width,
153
 
                             gint        height)
154
 
{
155
 
  guchar *srcdata;
156
 
  guchar *dstdata;
157
 
  gint    bpp;
158
 
  gint    line;
159
 
 
160
 
  /* Intended only for fast copies when format is equal! */
161
 
  g_assert (src->format == dst->format);
162
 
  bpp = _cogl_get_format_bpp (src->format);
163
 
 
164
 
  srcdata = src->data + src_y * src->rowstride + src_x * bpp;
165
 
  dstdata = dst->data + dst_y * dst->rowstride + dst_x * bpp;
166
 
 
167
 
  for (line=0; line<height; ++line)
168
 
    {
169
 
      memcpy (dstdata, srcdata, width * bpp);
170
 
      srcdata += src->rowstride;
171
 
      dstdata += dst->rowstride;
172
 
    }
173
 
}
174
 
 
175
 
gboolean
176
 
cogl_bitmap_get_size_from_file (const gchar *filename,
177
 
                                gint        *width,
178
 
                                gint        *height)
179
 
{
180
 
  return _cogl_bitmap_get_size_from_file (filename, width, height);
181
 
}
182
 
 
183
 
CoglHandle
184
 
cogl_bitmap_new_from_file (const gchar    *filename,
185
 
                           GError        **error)
186
 
{
187
 
  CoglBitmap   bmp;
188
 
  CoglBitmap  *ret;
189
 
 
190
 
  g_return_val_if_fail (error == NULL || *error == NULL, COGL_INVALID_HANDLE);
191
 
 
192
 
  /* Try loading with imaging backend */
193
 
  if (!_cogl_bitmap_from_file (&bmp, filename, error))
194
 
    {
195
 
      /* Try fallback */
196
 
      if (!_cogl_bitmap_fallback_from_file (&bmp, filename))
197
 
        return NULL;
198
 
      else if (error && *error)
199
 
        {
200
 
          g_error_free (*error);
201
 
          *error = NULL;
202
 
        }
203
 
    }
204
 
 
205
 
  ret = g_memdup (&bmp, sizeof (CoglBitmap));
206
 
  return _cogl_bitmap_handle_new (ret);
207
 
}
208