| 1 |
/* |
| 2 |
* pbm.c - loading and saving in PBM ("portable bitmap") format |
| 3 |
*/ |
| 4 |
|
| 5 |
#include "../base/mdjvucfg.h" |
| 6 |
#include <minidjvu/minidjvu.h> |
| 7 |
#include <stdio.h> |
| 8 |
|
| 9 |
static void skip_to_the_end_of_line(FILE *file) |
| 10 |
{ |
| 11 |
while (1) |
| 12 |
{ |
| 13 |
switch(fgetc(file)) |
| 14 |
{ |
| 15 |
case -1: case '\r': case '\n': |
| 16 |
return; |
| 17 |
} |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
static void mdjvu_skip_pbm_whitespace_and_comments(mdjvu_file_t f) |
| 22 |
{ |
| 23 |
FILE *file = (FILE *) f; |
| 24 |
int c = fgetc(file); |
| 25 |
while(1) |
| 26 |
{ |
| 27 |
switch(c) |
| 28 |
{ |
| 29 |
case ' ': case '\t': case '\r': case '\n': |
| 30 |
c = fgetc(file); |
| 31 |
break; |
| 32 |
case '#': |
| 33 |
skip_to_the_end_of_line(file); |
| 34 |
c = fgetc(file); |
| 35 |
break; |
| 36 |
default: |
| 37 |
ungetc(c,file); |
| 38 |
return; |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
MDJVU_IMPLEMENT int mdjvu_save_pbm(mdjvu_bitmap_t b, const char *path, mdjvu_error_t *perr) |
| 44 |
{ |
| 45 |
FILE *file = fopen(path, "wb"); |
| 46 |
int result; |
| 47 |
if (perr) *perr = NULL; |
| 48 |
if (!file) |
| 49 |
{ |
| 50 |
if (perr) *perr = mdjvu_get_error(mdjvu_error_fopen_write); |
| 51 |
return 0; |
| 52 |
} |
| 53 |
result = mdjvu_file_save_pbm(b, (mdjvu_file_t) file, perr); |
| 54 |
fclose(file); |
| 55 |
return result; |
| 56 |
} |
| 57 |
|
| 58 |
MDJVU_IMPLEMENT int mdjvu_file_save_pbm(mdjvu_bitmap_t b, mdjvu_file_t f, mdjvu_error_t *perr) |
| 59 |
{ |
| 60 |
FILE *file = (FILE *) f; |
| 61 |
int32 bytes_per_row = mdjvu_bitmap_get_packed_row_size(b); |
| 62 |
int32 width = mdjvu_bitmap_get_width(b); |
| 63 |
int32 height = mdjvu_bitmap_get_height(b); |
| 64 |
int32 i; |
| 65 |
|
| 66 |
if (perr) *perr = NULL; |
| 67 |
|
| 68 |
fprintf(file, "P4\n"MDJVU_INT32_FORMAT" "MDJVU_INT32_FORMAT"\n", |
| 69 |
width, height); |
| 70 |
|
| 71 |
for (i = 0; i < height; i++) |
| 72 |
{ |
| 73 |
unsigned char *row = mdjvu_bitmap_access_packed_row(b, i); |
| 74 |
if (fwrite(row, bytes_per_row, 1, file) != 1) |
| 75 |
{ |
| 76 |
if (perr) *perr = mdjvu_get_error(mdjvu_error_io); |
| 77 |
return 0; |
| 78 |
} |
| 79 |
} |
| 80 |
return 1; |
| 81 |
} |
| 82 |
|
| 83 |
MDJVU_IMPLEMENT mdjvu_bitmap_t mdjvu_load_pbm(const char *path, mdjvu_error_t *perr) |
| 84 |
{ |
| 85 |
FILE *file = fopen(path, "rb"); |
| 86 |
mdjvu_bitmap_t result; |
| 87 |
if (perr) *perr = NULL; |
| 88 |
if (!file) |
| 89 |
{ |
| 90 |
if(perr) *perr = mdjvu_get_error(mdjvu_error_fopen_read); |
| 91 |
return NULL; |
| 92 |
} |
| 93 |
result = mdjvu_file_load_pbm((mdjvu_file_t) file, perr); |
| 94 |
fclose(file); |
| 95 |
return result; |
| 96 |
} |
| 97 |
|
| 98 |
#define COMPLAIN \ |
| 99 |
{ \ |
| 100 |
if (perr) *perr = mdjvu_get_error(mdjvu_error_corrupted_pbm); \ |
| 101 |
return NULL; \ |
| 102 |
} |
| 103 |
MDJVU_IMPLEMENT mdjvu_bitmap_t mdjvu_file_load_pbm(mdjvu_file_t f, mdjvu_error_t *perr) |
| 104 |
{ |
| 105 |
FILE *file = (FILE *) f; |
| 106 |
int32 width, height, bytes_per_row, i; |
| 107 |
mdjvu_bitmap_t result; |
| 108 |
if (perr) *perr = NULL; |
| 109 |
if (fgetc(file) != 'P') COMPLAIN; |
| 110 |
if (fgetc(file) != '4') COMPLAIN; |
| 111 |
mdjvu_skip_pbm_whitespace_and_comments((mdjvu_file_t) file); |
| 112 |
if (fscanf(file, |
| 113 |
MDJVU_INT32_FORMAT" "MDJVU_INT32_FORMAT, &width, &height) != 2) |
| 114 |
{ |
| 115 |
COMPLAIN; |
| 116 |
} |
| 117 |
|
| 118 |
/* a fancy way to write if ( || || || ) - maybe, abandon this switch? */ |
| 119 |
switch(fgetc(file)) |
| 120 |
{ |
| 121 |
case ' ': case '\t': case '\r': case '\n': |
| 122 |
break; |
| 123 |
default: |
| 124 |
COMPLAIN; |
| 125 |
} |
| 126 |
|
| 127 |
result = mdjvu_bitmap_create(width, height); |
| 128 |
bytes_per_row = mdjvu_bitmap_get_packed_row_size(result); |
| 129 |
for (i = 0; i < height; i++) |
| 130 |
{ |
| 131 |
unsigned char *current_row = mdjvu_bitmap_access_packed_row(result, i); |
| 132 |
if (fread(current_row, bytes_per_row, 1, file) != 1) |
| 133 |
{ |
| 134 |
mdjvu_bitmap_destroy(result); |
| 135 |
COMPLAIN; |
| 136 |
} |
| 137 |
} |
| 138 |
return result; |
| 139 |
} |