Initial commit - from Precise source
[freerdp-ubuntu-pcb-backport.git] / libfreerdp-gdi / bitmap.c
1 /**
2  * FreeRDP: A Remote Desktop Protocol Client
3  * GDI Bitmap Functions
4  *
5  * Copyright 2010-2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <stdlib.h>
23 #include <freerdp/api.h>
24 #include <freerdp/freerdp.h>
25 #include <freerdp/gdi/gdi.h>
26 #include <freerdp/codec/color.h>
27
28 #include <freerdp/gdi/32bpp.h>
29 #include <freerdp/gdi/16bpp.h>
30 #include <freerdp/gdi/8bpp.h>
31
32 #include <freerdp/gdi/bitmap.h>
33
34 p_BitBlt BitBlt_[5] =
35 {
36         NULL,
37         BitBlt_8bpp,
38         BitBlt_16bpp,
39         NULL,
40         BitBlt_32bpp
41 };
42
43 /**
44  * Get pixel at the given coordinates.\n
45  * @msdn{dd144909}
46  * @param hdc device context
47  * @param nXPos pixel x position
48  * @param nYPos pixel y position
49  * @return pixel color
50  */
51
52 INLINE GDI_COLOR gdi_GetPixel(HGDI_DC hdc, int nXPos, int nYPos)
53 {
54         HGDI_BITMAP hBmp = (HGDI_BITMAP) hdc->selectedObject;
55         GDI_COLOR* colorp = (GDI_COLOR*)&(hBmp->data[(nYPos * hBmp->width * hdc->bytesPerPixel) + nXPos * hdc->bytesPerPixel]);
56         return (GDI_COLOR) *colorp;
57 }
58
59 INLINE uint8 gdi_GetPixel_8bpp(HGDI_BITMAP hBmp, int X, int Y)
60 {
61         return *((uint8*)&(hBmp->data[(Y * hBmp->width) + X]));
62 }
63
64 INLINE uint16 gdi_GetPixel_16bpp(HGDI_BITMAP hBmp, int X, int Y)
65 {
66         return *((uint16*)&(hBmp->data[(Y * hBmp->width * 2) + X * 2]));
67 }
68
69 INLINE uint32 gdi_GetPixel_32bpp(HGDI_BITMAP hBmp, int X, int Y)
70 {
71         return *((uint32*)&(hBmp->data[(Y * hBmp->width * 4) + X * 4]));
72 }
73
74 INLINE uint8* gdi_GetPointer_8bpp(HGDI_BITMAP hBmp, int X, int Y)
75 {
76         return ((uint8*)&(hBmp->data[(Y * hBmp->width) + X]));
77 }
78
79 INLINE uint16* gdi_GetPointer_16bpp(HGDI_BITMAP hBmp, int X, int Y)
80 {
81         return ((uint16*)&(hBmp->data[(Y * hBmp->width * 2) + X * 2]));
82 }
83
84 INLINE uint32* gdi_GetPointer_32bpp(HGDI_BITMAP hBmp, int X, int Y)
85 {
86         return ((uint32*)&(hBmp->data[(Y * hBmp->width * 4) + X * 4]));
87 }
88
89 /**
90  * Set pixel at the given coordinates.\n
91  * @msdn{dd145078}
92  * @param hdc device context
93  * @param X pixel x position
94  * @param Y pixel y position
95  * @param crColor new pixel color
96  * @return
97  */
98
99 INLINE GDI_COLOR gdi_SetPixel(HGDI_DC hdc, int X, int Y, GDI_COLOR crColor)
100 {
101         HGDI_BITMAP hBmp = (HGDI_BITMAP) hdc->selectedObject;
102         *((GDI_COLOR*)&(hBmp->data[(Y * hBmp->width * hdc->bytesPerPixel) + X * hdc->bytesPerPixel])) = crColor;
103         return 0;
104 }
105
106 INLINE void gdi_SetPixel_8bpp(HGDI_BITMAP hBmp, int X, int Y, uint8 pixel)
107 {
108         *((uint8*)&(hBmp->data[(Y * hBmp->width) + X])) = pixel;
109 }
110
111 INLINE void gdi_SetPixel_16bpp(HGDI_BITMAP hBmp, int X, int Y, uint16 pixel)
112 {
113         *((uint16*)&(hBmp->data[(Y * hBmp->width * 2) + X * 2])) = pixel;
114 }
115
116 INLINE void gdi_SetPixel_32bpp(HGDI_BITMAP hBmp, int X, int Y, uint32 pixel)
117 {
118         *((uint32*)&(hBmp->data[(Y * hBmp->width * 4) + X * 4])) = pixel;
119 }
120
121 /**
122  * Create a new bitmap with the given width, height, color format and pixel buffer.\n
123  * @msdn{dd183485}
124  * @param nWidth width
125  * @param nHeight height
126  * @param cBitsPerPixel bits per pixel
127  * @param data pixel buffer
128  * @return new bitmap
129  */
130
131 HGDI_BITMAP gdi_CreateBitmap(int nWidth, int nHeight, int cBitsPerPixel, uint8* data)
132 {
133         HGDI_BITMAP hBitmap = (HGDI_BITMAP) malloc(sizeof(GDI_BITMAP));
134         hBitmap->objectType = GDIOBJECT_BITMAP;
135         hBitmap->bitsPerPixel = cBitsPerPixel;
136         hBitmap->bytesPerPixel = (cBitsPerPixel + 1) / 8;
137         hBitmap->scanline = nWidth * hBitmap->bytesPerPixel;
138         hBitmap->width = nWidth;
139         hBitmap->height = nHeight;
140         hBitmap->data = data;
141         return hBitmap;
142 }
143
144 /**
145  * Create a new bitmap of the given width and height compatible with the current device context.\n
146  * @msdn{dd183488}
147  * @param hdc device context
148  * @param nWidth width
149  * @param nHeight height
150  * @return new bitmap
151  */
152
153 HGDI_BITMAP gdi_CreateCompatibleBitmap(HGDI_DC hdc, int nWidth, int nHeight)
154 {
155         HGDI_BITMAP hBitmap = (HGDI_BITMAP) malloc(sizeof(GDI_BITMAP));
156         hBitmap->objectType = GDIOBJECT_BITMAP;
157         hBitmap->bytesPerPixel = hdc->bytesPerPixel;
158         hBitmap->bitsPerPixel = hdc->bitsPerPixel;
159         hBitmap->width = nWidth;
160         hBitmap->height = nHeight;
161         hBitmap->data = malloc(nWidth * nHeight * hBitmap->bytesPerPixel);
162         hBitmap->scanline = nWidth * hBitmap->bytesPerPixel;
163         return hBitmap;
164 }
165
166 /**
167  * Perform a bit blit operation on the given pixel buffers.\n
168  * @msdn{dd183370}
169  * @param hdcDest destination device context
170  * @param nXDest destination x1
171  * @param nYDest destination y1
172  * @param nWidth width
173  * @param nHeight height
174  * @param hdcSrc source device context
175  * @param nXSrc source x1
176  * @param nYSrc source y1
177  * @param rop raster operation code
178  * @return 1 if successful, 0 otherwise
179  */
180
181 int gdi_BitBlt(HGDI_DC hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, HGDI_DC hdcSrc, int nXSrc, int nYSrc, int rop)
182 {
183         p_BitBlt _BitBlt = BitBlt_[IBPP(hdcDest->bitsPerPixel)];
184
185         if (_BitBlt != NULL)
186                 return _BitBlt(hdcDest, nXDest, nYDest, nWidth, nHeight, hdcSrc, nXSrc, nYSrc, rop);
187         else
188                 return 0;
189 }