Fri, 21 Feb 2003 10:49:11 +0000
changed pdf_calloc() to take two args like ordinary calloc(). added pdf_stream_write_bits() and pdf_stream_flush_bits().
bitblt_g4.c | file | annotate | diff | revisions | |
pdf.c | file | annotate | diff | revisions | |
pdf_g4.c | file | annotate | diff | revisions | |
pdf_prim.c | file | annotate | diff | revisions | |
pdf_prim.h | file | annotate | diff | revisions | |
pdf_util.c | file | annotate | diff | revisions | |
pdf_util.h | file | annotate | diff | revisions |
1.1 --- a/bitblt_g4.c Fri Feb 21 09:25:47 2003 +0000 1.2 +++ b/bitblt_g4.c Fri Feb 21 10:49:11 2003 +0000 1.3 @@ -4,7 +4,7 @@ 1.4 * will be compressed using ITU-T T.6 (G4) fax encoding. 1.5 * 1.6 * PDF routines 1.7 - * $Id: bitblt_g4.c,v 1.5 2003/02/21 01:25:47 eric Exp $ 1.8 + * $Id: bitblt_g4.c,v 1.6 2003/02/21 02:49:11 eric Exp $ 1.9 * Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com> 1.10 * 1.11 * This program is free software; you can redistribute it and/or modify 1.12 @@ -27,6 +27,7 @@ 1.13 #include <stdbool.h> 1.14 #include <stdint.h> 1.15 #include <stdio.h> 1.16 +#include <stdlib.h> 1.17 #include <string.h> 1.18 1.19 1.20 @@ -37,6 +38,9 @@ 1.21 #include "pdf_private.h" 1.22 1.23 1.24 +#define SWAP(type,a,b) do { type temp; temp = a; a = b; b = temp; } while (0) 1.25 + 1.26 + 1.27 struct pdf_g4_image 1.28 { 1.29 double width, height; 1.30 @@ -94,28 +98,48 @@ 1.31 { 1.32 struct pdf_g4_image *image = app_data; 1.33 1.34 -#if 0 1.35 - pdf_stream_write_data (pdf_file, stream, image->data, image->len); 1.36 -#else 1.37 - unsigned long row = 0; 1.38 - word_type *ref; 1.39 - word_type *raw; 1.40 + uint32_t row; 1.41 + 1.42 + /* reference (previous) row */ 1.43 + uint32_t *ref_runs = pdf_calloc (image->Columns, sizeof (uint32_t)); 1.44 + uint32_t ref_run_count; 1.45 1.46 - ref = NULL; 1.47 - raw = image->bitmap->bits; 1.48 + /* row being converted */ 1.49 + uint32_t *row_runs = pdf_calloc (image->Columns, sizeof (uint32_t)); 1.50 + uint32_t row_run_count; 1.51 + 1.52 + /* initialize reference row - all white */ 1.53 + ref_runs [0] = image->Columns; 1.54 + ref_run_count = 0; 1.55 1.56 - while (row < image->Rows) 1.57 + for (row = image->bitmap->rect.min.y; 1.58 + row < image->bitmap->rect.max.y; 1.59 + row++) 1.60 { 1.61 - pdf_stream_write_data (pdf_file, stream, (uint8_t *) raw, 1.62 - image->bitmap->row_words * sizeof (word_type)); 1.63 + row_run_count = get_row_run_lengths (image->bitmap, 1.64 + row, 1.65 + image->bitmap->rect.min.x, 1.66 + image->bitmap->rect.max.x - 1, 1.67 + image->Columns, /* max_runs */ 1.68 + row_runs); 1.69 + pdf_assert (row_run_count > 0); 1.70 + 1.71 + /* $$$ G4 encode the runs here */ 1.72 1.73 - row++; 1.74 - ref = raw; 1.75 - raw += image->bitmap->row_words; 1.76 + /* pdf_stream_write_data (pdf_file, stream, image->data, image->len); */ 1.77 + 1.78 + SWAP (uint32_t *, row_runs, ref_runs); 1.79 + ref_run_count = row_run_count; 1.80 } 1.81 + 1.82 + 1.83 /* $$$ generate and write EOFB code */ 1.84 - /* $$$ flush any remaining buffered bits */ 1.85 -#endif 1.86 + pdf_stream_write_bits (pdf_file, stream, 24, 0x001001); 1.87 + 1.88 + pdf_stream_flush_bits (pdf_file, stream); 1.89 + 1.90 + free (ref_runs); 1.91 + free (row_runs); 1.92 } 1.93 1.94 1.95 @@ -139,7 +163,7 @@ 1.96 1.97 struct pdf_obj *content_stream; 1.98 1.99 - image = pdf_calloc (sizeof (struct pdf_g4_image)); 1.100 + image = pdf_calloc (1, sizeof (struct pdf_g4_image)); 1.101 1.102 image->width = width; 1.103 image->height = height;
2.1 --- a/pdf.c Fri Feb 21 09:25:47 2003 +0000 2.2 +++ b/pdf.c Fri Feb 21 10:49:11 2003 +0000 2.3 @@ -4,7 +4,7 @@ 2.4 * will be compressed using ITU-T T.6 (G4) fax encoding. 2.5 * 2.6 * PDF routines 2.7 - * $Id: pdf.c,v 1.3 2003/02/20 04:44:17 eric Exp $ 2.8 + * $Id: pdf.c,v 1.4 2003/02/21 02:49:11 eric Exp $ 2.9 * Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com> 2.10 * 2.11 * This program is free software; you can redistribute it and/or modify 2.12 @@ -53,7 +53,7 @@ 2.13 2.14 struct pdf_pages *pdf_new_pages (pdf_file_handle pdf_file) 2.15 { 2.16 - struct pdf_pages *pages = pdf_calloc (sizeof (struct pdf_pages)); 2.17 + struct pdf_pages *pages = pdf_calloc (1, sizeof (struct pdf_pages)); 2.18 pages->kids = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_ARRAY)); 2.19 pages->count = pdf_new_integer (0); 2.20 pages->pages_dict = pdf_new_ind_ref (pdf_file, pdf_new_obj (PT_DICTIONARY)); 2.21 @@ -68,7 +68,7 @@ 2.22 { 2.23 pdf_file_handle pdf_file; 2.24 2.25 - pdf_file = pdf_calloc (sizeof (struct pdf_file)); 2.26 + pdf_file = pdf_calloc (1, sizeof (struct pdf_file)); 2.27 2.28 pdf_file->f = fopen (filename, "wb"); 2.29 if (! pdf_file->f) 2.30 @@ -154,7 +154,7 @@ 2.31 double width, 2.32 double height) 2.33 { 2.34 - pdf_page_handle page = pdf_calloc (sizeof (struct pdf_page)); 2.35 + pdf_page_handle page = pdf_calloc (1, sizeof (struct pdf_page)); 2.36 2.37 page->pdf_file = pdf_file; 2.38
3.1 --- a/pdf_g4.c Fri Feb 21 09:25:47 2003 +0000 3.2 +++ b/pdf_g4.c Fri Feb 21 10:49:11 2003 +0000 3.3 @@ -4,7 +4,7 @@ 3.4 * will be compressed using ITU-T T.6 (G4) fax encoding. 3.5 * 3.6 * PDF routines 3.7 - * $Id: pdf_g4.c,v 1.5 2003/02/21 01:25:47 eric Exp $ 3.8 + * $Id: pdf_g4.c,v 1.6 2003/02/21 02:49:11 eric Exp $ 3.9 * Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com> 3.10 * 3.11 * This program is free software; you can redistribute it and/or modify 3.12 @@ -27,6 +27,7 @@ 3.13 #include <stdbool.h> 3.14 #include <stdint.h> 3.15 #include <stdio.h> 3.16 +#include <stdlib.h> 3.17 #include <string.h> 3.18 3.19 3.20 @@ -37,6 +38,9 @@ 3.21 #include "pdf_private.h" 3.22 3.23 3.24 +#define SWAP(type,a,b) do { type temp; temp = a; a = b; b = temp; } while (0) 3.25 + 3.26 + 3.27 struct pdf_g4_image 3.28 { 3.29 double width, height; 3.30 @@ -94,28 +98,48 @@ 3.31 { 3.32 struct pdf_g4_image *image = app_data; 3.33 3.34 -#if 0 3.35 - pdf_stream_write_data (pdf_file, stream, image->data, image->len); 3.36 -#else 3.37 - unsigned long row = 0; 3.38 - word_type *ref; 3.39 - word_type *raw; 3.40 + uint32_t row; 3.41 + 3.42 + /* reference (previous) row */ 3.43 + uint32_t *ref_runs = pdf_calloc (image->Columns, sizeof (uint32_t)); 3.44 + uint32_t ref_run_count; 3.45 3.46 - ref = NULL; 3.47 - raw = image->bitmap->bits; 3.48 + /* row being converted */ 3.49 + uint32_t *row_runs = pdf_calloc (image->Columns, sizeof (uint32_t)); 3.50 + uint32_t row_run_count; 3.51 + 3.52 + /* initialize reference row - all white */ 3.53 + ref_runs [0] = image->Columns; 3.54 + ref_run_count = 0; 3.55 3.56 - while (row < image->Rows) 3.57 + for (row = image->bitmap->rect.min.y; 3.58 + row < image->bitmap->rect.max.y; 3.59 + row++) 3.60 { 3.61 - pdf_stream_write_data (pdf_file, stream, (uint8_t *) raw, 3.62 - image->bitmap->row_words * sizeof (word_type)); 3.63 + row_run_count = get_row_run_lengths (image->bitmap, 3.64 + row, 3.65 + image->bitmap->rect.min.x, 3.66 + image->bitmap->rect.max.x - 1, 3.67 + image->Columns, /* max_runs */ 3.68 + row_runs); 3.69 + pdf_assert (row_run_count > 0); 3.70 + 3.71 + /* $$$ G4 encode the runs here */ 3.72 3.73 - row++; 3.74 - ref = raw; 3.75 - raw += image->bitmap->row_words; 3.76 + /* pdf_stream_write_data (pdf_file, stream, image->data, image->len); */ 3.77 + 3.78 + SWAP (uint32_t *, row_runs, ref_runs); 3.79 + ref_run_count = row_run_count; 3.80 } 3.81 + 3.82 + 3.83 /* $$$ generate and write EOFB code */ 3.84 - /* $$$ flush any remaining buffered bits */ 3.85 -#endif 3.86 + pdf_stream_write_bits (pdf_file, stream, 24, 0x001001); 3.87 + 3.88 + pdf_stream_flush_bits (pdf_file, stream); 3.89 + 3.90 + free (ref_runs); 3.91 + free (row_runs); 3.92 } 3.93 3.94 3.95 @@ -139,7 +163,7 @@ 3.96 3.97 struct pdf_obj *content_stream; 3.98 3.99 - image = pdf_calloc (sizeof (struct pdf_g4_image)); 3.100 + image = pdf_calloc (1, sizeof (struct pdf_g4_image)); 3.101 3.102 image->width = width; 3.103 image->height = height;
4.1 --- a/pdf_prim.c Fri Feb 21 09:25:47 2003 +0000 4.2 +++ b/pdf_prim.c Fri Feb 21 10:49:11 2003 +0000 4.3 @@ -4,7 +4,7 @@ 4.4 * will be compressed using ITU-T T.6 (G4) fax encoding. 4.5 * 4.6 * PDF routines 4.7 - * $Id: pdf_prim.c,v 1.4 2003/02/21 01:25:47 eric Exp $ 4.8 + * $Id: pdf_prim.c,v 1.5 2003/02/21 02:49:11 eric Exp $ 4.9 * Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com> 4.10 * 4.11 * This program is free software; you can redistribute it and/or modify 4.12 @@ -66,6 +66,8 @@ 4.13 }; 4.14 4.15 4.16 +#define STREAM_BUF_SIZE 4096 4.17 + 4.18 struct pdf_stream 4.19 { 4.20 struct pdf_obj *stream_dict; 4.21 @@ -74,6 +76,13 @@ 4.22 void *app_data; /* arg to pass to callback */ 4.23 struct pdf_obj *filters; /* name or array of names */ 4.24 struct pdf_obj *decode_parms; 4.25 + 4.26 + /* The following fields are used by pdf_stream_write_bits() and 4.27 + pdf_stream_flush_bits(). */ 4.28 + uint32_t byte_idx; /* index to next byte position in data buffer */ 4.29 + uint32_t bit_idx; /* index to next bit position in data buffer, 4.30 + 0 = MSB, 7 = LSB */ 4.31 + uint8_t data [STREAM_BUF_SIZE]; 4.32 }; 4.33 4.34 4.35 @@ -145,7 +154,7 @@ 4.36 } 4.37 4.38 /* new entry */ 4.39 - entry = pdf_calloc (sizeof (struct pdf_dict_entry)); 4.40 + entry = pdf_calloc (1, sizeof (struct pdf_dict_entry)); 4.41 4.42 entry->next = dict_obj->val.dict.first; 4.43 dict_obj->val.dict.first = entry; 4.44 @@ -174,7 +183,7 @@ 4.45 4.46 void pdf_add_array_elem (struct pdf_obj *array_obj, struct pdf_obj *val) 4.47 { 4.48 - struct pdf_array_elem *elem = pdf_calloc (sizeof (struct pdf_array_elem)); 4.49 + struct pdf_array_elem *elem = pdf_calloc (1, sizeof (struct pdf_array_elem)); 4.50 4.51 if (array_obj->type == PT_IND_REF) 4.52 array_obj = pdf_deref_ind_obj (array_obj); 4.53 @@ -194,7 +203,7 @@ 4.54 4.55 struct pdf_obj *pdf_new_obj (pdf_obj_type type) 4.56 { 4.57 - struct pdf_obj *obj = pdf_calloc (sizeof (struct pdf_obj)); 4.58 + struct pdf_obj *obj = pdf_calloc (1, sizeof (struct pdf_obj)); 4.59 obj->type = type; 4.60 return (obj); 4.61 } 4.62 @@ -453,6 +462,65 @@ 4.63 } 4.64 4.65 4.66 +void pdf_stream_flush_bits (pdf_file_handle pdf_file, 4.67 + struct pdf_obj *stream) 4.68 +{ 4.69 + struct pdf_stream *s = & stream->val.stream; 4.70 + 4.71 + if (s->bit_idx) 4.72 + { 4.73 + /* zero remaining bits in last byte */ 4.74 + s->data [s->byte_idx] &= ~ ((1 << (8 - s->bit_idx)) - 1); 4.75 + s->byte_idx++; 4.76 + s->bit_idx = 0; 4.77 + } 4.78 + pdf_stream_write_data (pdf_file, stream, 4.79 + (char *) & s->data [0], 4.80 + s->byte_idx); 4.81 + s->byte_idx = 0; 4.82 +} 4.83 + 4.84 + 4.85 +static void pdf_stream_advance_byte (pdf_file_handle pdf_file, 4.86 + struct pdf_obj *stream) 4.87 +{ 4.88 + struct pdf_stream *s = & stream->val.stream; 4.89 + 4.90 + s->byte_idx++; 4.91 + s->bit_idx = 0; 4.92 + if (s->byte_idx == STREAM_BUF_SIZE) 4.93 + pdf_stream_flush_bits (pdf_file, stream); 4.94 +} 4.95 + 4.96 + 4.97 +void pdf_stream_write_bits (pdf_file_handle pdf_file, 4.98 + struct pdf_obj *stream, 4.99 + uint32_t count, 4.100 + uint32_t bits) 4.101 +{ 4.102 + struct pdf_stream *s = & stream->val.stream; 4.103 + 4.104 + uint32_t b2; /* how many bits will fit in byte in data buffer */ 4.105 + uint32_t c2; /* how many bits to transfer on this iteration */ 4.106 + uint32_t d2; /* bits to transfer on this iteration */ 4.107 + 4.108 + while (count) 4.109 + { 4.110 + b2 = 8 - s->bit_idx; 4.111 + if (b2 >= count) 4.112 + c2 = count; 4.113 + else 4.114 + c2 = b2; 4.115 + d2 = bits >> (count - c2); 4.116 + s->data [s->byte_idx] |= (d2 << (b2 + c2)); 4.117 + s->bit_idx += c2; 4.118 + if (s->bit_idx > 7) 4.119 + pdf_stream_advance_byte (pdf_file, stream); 4.120 + count -= c2; 4.121 + } 4.122 +} 4.123 + 4.124 + 4.125 void pdf_stream_printf (pdf_file_handle pdf_file, 4.126 struct pdf_obj *stream, 4.127 char *fmt, ...)
5.1 --- a/pdf_prim.h Fri Feb 21 09:25:47 2003 +0000 5.2 +++ b/pdf_prim.h Fri Feb 21 10:49:11 2003 +0000 5.3 @@ -4,7 +4,7 @@ 5.4 * will be compressed using ITU-T T.6 (G4) fax encoding. 5.5 * 5.6 * PDF routines 5.7 - * $Id: pdf_prim.h,v 1.3 2003/02/21 01:25:47 eric Exp $ 5.8 + * $Id: pdf_prim.h,v 1.4 2003/02/21 02:49:11 eric Exp $ 5.9 * Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com> 5.10 * 5.11 * This program is free software; you can redistribute it and/or modify 5.12 @@ -95,8 +95,19 @@ 5.13 pdf_stream_write_callback callback, 5.14 void *app_data); 5.15 5.16 -/* The callback should call pdf_stream_write_data or pdf_stream_printf 5.17 - to write the actual stream data. */ 5.18 +/* The callback should call pdf_stream_write_bits(), pdf_stream_write_data(), 5.19 + or pdf_stream_printf() to write the actual stream data. If 5.20 + pdf_stream_write_bits() is used, pdf_stream_flush_bits() should be 5.21 + called after all the bits are written. */ 5.22 + 5.23 +void pdf_stream_write_bits (pdf_file_handle pdf_file, 5.24 + struct pdf_obj *stream, 5.25 + uint32_t count, 5.26 + uint32_t bits); 5.27 + 5.28 +void pdf_stream_flush_bits (pdf_file_handle pdf_file, 5.29 + struct pdf_obj *stream); 5.30 + 5.31 void pdf_stream_write_data (pdf_file_handle pdf_file, 5.32 struct pdf_obj *stream, 5.33 char *data,
6.1 --- a/pdf_util.c Fri Feb 21 09:25:47 2003 +0000 6.2 +++ b/pdf_util.c Fri Feb 21 10:49:11 2003 +0000 6.3 @@ -4,7 +4,7 @@ 6.4 * will be compressed using ITU-T T.6 (G4) fax encoding. 6.5 * 6.6 * PDF routines 6.7 - * $Id: pdf_util.c,v 1.3 2003/02/20 04:44:17 eric Exp $ 6.8 + * $Id: pdf_util.c,v 1.4 2003/02/21 02:49:11 eric Exp $ 6.9 * Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com> 6.10 * 6.11 * This program is free software; you can redistribute it and/or modify 6.12 @@ -48,9 +48,9 @@ 6.13 } 6.14 6.15 6.16 -void *pdf_calloc (long int size) 6.17 +void *pdf_calloc (size_t nmemb, size_t size) 6.18 { 6.19 - void *m = calloc (1, size); 6.20 + void *m = calloc (nmemb, size); 6.21 if (! m) 6.22 pdf_fatal ("failed to allocate memory\n"); 6.23 return (m); 6.24 @@ -60,7 +60,7 @@ 6.25 char *pdf_strdup (char *s) 6.26 { 6.27 unsigned long len = strlen (s); 6.28 - char *s2 = pdf_calloc (len + 1); 6.29 + char *s2 = pdf_calloc (1, len + 1); 6.30 strcpy (s2, s); 6.31 return (s2); 6.32 }
7.1 --- a/pdf_util.h Fri Feb 21 09:25:47 2003 +0000 7.2 +++ b/pdf_util.h Fri Feb 21 10:49:11 2003 +0000 7.3 @@ -4,7 +4,7 @@ 7.4 * will be compressed using ITU-T T.6 (G4) fax encoding. 7.5 * 7.6 * PDF routines 7.7 - * $Id: pdf_util.h,v 1.2 2003/02/20 04:44:17 eric Exp $ 7.8 + * $Id: pdf_util.h,v 1.3 2003/02/21 02:49:11 eric Exp $ 7.9 * Copyright 2001, 2002, 2003 Eric Smith <eric@brouhaha.com> 7.10 * 7.11 * This program is free software; you can redistribute it and/or modify 7.12 @@ -26,7 +26,7 @@ 7.13 7.14 void pdf_fatal (char *fmt, ...); 7.15 7.16 -void *pdf_calloc (long int size); 7.17 +void *pdf_calloc (size_t nmemb, size_t size); 7.18 7.19 char *pdf_strdup (char *s); 7.20