#define MAXBUFFER 6
enum imgformat {
- RGB = 3,
+ BW = 0,
GRAY = 1,
- BW = 1
+ RGB = 3
};
tsize_t _kds_s2000w_image_converter_tiff_read(thandle_t handler, tdata_t data, tsize_t size)
fclose(jpeg_stream);
}
+void _kds_s2000w_image_converter_write_bw_pnm(unsigned char* data,
+ size_t size,
+ int width,
+ int height,
+ FILE* pnm_stream)
+{
+ kds_s2000w_debug_printf(ALL, "kds_s2000w_image_converter_write_bw_pnm");
+
+ xel** pixels = malloc(sizeof(xel*) * height);
+ for (int i = 0; i < height; i++) {
+ pixels[i] = malloc(sizeof(xel) * width);
+ }
+
+ int currwidth = 0;
+ int currheight = 0;
+ for (int i = 0; i < size; i++) {
+ pixels[currheight][currwidth].r = 0;
+ pixels[currheight][currwidth].g = 0;
+ pixels[currheight][currwidth].b = 0;
+ if (data[i] < 1)
+ pixels[currheight][currwidth].b = PGM_MAXMAXVAL;
+
+ currwidth++;
+ if (currwidth >= width) {
+ currwidth = 0;
+ currheight++;
+ }
+ }
+
+ pnm_writepnm(pnm_stream, pixels, width, height, PNM_MAXMAXVAL, PBM_FORMAT, 0);
+
+ for (int i = 0; i < height; i++) {
+ free(pixels[i]);
+ pixels[i] = NULL;
+ }
+ free(pixels);
+ pixels = NULL;
+}
+
void _kds_s2000w_image_converter_write_pnm(unsigned char* data,
size_t size,
int width,
{
kds_s2000w_debug_printf(ALL, "kds_s2000w_image_converter_write_pnm");
+ if (format == BW) {
+ _kds_s2000w_image_converter_write_bw_pnm(data,
+ size,
+ width,
+ height,
+ pnm_stream);
+ return;
+ }
+
xel** pixels = malloc(sizeof(xel*) * height);
for (int i = 0; i < height; i++) {
pixels[i] = malloc(sizeof(xel) * width);
pnm_writepnm(pnm_stream, pixels, width, height, PNM_MAXMAXVAL, PPM_FORMAT, 0);
else
pnm_writepnm(pnm_stream, pixels, width, height, PGM_MAXMAXVAL, PGM_FORMAT, 0);
+
+ for (int i = 0; i < height; i++) {
+ free(pixels[i]);
+ pixels[i] = NULL;
+ }
+ free(pixels);
+ pixels = NULL;
}
void _kds_s2000w_image_converter_jpg_to_pnm(j_decompress_ptr cinfo,
TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &width);
TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &height);
- bit** bits = malloc(sizeof(bit*) * height);
- for (int i = 0; i < height; i++) {
- bits[i] = malloc(sizeof(bit) * width);
- }
+ size_t decompress_size = width * height;
+ unsigned char* decompress_data = malloc(sizeof(unsigned char) * decompress_size);
- unsigned char* rowbuf = malloc(sizeof(unsigned char) * width);
+ unsigned char* row = NULL;
for (int i = 0; i < height; i++) {
- TIFFReadScanline(tiff, bits[i], i, 0);
+ row = &decompress_data[width * i];
+ TIFFReadScanline(tiff, row, i, 0);
}
- free(rowbuf);
- rowbuf = NULL;
+ row = NULL;
char** outdata = malloc(sizeof(char*));
FILE* pnm_stream = open_memstream(outdata, &out->size);
- pbm_writepbm(pnm_stream, bits, width, height, 0);
+ _kds_s2000w_image_converter_write_pnm(decompress_data,
+ decompress_size,
+ width,
+ height,
+ BW,
+ pnm_stream);
fclose(pnm_stream);
pnm_stream = NULL;
out->data = malloc(sizeof(char) * out->size);
memcpy(out->data, *outdata, out->size);
+ free(decompress_data);
+ decompress_data = NULL;
free(*outdata);
*outdata = NULL;
free(outdata);
outdata = NULL;
TIFFClose(tiff);
- for (int i = 0; i < height; i++) {
- free(bits[i]);
- bits[i] = NULL;
- }
- free(bits);
- bits = NULL;
+ tiff = NULL;
}