1 /*****************************************************************************
2 * This file is part of gfxprim library. *
4 * Gfxprim 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.1 of the License, or (at your option) any later version. *
9 * Gfxprim 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. *
14 * You should have received a copy of the GNU Lesser General Public *
15 * License along with gfxprim; if not, write to the Free Software *
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
17 * Boston, MA 02110-1301 USA *
19 * Copyright (C) 2009-2010 Jiri "BlueBear" Dluhos *
20 * <jiri.bluebear.dluhos@gmail.com> *
22 * Copyright (C) 2009-2010 Cyril Hrubis <metan@ucw.cz> *
24 *****************************************************************************/
30 #include "GP_PXMCommon.h"
32 static int read_comment(FILE *f
)
44 GP_RetCode
GP_PXMLoad1bpp(FILE *f
, GP_Context
*context
)
46 uint8_t *pixel
= context
->pixels
;
49 for (y
= 0; y
< context
->h
; y
++) {
50 for (x
= 0; x
< context
->w
;) {
62 *pixel
&= ~(0x80>>(x
%8));
66 *pixel
|= 0x80>>(x
%8);
87 GP_RetCode
GP_PXMLoad2bpp(FILE *f
, GP_Context
*context
)
89 uint8_t *pixel
= context
->pixels
;
92 for (y
= 0; y
< context
->h
; y
++) {
93 for (x
= 0; x
< context
->w
;) {
105 *pixel
&= ~(0xc0>>(2*(x
%4)));
109 *pixel
&= ~(0x80>>(2*(x
%4)));
110 *pixel
|= 0x40>>(2*(x
%4));
114 *pixel
|= 0x80>>(2*(x
%4));
115 *pixel
&= ~(0x40>>(2*(x
%4)));
119 *pixel
|= 0xc0>>(2*(x
%4));
140 GP_RetCode
GP_PXMLoad4bpp(FILE *f
, GP_Context
*context
)
142 uint8_t *pixel
= context
->pixels
;
145 for (y
= 0; y
< context
->h
; y
++) {
146 for (x
= 0; x
< context
->w
;) {
162 if (ch
>= 0 && ch
<= 5)
167 *pixel
= (*pixel
& 0xf0>>4*(x
%2)) |
186 GP_RetCode
GP_PXMLoad8bpp(FILE *f
, GP_Context
*context
)
188 uint8_t *pixel
= context
->pixels
;
191 for (y
= 0; y
< context
->h
; y
++) {
192 for (x
= 0; x
< context
->w
; x
++) {
198 switch (ch
= fgetc(f
)) {
208 if (ch
>= '0' && ch
<= '9') {
209 val
= val
* 10 + ch
- '0';
211 if (ch
>= '0' && ch
<= '9')
212 val
= val
* 10 + ch
- '0';
234 #define BITMASK(byte, bit) (!!((byte)&(0x80>>(bit))))
236 static GP_RetCode
write_line_1bpp(FILE *f
, const uint8_t *data
, GP_Context
*src
)
238 uint32_t x
, max
= src
->bytes_per_row
;
244 for (x
= 0; x
< max
; x
++) {
247 if (fprintf(f
, " ") < 0)
250 ret
= fprintf(f
, "%u %u %u %u %u %u %u %u",
258 BITMASK(data
[x
], 7));
263 for (x
= 0; x
< (src
->w
% 8); x
++) {
264 ret
= fprintf(f
, " %u", BITMASK(data
[max
], x
));
270 if (fprintf(f
, "\n") < 0)
276 GP_RetCode
GP_PXMSave1bpp(FILE *f
, GP_Context
*context
)
281 for (y
= 0; y
< context
->h
; y
++) {
282 ret
= write_line_1bpp(f
, context
->pixels
+ context
->bytes_per_row
* y
,
292 #define MASK_2BPP(byte, pix) (0x03 & (byte>>((3 - pix)<<1)))
294 static GP_RetCode
write_line_2bpp(FILE *f
, const uint8_t *data
, GP_Context
*src
)
296 uint32_t x
, max
= src
->bytes_per_row
;
302 for (x
= 0; x
< max
; x
++) {
305 if (fprintf(f
, " ") < 0)
308 ret
= fprintf(f
, "%u %u %u %u",
309 MASK_2BPP(data
[x
], 0),
310 MASK_2BPP(data
[x
], 1),
311 MASK_2BPP(data
[x
], 2),
312 MASK_2BPP(data
[x
], 3));
317 for (x
= 0; x
< (src
->w
% 4); x
++) {
318 ret
= fprintf(f
, " %u", MASK_2BPP(data
[max
], x
));
324 if (fprintf(f
, "\n") < 0)
330 GP_RetCode
GP_PXMSave2bpp(FILE *f
, GP_Context
*context
)
335 for (y
= 0; y
< context
->h
; y
++) {
336 ret
= write_line_2bpp(f
, context
->pixels
+ context
->bytes_per_row
* y
,
346 static GP_RetCode
write_line_8bpp(FILE *f
, const uint8_t *data
, GP_Context
*src
)
351 for (x
= 0; x
< src
->w
; x
++) {
354 if (fprintf(f
, " ") < 0)
357 ret
= fprintf(f
, "%u", data
[x
]);
363 if (fprintf(f
, "\n") < 0)
369 GP_RetCode
GP_PXMSave8bpp(FILE *f
, GP_Context
*context
)
374 for (y
= 0; y
< context
->h
; y
++) {
375 ret
= write_line_8bpp(f
, context
->pixels
+ context
->bytes_per_row
* y
,