This is an automated email from the git hooks/post-receive script. It was generated because a ref change was pushed to the repository containing the project gfxprim.git.
The branch, master has been updated via 93832f616ef3ea2dde888cf8e0b7e1a2da077d06 (commit) from ac5ab1fc384840bd63b923cd8d02043176676d01 (commit)
Those revisions listed above that are new to this repository have not appeared on any other notification email; so we list those revisions in full, below.
- Log ----------------------------------------------------------------- http://repo.or.cz/w/gfxprim.git/commit/93832f616ef3ea2dde888cf8e0b7e1a2da077...
commit 93832f616ef3ea2dde888cf8e0b7e1a2da077d06 Author: Cyril Hrubis metan@ucw.cz Date: Fri May 2 23:29:31 2014 +0200
loaders: BMP: Use IO for saving.
Adds GP_IOWriteF() and converts BMP saver to it.
There is also new GP_WriteBMP() function that takes opened IO instead of a path as a parameter.
Signed-off-by: Cyril Hrubis metan@ucw.cz
diff --git a/build/syms/Loaders_symbols.txt b/build/syms/Loaders_symbols.txt index f8c1d68c..b73506dd 100644 --- a/build/syms/Loaders_symbols.txt +++ b/build/syms/Loaders_symbols.txt @@ -15,6 +15,7 @@ GP_LoadPNGMetaData GP_SavePNG
GP_MatchBMP +GP_WriteBMP GP_LoadBMP GP_ReadBMP GP_SaveBMP @@ -109,6 +110,7 @@ GP_IOFill GP_IOReadF GP_IOReadB2 GP_IOReadB4 +GP_IOWriteF
GP_IOZlib GP_IOZlibReset diff --git a/include/loaders/GP_BMP.h b/include/loaders/GP_BMP.h index 48eff73a..047d7733 100644 --- a/include/loaders/GP_BMP.h +++ b/include/loaders/GP_BMP.h @@ -41,6 +41,14 @@ GP_Context *GP_ReadBMP(GP_IO *io, GP_ProgressCallback *callback); GP_Context *GP_LoadBMP(const char *src_path, GP_ProgressCallback *callback);
/* + * Writes a BMP to an IO Stream. + * + * Returns zero on success, non-zero on failure and errno is set. + */ +int GP_WriteBMP(const GP_Context *src, GP_IO *io, + GP_ProgressCallback *callback); + +/* * Saves BMP to a file. Zero is returned on succes. Upon failure non-zero is * returned and errno is filled accordingly. */ diff --git a/include/loaders/GP_IO.h b/include/loaders/GP_IO.h index 6947e3b3..19a1c78e 100644 --- a/include/loaders/GP_IO.h +++ b/include/loaders/GP_IO.h @@ -173,6 +173,8 @@ enum GP_IOReadFTypes {
int GP_IOReadF(GP_IO *self, uint16_t *types, ...);
+int GP_IOWriteF(GP_IO *self, uint16_t *types, ...); + /* * GP_IOReadF wrappers for convinient reading of single value */ diff --git a/libs/loaders/GP_BMP.c b/libs/loaders/GP_BMP.c index cc03a5e3..1d9e9c20 100644 --- a/libs/loaders/GP_BMP.c +++ b/libs/loaders/GP_BMP.c @@ -226,8 +226,7 @@ static int read_bitmap_info_header(GP_IO *io, struct bitmap_info_header *header)
/* This must be 1 according to specs */ if (nr_planes != 1) - GP_DEBUG(1, "Number of planes is %"PRId16" should be 1", - nr_planes); + GP_WARN("Number of planes %"PRId16" should be 1", nr_planes);
GP_DEBUG(2, "Have BMP bitmap size %"PRId32"x%"PRId32" %"PRIu16" " "bpp, %"PRIu32" pallete colors, '%s' compression", @@ -742,22 +741,43 @@ static uint32_t bmp_count_bitmap_size(struct bitmap_info_header *header) return header->h * bmp_align_row_size(header->bpp * header->w); }
-static int bmp_write_header(struct bitmap_info_header *header, FILE *f) +static int bmp_write_header(GP_IO *io, struct bitmap_info_header *header) { uint32_t bitmap_size = bmp_count_bitmap_size(header); uint32_t file_size = bitmap_size + header->header_size + 14;
- /* Bitmap Header */ - if (GP_FWrite(f, "A2 L4 0x00 0x00 0x00 0x00 L4", "BM", - file_size, header->pixel_offset) != 7) + uint16_t bitmap_header[] = { + 'B', 'M', /* signature */ + GP_IO_L4, /* pixels offset */ + 0x00, 0x00, 0x00, 0x00, /* reserved */ + GP_IO_L4, /* file size */ + GP_IO_END, + }; + + if (GP_IOWriteF(io, bitmap_header, file_size, header->pixel_offset)) return EIO;
- /* Bitmap Info Header */ - if (GP_FWrite(f, "L4 L4 L4 L2 L2 L4 L4 L4 L4 L4 L4", - header->header_size, header->w, header->h, 1, - header->bpp, header->compress_type, bitmap_size, 0, 0, - header->palette_colors, 0) != 11) + uint16_t bitmap_info_header[] = { + GP_IO_L4, /* header size */ + GP_IO_L4, /* width */ + GP_IO_L4, /* height */ + 0x01, 0x00, /* nr planes, always 1 */ + GP_IO_L2, /* bpp */ + GP_IO_L4, /* compression type */ + GP_IO_L4, /* bitmap size */ + 0, 0, 0, 0, /* X PPM */ + 0, 0, 0, 0, /* Y PPM */ + GP_IO_L4, /* palette colors */ + 0, 0, 0, 0, /* important palette colors */ + GP_IO_END, + }; + + if (GP_IOWriteF(io, bitmap_info_header, header->header_size, + header->w, header->h, header->bpp, + header->compress_type, bitmap_size, + header->palette_colors)) { return EIO; + }
return 0; } @@ -796,7 +816,8 @@ static int bmp_fill_header(const GP_Context *src, struct bitmap_info_header *hea return 0; }
-static int bmp_write_data(FILE *f, const GP_Context *src, GP_ProgressCallback *callback) +static int bmp_write_data(GP_IO *io, const GP_Context *src, + GP_ProgressCallback *callback) { int y; uint32_t padd_len = 0; @@ -817,13 +838,14 @@ static int bmp_write_data(FILE *f, const GP_Context *src, GP_ProgressCallback *c row = tmp; }
- if (fwrite(row, src->bytes_per_row, 1, f) != 1) + if (GP_IOWrite(io, row, src->bytes_per_row) != src->bytes_per_row) return EIO;
/* write padding */ - if (padd_len) - if (fwrite(padd, padd_len, 1, f) != 1) + if (padd_len) { + if (GP_IOWrite(io, padd, padd_len) != padd_len) return EIO; + }
if (GP_ProgressCallbackReport(callback, y, src->h, src->w)) { GP_DEBUG(1, "Operation aborted"); @@ -831,50 +853,55 @@ static int bmp_write_data(FILE *f, const GP_Context *src, GP_ProgressCallback *c } }
+ GP_ProgressCallbackDone(callback); + return 0; }
-int GP_SaveBMP(const GP_Context *src, const char *dst_path, - GP_ProgressCallback *callback) +int GP_WriteBMP(const GP_Context *src, GP_IO *io, + GP_ProgressCallback *callback) { struct bitmap_info_header header; - FILE *f; int err;
- GP_DEBUG(1, "Saving BMP Image '%s'", dst_path); - if ((err = bmp_fill_header(src, &header))) - goto err0; + goto err;
- f = fopen(dst_path, "wb"); + if ((err = bmp_write_header(io, &header))) + goto err;
- if (f == NULL) { - err = errno; - GP_DEBUG(1, "Failed to open '%s' for writing: %s", - dst_path, strerror(errno)); - goto err0; - } + if ((err = bmp_write_data(io, src, callback))) + goto err;
- if ((err = bmp_write_header(&header, f))) - goto err1; + return 0; +err: + errno = err; + return 1; +}
- if ((err = bmp_write_data(f, src, callback))) - goto err1;
- if (fclose(f)) { - err = errno; - GP_DEBUG(1, "Failed to close file '%s': %s", - dst_path, strerror(errno)); - goto err1; +int GP_SaveBMP(const GP_Context *src, const char *dst_path, + GP_ProgressCallback *callback) +{ + GP_IO *io; + + GP_DEBUG(1, "Saving BMP Image '%s'", dst_path); + + io = GP_IOFile(dst_path, GP_IO_WRONLY); + + if (!io) + return 1; + + if (GP_WriteBMP(src, io, callback)) { + GP_IOClose(io); + unlink(dst_path); + return 1; }
- GP_ProgressCallbackDone(callback); + if (GP_IOClose(io)) { + unlink(dst_path); + return 1; + }
return 0; -err1: - fclose(f); -err0: - unlink(dst_path); - errno = err; - return 1; } diff --git a/libs/loaders/GP_IO.c b/libs/loaders/GP_IO.c index 0b77bd5c..145058b8 100644 --- a/libs/loaders/GP_IO.c +++ b/libs/loaders/GP_IO.c @@ -27,6 +27,7 @@ #include <sys/stat.h> #include <fcntl.h> #include <stdarg.h> +#include <inttypes.h>
#include <core/GP_ByteOrder.h> #include <core/GP_Debug.h> @@ -612,6 +613,60 @@ end: return ret; }
+int GP_IOWriteF(GP_IO *io, uint16_t *types, ...) +{ + va_list va; + uint8_t *ptr, t; + int32_t i4; + int16_t i2; + + va_start(va, types); + + while (*types != GP_IO_END) { + switch (TYPE(*types)) { + case GP_IO_CONST: + t = VAL(*types); + if (GP_IOWrite(io, &t, 1) != 1) + goto err; + break; + case GP_IO_L2: + case GP_IO_B2: + i2 = va_arg(va, int); + ptr = (void*)&i2; + + if (needs_swap(*types)) + GP_SWAP(ptr[0], ptr[1]); + + if (GP_IOWrite(io, ptr, 2) != 2) + goto err; + break; + case GP_IO_L4: + case GP_IO_B4: + i4 = va_arg(va, int); + ptr = (void*)&i4; + + if (needs_swap(*types)) { + GP_SWAP(ptr[0], ptr[3]); + GP_SWAP(ptr[1], ptr[2]); + } + + if (GP_IOWrite(io, ptr, 4) != 4) + goto err; + break; + default: + GP_WARN("Invalid type %"PRIu16"n", *types); + goto err; + } + types++; + } + + va_end(va); + return 0; +err: + va_end(va); + return -1; +} + int GP_IOReadB4(GP_IO *io, uint32_t *val) { uint16_t desc[] = {
-----------------------------------------------------------------------
Summary of changes: build/syms/Loaders_symbols.txt | 2 + include/loaders/GP_BMP.h | 8 +++ include/loaders/GP_IO.h | 2 + libs/loaders/GP_BMP.c | 115 ++++++++++++++++++++++++--------------- libs/loaders/GP_IO.c | 55 +++++++++++++++++++ 5 files changed, 138 insertions(+), 44 deletions(-)
repo.or.cz automatic notification. Contact project admin jiri.bluebear.dluhos@gmail.com if you want to unsubscribe, or site admin admin@repo.or.cz if you receive no reply.