2
0

t43_tests.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408
  1. /*
  2. * SpanDSP - a series of DSP components for telephony
  3. *
  4. * t43_tests.c - ITU T.43 JBIG for grey and colour FAX image processing
  5. *
  6. * Written by Steve Underwood <steveu@coppice.org>
  7. *
  8. * Copyright (C) 2011, 2013 Steve Underwood
  9. *
  10. * All rights reserved.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License version 2, as
  14. * published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24. */
  25. /*! \file */
  26. /*! \page t43_tests_page T.43 tests
  27. \section t43_tests_page_sec_1 What does it do
  28. */
  29. #if defined(HAVE_CONFIG_H)
  30. #include "config.h"
  31. #endif
  32. #include <stdlib.h>
  33. #include <stdio.h>
  34. #include <fcntl.h>
  35. #include <unistd.h>
  36. #include <memory.h>
  37. #define SPANDSP_EXPOSE_INTERNAL_STRUCTURES
  38. #include "spandsp.h"
  39. #if defined(SPANDSP_SUPPORT_TIFF_FX) && defined(HAVE_TIF_DIR_H)
  40. #include <tif_dir.h>
  41. #endif
  42. #define IN_FILE_NAME "../test-data/itu/tiff-fx/l04x_02x.tif"
  43. #define OUT_FILE_NAME "t43_tests_receive.tif"
  44. t43_decode_state_t t43;
  45. t85_decode_state_t t85;
  46. lab_params_t lab_param;
  47. int write_row = 0;
  48. typedef struct
  49. {
  50. uint8_t *buf;
  51. int ptr;
  52. } packer_t;
  53. #if defined(SPANDSP_SUPPORT_TIFF_FX) && defined(HAVE_TIF_DIR_H)
  54. /* TIFF-FX related extensions to the tag set supported by libtiff */
  55. static const TIFFFieldInfo tiff_fx_tiff_field_info[] =
  56. {
  57. {TIFFTAG_INDEXED, 1, 1, TIFF_SHORT, FIELD_CUSTOM, false, false, (char *) "Indexed"},
  58. {TIFFTAG_GLOBALPARAMETERSIFD, 1, 1, TIFF_IFD8, FIELD_CUSTOM, false, false, (char *) "GlobalParametersIFD"},
  59. {TIFFTAG_PROFILETYPE, 1, 1, TIFF_LONG, FIELD_CUSTOM, false, false, (char *) "ProfileType"},
  60. {TIFFTAG_FAXPROFILE, 1, 1, TIFF_BYTE, FIELD_CUSTOM, false, false, (char *) "FaxProfile"},
  61. {TIFFTAG_CODINGMETHODS, 1, 1, TIFF_LONG, FIELD_CUSTOM, false, false, (char *) "CodingMethods"},
  62. {TIFFTAG_VERSIONYEAR, 4, 4, TIFF_BYTE, FIELD_CUSTOM, false, false, (char *) "VersionYear"},
  63. {TIFFTAG_MODENUMBER, 1, 1, TIFF_BYTE, FIELD_CUSTOM, false, false, (char *) "ModeNumber"},
  64. {TIFFTAG_DECODE, TIFF_VARIABLE, TIFF_VARIABLE, TIFF_SRATIONAL, FIELD_CUSTOM, false, true, (char *) "Decode"},
  65. {TIFFTAG_IMAGEBASECOLOR, TIFF_VARIABLE, TIFF_VARIABLE, TIFF_SHORT, FIELD_CUSTOM, false, true, (char *) "ImageBaseColor"},
  66. {TIFFTAG_T82OPTIONS, 1, 1, TIFF_LONG, FIELD_CUSTOM, false, false, (char *) "T82Options"},
  67. {TIFFTAG_STRIPROWCOUNTS, TIFF_VARIABLE, TIFF_VARIABLE, TIFF_LONG, FIELD_CUSTOM, false, true, (char *) "StripRowCounts"},
  68. {TIFFTAG_IMAGELAYER, 2, 2, TIFF_LONG, FIELD_CUSTOM, false, false, (char *) "ImageLayer"},
  69. };
  70. static TIFFFieldArray tifffxFieldArray;
  71. static TIFFField tiff_fx_tiff_fields[] =
  72. {
  73. { TIFFTAG_INDEXED, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, (char *) "Indexed" },
  74. { TIFFTAG_GLOBALPARAMETERSIFD, 1, 1, TIFF_IFD8, 0, TIFF_SETGET_IFD8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 0, 0, (char *) "GlobalParametersIFD", &tifffxFieldArray },
  75. { TIFFTAG_PROFILETYPE, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, (char *) "ProfileType", NULL },
  76. { TIFFTAG_FAXPROFILE, 1, 1, TIFF_BYTE, 0, TIFF_SETGET_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, (char *) "FaxProfile", NULL },
  77. { TIFFTAG_CODINGMETHODS, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, (char *) "CodingMethods", NULL },
  78. { TIFFTAG_VERSIONYEAR, 4, 4, TIFF_BYTE, 0, TIFF_SETGET_C0_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, (char *) "VersionYear", NULL },
  79. { TIFFTAG_MODENUMBER, 1, 1, TIFF_BYTE, 0, TIFF_SETGET_UINT8, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, (char *) "ModeNumber", NULL },
  80. { TIFFTAG_DECODE, -1, -1, TIFF_SRATIONAL, 0, TIFF_SETGET_C16_FLOAT, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 1, (char *) "Decode", NULL },
  81. { TIFFTAG_IMAGEBASECOLOR, -1, -1, TIFF_SHORT, 0, TIFF_SETGET_C16_UINT16, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 1, (char *) "ImageBaseColor", NULL },
  82. { TIFFTAG_T82OPTIONS, 1, 1, TIFF_LONG, 0, TIFF_SETGET_UINT32, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, (char *) "T82Options", NULL },
  83. { TIFFTAG_STRIPROWCOUNTS, -1, -1, TIFF_LONG, 0, TIFF_SETGET_C16_UINT32, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 1, (char *) "StripRowCounts", NULL },
  84. { TIFFTAG_IMAGELAYER, 2, 2, TIFF_LONG, 0, TIFF_SETGET_C0_UINT32, TIFF_SETGET_UNDEFINED, FIELD_CUSTOM, 1, 0, (char *) "ImageLayer", NULL },
  85. };
  86. static TIFFFieldArray tiff_fx_field_array = { tfiatOther, 0, 12, tiff_fx_tiff_fields };
  87. #endif
  88. typedef struct
  89. {
  90. TIFF *tif;
  91. int pre_compressed;
  92. uint32_t compressed_image_len;
  93. uint32_t image_width;
  94. uint32_t image_length;
  95. float x_resolution;
  96. float y_resolution;
  97. uint16_t resolution_unit;
  98. uint16_t bits_per_sample;
  99. uint16_t samples_per_pixel;
  100. uint16_t compression;
  101. uint16_t photometric;
  102. int16_t YCbCrSubsampleHoriz;
  103. int16_t YCbCrSubsampleVert;
  104. int16_t planar_config;
  105. int32_t tile_width;
  106. int32_t tile_length;
  107. uint8_t *colour_map;
  108. float lmin;
  109. float lmax;
  110. float amin;
  111. float amax;
  112. float bmin;
  113. float bmax;
  114. } meta_t;
  115. int write_file(meta_t *meta, int page, const uint8_t buf[]);
  116. int read_file(meta_t *meta, int page);
  117. int read_compressed_image(meta_t *meta, uint8_t **buf);
  118. int read_decompressed_image(meta_t *meta, uint8_t **buf);
  119. static int row_write_handler(void *user_data, const uint8_t buf[], size_t len)
  120. {
  121. packer_t *s;
  122. s = (packer_t *) user_data;
  123. memcpy(&s->buf[s->ptr], buf, len);
  124. s->ptr += len;
  125. return 0;
  126. }
  127. /*- End of function --------------------------------------------------------*/
  128. static int t85_comment_handler(void *user_data, const uint8_t buf[], size_t len)
  129. {
  130. if (buf)
  131. printf("Comment (%lu): %s\n", (unsigned long int) len, buf);
  132. else
  133. printf("Comment (%lu): ---\n", (unsigned long int) len);
  134. return 0;
  135. }
  136. /*- End of function --------------------------------------------------------*/
  137. static int row_read_handler(void *user_data, uint8_t row[], size_t len)
  138. {
  139. packer_t *s;
  140. s = (packer_t *) user_data;
  141. memcpy(row, &s->buf[s->ptr], len);
  142. s->ptr += len;
  143. return len;
  144. }
  145. /*- End of function --------------------------------------------------------*/
  146. int write_file(meta_t *meta, int page, const uint8_t buf[])
  147. {
  148. TIFF *tif;
  149. int off;
  150. int i;
  151. time_t now;
  152. struct tm *tm;
  153. char date_buf[50 + 1];
  154. int bytes_per_row;
  155. t85_encode_state_t t85;
  156. t43_encode_state_t t43;
  157. int out_buf_len;
  158. int out_len;
  159. int chunk_len;
  160. uint8_t *out_buf;
  161. uint8_t *out_buf2;
  162. packer_t packer;
  163. #if defined(SPANDSP_SUPPORT_TIFF_FX) && defined(HAVE_TIF_DIR_H)
  164. toff_t diroff;
  165. #endif
  166. tif = meta->tif;
  167. TIFFSetField(tif, TIFFTAG_SUBFILETYPE, FILETYPE_PAGE);
  168. TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, meta->image_width);
  169. /* libtiff requires IMAGELENGTH to be set before SAMPLESPERPIXEL,
  170. or StripOffsets and StripByteCounts will have SAMPLESPERPIXEL values */
  171. TIFFSetField(tif, TIFFTAG_IMAGELENGTH, meta->image_length);
  172. TIFFSetField(tif, TIFFTAG_COMPRESSION, meta->compression);
  173. TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, meta->bits_per_sample);
  174. TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, meta->samples_per_pixel);
  175. TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
  176. TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
  177. TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, meta->image_length);
  178. TIFFSetField(tif, TIFFTAG_XRESOLUTION, meta->x_resolution);
  179. TIFFSetField(tif, TIFFTAG_YRESOLUTION, meta->y_resolution);
  180. TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, meta->resolution_unit);
  181. TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, meta->photometric);
  182. if (meta->samples_per_pixel > 1 && (meta->YCbCrSubsampleHoriz || meta->YCbCrSubsampleVert))
  183. TIFFSetField(tif, TIFFTAG_YCBCRSUBSAMPLING, meta->YCbCrSubsampleHoriz, meta->YCbCrSubsampleVert);
  184. TIFFSetField(tif, TIFFTAG_SOFTWARE, "spandsp");
  185. TIFFSetField(tif, TIFFTAG_IMAGEDESCRIPTION, "Test");
  186. time(&now);
  187. tm = localtime(&now);
  188. sprintf(date_buf,
  189. "%4d/%02d/%02d %02d:%02d:%02d",
  190. tm->tm_year + 1900,
  191. tm->tm_mon + 1,
  192. tm->tm_mday,
  193. tm->tm_hour,
  194. tm->tm_min,
  195. tm->tm_sec);
  196. TIFFSetField(tif, TIFFTAG_DATETIME, date_buf);
  197. TIFFSetField(tif, TIFFTAG_MAKE, "soft-switch.org");
  198. TIFFSetField(tif, TIFFTAG_MODEL, "spandsp");
  199. TIFFSetField(tif, TIFFTAG_HOSTCOMPUTER, "i7.coppice.org");
  200. #if defined(SPANDSP_SUPPORT_TIFF_FX)
  201. /* Make space for this to be filled in later */
  202. TIFFSetField(tif, TIFFTAG_GLOBALPARAMETERSIFD, 0);
  203. #endif
  204. if (meta->pre_compressed)
  205. {
  206. if (TIFFWriteRawStrip(tif, 0, (tdata_t) buf, meta->compressed_image_len) < 0)
  207. printf("Error writing TIFF strip.\n");
  208. }
  209. else
  210. {
  211. switch (meta->compression)
  212. {
  213. case COMPRESSION_T85:
  214. packer.buf = (uint8_t *) buf;
  215. packer.ptr = 0;
  216. t85_encode_init(&t85, meta->image_width, meta->image_length, row_read_handler, &packer);
  217. //if (meta->compression == T4_COMPRESSION_T85_L0)
  218. // t85_encode_set_options(&t85, 256, -1, -1);
  219. out_len = 0;
  220. out_buf_len = 0;
  221. out_buf = NULL;
  222. do
  223. {
  224. if (out_buf_len < out_len + 50000)
  225. {
  226. out_buf_len += 50000;
  227. if ((out_buf2 = realloc(out_buf, out_buf_len)) == NULL)
  228. {
  229. if (out_buf)
  230. free(out_buf);
  231. return -1;
  232. }
  233. out_buf = out_buf2;
  234. }
  235. chunk_len = t85_encode_get(&t85, &out_buf[out_len], 50000);
  236. out_len += chunk_len;
  237. }
  238. while (chunk_len > 0);
  239. if (TIFFWriteRawStrip(tif, 0, out_buf, out_len) < 0)
  240. printf("Error writing TIFF strip.\n");
  241. t85_encode_release(&t85);
  242. free(out_buf);
  243. break;
  244. case COMPRESSION_T43:
  245. packer.buf = (uint8_t *) buf;
  246. packer.ptr = 0;
  247. t43_encode_init(&t43, meta->image_width, meta->image_length, row_read_handler, &packer);
  248. out_len = 0;
  249. out_buf_len = 0;
  250. out_buf = NULL;
  251. do
  252. {
  253. if (out_buf_len < out_len + 50000)
  254. {
  255. out_buf_len += 50000;
  256. if ((out_buf2 = realloc(out_buf, out_buf_len)) == NULL)
  257. {
  258. if (out_buf)
  259. free(out_buf);
  260. return -1;
  261. }
  262. out_buf = out_buf2;
  263. }
  264. chunk_len = t43_encode_get(&t43, &out_buf[out_len], 50000);
  265. out_len += chunk_len;
  266. }
  267. while (chunk_len > 0);
  268. if (TIFFWriteRawStrip(tif, 0, out_buf, out_len) < 0)
  269. printf("Error writing TIFF strip.\n");
  270. t43_encode_release(&t43);
  271. free(out_buf);
  272. break;
  273. default:
  274. bytes_per_row = TIFFScanlineSize(tif);
  275. for (off = 0, i = 0; i < meta->image_length; off += bytes_per_row, i++)
  276. {
  277. if (TIFFWriteScanline(tif, (tdata_t) &buf[off], i, 0) < 0)
  278. printf("Error writing TIFF scan line.\n");
  279. }
  280. break;
  281. }
  282. }
  283. if (!TIFFWriteDirectory(tif))
  284. printf("Failed to write directory.\n");
  285. #if defined(SPANDSP_SUPPORT_TIFF_FX) && defined(HAVE_TIF_DIR_H)
  286. if (!TIFFCreateCustomDirectory(tif, &tiff_fx_field_array))
  287. {
  288. TIFFSetField(tif, TIFFTAG_PROFILETYPE, PROFILETYPE_G3_FAX);
  289. TIFFSetField(tif, TIFFTAG_FAXPROFILE, FAXPROFILE_F);
  290. TIFFSetField(tif, TIFFTAG_CODINGMETHODS, CODINGMETHODS_T4_1D | CODINGMETHODS_T4_2D | CODINGMETHODS_T6);
  291. TIFFSetField(tif, TIFFTAG_VERSIONYEAR, "1998");
  292. TIFFSetField(tif, TIFFTAG_MODENUMBER, 3);
  293. diroff = 0;
  294. if (!TIFFWriteCustomDirectory(tif, &diroff))
  295. printf("Failed to write custom directory.\n");
  296. if (!TIFFSetDirectory(tif, (tdir_t) page))
  297. printf("Failed to set directory.\n");
  298. if (!TIFFSetField(tif, TIFFTAG_GLOBALPARAMETERSIFD, diroff))
  299. printf("Failed to set global parameters IFD.\n");
  300. if (!TIFFWriteDirectory(tif))
  301. printf("Failed to write directory.\n");
  302. }
  303. #endif
  304. return 0;
  305. }
  306. /*- End of function --------------------------------------------------------*/
  307. int read_file(meta_t *meta, int page)
  308. {
  309. #if defined(SPANDSP_SUPPORT_TIFF_FX) && defined(HAVE_TIF_DIR_H)
  310. static const char *tiff_fx_fax_profiles[] =
  311. {
  312. "???",
  313. "profile S",
  314. "profile F",
  315. "profile J",
  316. "profile C",
  317. "profile L",
  318. "profile M"
  319. };
  320. uint8_t parm8;
  321. uint16_t parm16;
  322. uint32_t parm32;
  323. float *fl_parms;
  324. char uu[10];
  325. char *u;
  326. toff_t diroff;
  327. #endif
  328. TIFF *tif;
  329. uint16_t *map_L;
  330. uint16_t *map_a;
  331. uint16_t *map_b;
  332. uint16_t *map_z;
  333. lab_params_t lab;
  334. int entries;
  335. int i;
  336. tif = meta->tif;
  337. printf("Read %d\n", page);
  338. if (!TIFFSetDirectory(tif, (tdir_t) page))
  339. {
  340. printf("Unable to set TIFF directory %d!\n", page);
  341. return -1;
  342. }
  343. meta->image_width = 0;
  344. TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &meta->image_width);
  345. meta->image_length = 0;
  346. TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &meta->image_length);
  347. meta->x_resolution = 200.0f;
  348. TIFFGetField(tif, TIFFTAG_XRESOLUTION, &meta->x_resolution);
  349. meta->y_resolution = 200.0f;
  350. TIFFGetField(tif, TIFFTAG_YRESOLUTION, &meta->y_resolution);
  351. meta->resolution_unit = RESUNIT_INCH;
  352. TIFFGetField(tif, TIFFTAG_RESOLUTIONUNIT, &meta->resolution_unit);
  353. meta->bits_per_sample = 0;
  354. TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &meta->bits_per_sample);
  355. meta->samples_per_pixel = 0;
  356. TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &meta->samples_per_pixel);
  357. meta->compression = 0;
  358. TIFFGetField(tif, TIFFTAG_COMPRESSION, &meta->compression);
  359. meta->photometric = 0;
  360. TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &meta->photometric);
  361. meta->YCbCrSubsampleHoriz = 0;
  362. meta->YCbCrSubsampleVert = 0;
  363. TIFFGetField(tif, TIFFTAG_YCBCRSUBSAMPLING, &meta->YCbCrSubsampleHoriz, &meta->YCbCrSubsampleVert);
  364. meta->planar_config = PLANARCONFIG_CONTIG;
  365. TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &meta->planar_config);
  366. meta->tile_width = 0;
  367. TIFFGetField(tif, TIFFTAG_TILEWIDTH, &meta->tile_width);
  368. meta->tile_length = 0;
  369. TIFFGetField(tif, TIFFTAG_TILELENGTH, &meta->tile_length);
  370. switch (meta->photometric)
  371. {
  372. case PHOTOMETRIC_ITULAB:
  373. meta->lmin = 0.0f;
  374. meta->lmax = 100.0f;
  375. meta->amin = -21760.0f/255.0f; // For 12 bit -348160.0f/4095.0f
  376. meta->amax = 21590.0f/255.0f; // For 12 bit 347990.0f/4095.0f
  377. meta->bmin = -19200.0f/255.0f; // For 12 bit -307200.0f/4095.0f
  378. meta->bmax = 31800.0f/255.0f; // For 12 bit 511800.0f/4095.0f
  379. break;
  380. default:
  381. meta->lmin = 0.0f;
  382. meta->lmax = 0.0f;
  383. meta->amin = 0.0f;
  384. meta->amax = 0.0f;
  385. meta->bmin = 0.0f;
  386. meta->bmax = 0.0f;
  387. break;
  388. }
  389. #if defined(SPANDSP_SUPPORT_TIFF_FX) && defined(HAVE_TIF_DIR_H)
  390. if (TIFFGetField(tif, TIFFTAG_DECODE, &parm16, &fl_parms))
  391. {
  392. meta->lmin = fl_parms[0];
  393. meta->lmax = fl_parms[1];
  394. meta->amin = fl_parms[2];
  395. meta->amax = fl_parms[3];
  396. meta->bmin = fl_parms[4];
  397. meta->bmax = fl_parms[5];
  398. printf("Got decode tag %f %f %f %f %f %f\n", meta->lmin, meta->lmax, meta->amin, meta->amax, meta->bmin, meta->bmax);
  399. }
  400. #endif
  401. #if defined(SPANDSP_SUPPORT_TIFF_FX) && defined(HAVE_TIF_DIR_H)
  402. printf("Trying to get global parameters\n");
  403. if (TIFFGetField(tif, TIFFTAG_GLOBALPARAMETERSIFD, &diroff))
  404. {
  405. printf("Got global parameters - %" PRIu64 "\n", (uint64_t) diroff);
  406. if (!TIFFReadCustomDirectory(tif, diroff, &tiff_fx_field_array))
  407. {
  408. printf("Failed to set global parameters IFD.\n");
  409. }
  410. else
  411. {
  412. if (TIFFGetField(tif, TIFFTAG_PROFILETYPE, &parm32))
  413. printf(" Profile type %u\n", parm32);
  414. if (TIFFGetField(tif, TIFFTAG_FAXPROFILE, &parm8))
  415. printf(" FAX profile %s (%u)\n", tiff_fx_fax_profiles[parm8], parm8);
  416. if (TIFFGetField(tif, TIFFTAG_CODINGMETHODS, &parm32))
  417. printf(" Coding methods 0x%x\n", parm32);
  418. if (TIFFGetField(tif, TIFFTAG_VERSIONYEAR, &u))
  419. {
  420. memcpy(uu, u, 4);
  421. uu[4] = '\0';
  422. printf(" Version year \"%s\"\n", uu);
  423. }
  424. if (TIFFGetField(tif, TIFFTAG_MODENUMBER, &parm8))
  425. printf(" Mode number %u\n", parm8);
  426. }
  427. TIFFSetDirectory(tif, (tdir_t) page);
  428. }
  429. if (TIFFGetField(tif, TIFFTAG_PROFILETYPE, &parm32))
  430. printf("Profile type %u\n", parm32);
  431. if (TIFFGetField(tif, TIFFTAG_FAXPROFILE, &parm8))
  432. printf("FAX profile %s (%u)\n", tiff_fx_fax_profiles[parm8], parm8);
  433. if (TIFFGetField(tif, TIFFTAG_CODINGMETHODS, &parm32))
  434. printf("Coding methods 0x%x\n", parm32);
  435. if (TIFFGetField(tif, TIFFTAG_VERSIONYEAR, &u))
  436. {
  437. memcpy(uu, u, 4);
  438. uu[4] = '\0';
  439. printf("Version year \"%s\"\n", uu);
  440. }
  441. if (TIFFGetField(tif, TIFFTAG_MODENUMBER, &parm8))
  442. printf("Mode number %u\n", parm8);
  443. if (TIFFGetField(tif, TIFFTAG_T82OPTIONS, &parm32))
  444. printf("T.82 options 0x%x\n", parm32);
  445. #endif
  446. map_L = NULL;
  447. map_a = NULL;
  448. map_b = NULL;
  449. map_z = NULL;
  450. if (TIFFGetField(tif, TIFFTAG_COLORMAP, &map_L, &map_a, &map_b, &map_z))
  451. {
  452. entries = 1 << meta->bits_per_sample;
  453. if ((meta->colour_map = malloc(3*entries)))
  454. {
  455. #if 0
  456. /* Sweep the colormap in the proper order */
  457. for (i = 0; i < entries; i++)
  458. {
  459. meta->colour_map[3*i] = (map_L[i] >> 8) & 0xFF;
  460. meta->colour_map[3*i + 1] = (map_a[i] >> 8) & 0xFF;
  461. meta->colour_map[3*i + 2] = (map_b[i] >> 8) & 0xFF;
  462. printf("Map %3d - %5d %5d %5d\n", i, meta->colour_map[3*i], meta->colour_map[3*i + 1], meta->colour_map[3*i + 2]);
  463. }
  464. #else
  465. /* Sweep the colormap in the order that seems to work for l04x_02x.tif */
  466. for (i = 0; i < entries; i++)
  467. {
  468. meta->colour_map[i] = (map_L[i] >> 8) & 0xFF;
  469. meta->colour_map[256 + i] = (map_a[i] >> 8) & 0xFF;
  470. meta->colour_map[2*256 + i] = (map_b[i] >> 8) & 0xFF;
  471. }
  472. #endif
  473. /* The default luminant is D50 */
  474. set_lab_illuminant(&lab_param, 96.422f, 100.000f, 82.521f);
  475. set_lab_gamut(&lab, 0, 100, -85, 85, -75, 125, false);
  476. lab_to_srgb(&lab, meta->colour_map, meta->colour_map, 256);
  477. for (i = 0; i < entries; i++)
  478. printf("Map %3d - %5d %5d %5d\n", i, meta->colour_map[3*i], meta->colour_map[3*i + 1], meta->colour_map[3*i + 2]);
  479. }
  480. }
  481. meta->tif = tif;
  482. return 0;
  483. }
  484. /*- End of function --------------------------------------------------------*/
  485. int read_compressed_image(meta_t *meta, uint8_t **buf)
  486. {
  487. int i;
  488. int len;
  489. int total_len;
  490. int read_len;
  491. int num_strips;
  492. uint8_t *data;
  493. num_strips = TIFFNumberOfStrips(meta->tif);
  494. for (i = 0, total_len = 0; i < num_strips; i++)
  495. {
  496. total_len += TIFFRawStripSize(meta->tif, i);
  497. }
  498. if ((data = malloc(total_len)) == NULL)
  499. return -1;
  500. for (i = 0, read_len = 0; i < num_strips; i++, read_len += len)
  501. {
  502. if ((len = TIFFReadRawStrip(meta->tif, i, &data[read_len], total_len - read_len)) < 0)
  503. {
  504. printf("TIFF read error.\n");
  505. return -1;
  506. }
  507. }
  508. *buf = data;
  509. return total_len;
  510. }
  511. /*- End of function --------------------------------------------------------*/
  512. int read_decompressed_image(meta_t *meta, uint8_t **buf)
  513. {
  514. int bytes_per_row;
  515. int x;
  516. int y;
  517. int xx;
  518. int yy;
  519. int xxx;
  520. int yyy;
  521. int i;
  522. int j;
  523. int result;
  524. int total_raw;
  525. int total_data;
  526. uint8_t *raw_buf;
  527. uint8_t *image_buf;
  528. t85_decode_state_t t85;
  529. t43_decode_state_t t43;
  530. packer_t pack;
  531. logging_state_t *logging;
  532. logging_state_t logging2;
  533. #if 0
  534. uint8_t *jpeg_table;
  535. uint32_t jpeg_table_len;
  536. tsize_t off;
  537. uint32_t w;
  538. uint32_t h;
  539. #endif
  540. image_buf = NULL;
  541. total_data = 0;
  542. switch (meta->compression)
  543. {
  544. case COMPRESSION_T85:
  545. bytes_per_row = (meta->image_width + 7)/8;
  546. total_data = meta->image_length*bytes_per_row;
  547. printf("Total decompressed data %d, %d per row\n", total_data, bytes_per_row);
  548. /* Read the image into memory. */
  549. if ((image_buf = malloc(total_data)) == NULL)
  550. {
  551. printf("Failed to allocated image buffer\n");
  552. return -1;
  553. }
  554. total_raw = read_compressed_image(meta, &raw_buf);
  555. t85_decode_init(&t85, row_write_handler, &pack);
  556. t85_decode_set_comment_handler(&t85, 1000, t85_comment_handler, NULL);
  557. logging = t85_decode_get_logging_state(&t85);
  558. span_log_set_level(logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_FLOW);
  559. pack.buf = image_buf;
  560. pack.ptr = 0;
  561. result = t85_decode_put(&t85, raw_buf, total_raw);
  562. if (result == T4_DECODE_MORE_DATA)
  563. result = t85_decode_put(&t85, NULL, 0);
  564. total_data = t85_decode_get_compressed_image_size(&t85);
  565. printf("Compressed image is %d/%d bytes, %d rows\n", total_raw, total_data/8, write_row);
  566. t85_decode_release(&t85);
  567. free(raw_buf);
  568. break;
  569. case COMPRESSION_T43:
  570. bytes_per_row = meta->samples_per_pixel*meta->image_width;
  571. total_data = meta->image_length*bytes_per_row;
  572. printf("Total decompressed data %d, %d per row\n", total_data, bytes_per_row);
  573. total_data *= 8;
  574. /* Read the image into memory. */
  575. if ((image_buf = malloc(total_data)) == NULL)
  576. printf("Failed to allocated image buffer\n");
  577. total_raw = read_compressed_image(meta, &raw_buf);
  578. t43_decode_init(&t43, row_write_handler, &pack);
  579. t43_decode_set_comment_handler(&t43, 1000, t85_comment_handler, NULL);
  580. logging = t43_decode_get_logging_state(&t43);
  581. span_log_set_level(logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_FLOW);
  582. pack.buf = image_buf;
  583. pack.ptr = 0;
  584. result = t43_decode_put(&t43, raw_buf, total_raw);
  585. if (result == T4_DECODE_MORE_DATA)
  586. result = t43_decode_put(&t43, NULL, 0);
  587. t43_decode_release(&t43);
  588. free(raw_buf);
  589. meta->samples_per_pixel = 1;
  590. meta->photometric = PHOTOMETRIC_RGB;
  591. printf("Image %d x %d pixels\n", meta->image_width, meta->image_length);
  592. break;
  593. case COMPRESSION_JPEG:
  594. if (meta->photometric == PHOTOMETRIC_ITULAB)
  595. {
  596. printf(" ITULAB");
  597. span_log_init(&logging2, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_FLOW, "lab");
  598. bytes_per_row = TIFFScanlineSize(meta->tif);
  599. total_data = meta->image_length*bytes_per_row;
  600. printf("Total decompressed data %d, %d per row\n", total_data, bytes_per_row);
  601. /* Read the image into memory. */
  602. if ((image_buf = malloc(total_data)) == NULL)
  603. printf("Failed to allocated image buffer\n");
  604. #if 0
  605. jpeg_table_len = 0;
  606. if (TIFFGetField(meta->tif, TIFFTAG_JPEGTABLES, &jpeg_table_len, &jpeg_table))
  607. {
  608. total_image_len += (jpeg_table_len - 4);
  609. printf("JPEG tables %u\n", jpeg_table_len);
  610. {
  611. int ii;
  612. printf("YYY1 %d - ", jpeg_table_len);
  613. for (ii = 0; ii < jpeg_table_len; ii++)
  614. printf(" %02x", jpeg_table[ii]);
  615. printf("\n");
  616. }
  617. }
  618. #endif
  619. total_raw = read_compressed_image(meta, &raw_buf);
  620. //if (!t42_itulab_jpeg_to_srgb(&logging2, &lab_param, (tdata_t) image_buf, &off, raw_buf, total_raw, &w, &h, &samples_per_pixel))
  621. {
  622. printf("Failed to convert from ITULAB.\n");
  623. return 1;
  624. }
  625. meta->photometric = PHOTOMETRIC_RGB;
  626. #if 0
  627. total_len = 0;
  628. if (jpeg_table_len > 0)
  629. total_len += jpeg_table_len - 4;
  630. printf("nstrips %d\n", nstrips);
  631. data2 = NULL;
  632. for (i = 0; i < nstrips; i++, total_len += len)
  633. {
  634. total_len = 0;
  635. if (jpeg_table_len > 0)
  636. total_len += jpeg_table_len - 4;
  637. if ((len = TIFFReadRawStrip(tif, i, &data[total_len], total_image_len - total_len)) < 0)
  638. {
  639. printf("TIFF read error.\n");
  640. return -1;
  641. }
  642. if (jpeg_table_len > 0)
  643. {
  644. memcpy(data, jpeg_table, jpeg_table_len - 2);
  645. printf("%02x %02x %02x %02x\n", data[total_len], data[total_len + 1], data[total_len + 2], data[total_len + 3]);
  646. }
  647. totdata = meta->image_width*3000*meta->samples_per_pixel;
  648. data2 = realloc(data2, totdata);
  649. off = total_len;
  650. if (!t42_itulab_jpeg_to_srgb(&logging2, &lab_param, data2, &off, data, off, &w, &h, &samples_per_pixel))
  651. {
  652. printf("Failed to convert from ITULAB.\n");
  653. return 1;
  654. }
  655. }
  656. if (data2)
  657. free(data2);
  658. //exit(2);
  659. if (jpeg_table_len > 0)
  660. memcpy(data, jpeg_table, jpeg_table_len - 2);
  661. if (total_len != total_image_len)
  662. printf("Size mismatch %d %d\n", (int) total_len, (int) total_image_len);
  663. {
  664. int ii;
  665. printf("YYY2 %d - ", jpeg_table_len);
  666. for (ii = 0; ii < 800; ii++)
  667. printf(" %02x", data[ii]);
  668. printf("\n");
  669. }
  670. off = total_len;
  671. len = total_len;
  672. #endif
  673. break;
  674. }
  675. /* Fall through */
  676. default:
  677. if (meta->tile_width > 0)
  678. {
  679. /* The image is tiled, so we need to patch together a bunch of tiles */
  680. switch (meta->planar_config)
  681. {
  682. case PLANARCONFIG_CONTIG:
  683. bytes_per_row = TIFFScanlineSize(meta->tif);
  684. total_data = meta->image_length*bytes_per_row;
  685. printf("Total decompressed data %d, %d per row\n", total_data, bytes_per_row);
  686. /* Read the image into memory. */
  687. if ((image_buf = malloc(total_data)) == NULL)
  688. printf("Failed to allocated image buffer\n");
  689. for (y = 0; y < meta->image_length; y += meta->tile_length)
  690. {
  691. for (x = 0; x < meta->image_width; x += meta->tile_width)
  692. {
  693. uint8_t data[meta->tile_width*meta->tile_length*meta->samples_per_pixel];
  694. TIFFReadTile(meta->tif, data, x, y, 0, 0);
  695. yyy = meta->tile_length;
  696. if (y + meta->tile_length > meta->image_length)
  697. yyy = meta->image_length - y;
  698. xxx = meta->tile_width;
  699. if (x + meta->tile_width > meta->image_width)
  700. xxx = meta->image_width - x;
  701. for (yy = 0; yy < yyy; yy++)
  702. {
  703. for (xx = 0; xx < xxx; xx++)
  704. {
  705. for (j = 0; j < meta->samples_per_pixel; j++)
  706. image_buf[meta->samples_per_pixel*((y + yy)*meta->image_width + x + xx) + j] = data[meta->samples_per_pixel*(yy*meta->tile_width + xx) + j];
  707. }
  708. }
  709. }
  710. }
  711. break;
  712. case PLANARCONFIG_SEPARATE:
  713. bytes_per_row = TIFFScanlineSize(meta->tif);
  714. total_data = meta->samples_per_pixel*meta->image_length*bytes_per_row;
  715. printf("Total decompressed data %d, %d per row\n", total_data, bytes_per_row);
  716. /* Read the image into memory. */
  717. if ((image_buf = malloc(total_data)) == NULL)
  718. printf("Failed to allocated image buffer\n");
  719. for (j = 0; j < meta->samples_per_pixel; j++)
  720. {
  721. for (y = 0; y < meta->image_length; y += meta->tile_length)
  722. {
  723. for (x = 0; x < meta->image_width; x += meta->tile_width)
  724. {
  725. uint8_t data[meta->tile_width*meta->tile_length*meta->samples_per_pixel];
  726. TIFFReadTile(meta->tif, data, x, y, 0, j);
  727. yyy = meta->tile_length;
  728. if (y + meta->tile_length > meta->image_length)
  729. yyy = meta->image_length - y;
  730. xxx = meta->tile_width;
  731. if (x + meta->tile_width > meta->image_width)
  732. xxx = meta->image_width - x;
  733. for (yy = 0; yy < yyy; yy++)
  734. {
  735. for (xx = 0; xx < xxx; xx++)
  736. {
  737. image_buf[meta->samples_per_pixel*((y + yy)*meta->image_width + x + xx) + j] = data[yy*meta->tile_width + xx];
  738. }
  739. }
  740. }
  741. }
  742. }
  743. break;
  744. }
  745. }
  746. else
  747. {
  748. /* There is no tiling to worry about, but we might have planar issues to resolve */
  749. switch (meta->planar_config)
  750. {
  751. case PLANARCONFIG_CONTIG:
  752. bytes_per_row = TIFFScanlineSize(meta->tif);
  753. total_data = meta->image_length*bytes_per_row;
  754. printf("Total decompressed data %d, %d per row\n", total_data, bytes_per_row);
  755. /* Read the image into memory. */
  756. if ((image_buf = malloc(total_data)) == NULL)
  757. printf("Failed to allocated image buffer\n");
  758. for (y = 0; y < meta->image_length; y++)
  759. {
  760. if (TIFFReadScanline(meta->tif, &image_buf[y*bytes_per_row], y, 0) < 0)
  761. return 1;
  762. }
  763. break;
  764. case PLANARCONFIG_SEPARATE:
  765. bytes_per_row = TIFFScanlineSize(meta->tif);
  766. total_data = meta->samples_per_pixel*meta->image_length*bytes_per_row;
  767. printf("Total decompressed data %d, %d per row\n", total_data, bytes_per_row);
  768. /* Read the image into memory. */
  769. if ((image_buf = malloc(total_data)) == NULL)
  770. printf("Failed to allocated image buffer\n");
  771. for (j = 0; j < meta->samples_per_pixel; j++)
  772. {
  773. uint8_t data[bytes_per_row];
  774. for (y = 0; y < meta->image_length; y++)
  775. {
  776. if (TIFFReadScanline(meta->tif, data, y, j) < 0)
  777. return 1;
  778. for (x = 0; x < meta->image_width; x++)
  779. image_buf[meta->samples_per_pixel*(y*bytes_per_row + x) + j] = data[x];
  780. }
  781. }
  782. break;
  783. }
  784. }
  785. break;
  786. }
  787. /* Normalise bi-level images, so they are always in PHOTOMETRIC_MINISWHITE form */
  788. if (image_buf && meta->samples_per_pixel == 1 && meta->bits_per_sample == 1)
  789. {
  790. if (meta->photometric != PHOTOMETRIC_MINISWHITE)
  791. {
  792. for (i = 0; i < total_data; i++)
  793. image_buf[i] = ~image_buf[i];
  794. meta->photometric = PHOTOMETRIC_MINISWHITE;
  795. }
  796. }
  797. *buf = image_buf;
  798. return total_data;
  799. }
  800. /*- End of function --------------------------------------------------------*/
  801. int main(int argc, char *argv[])
  802. {
  803. const char *source_file;
  804. const char *destination_file;
  805. TIFF *tif;
  806. tstrip_t nstrips;
  807. uint32_t totdata;
  808. tsize_t off;
  809. uint8_t *data;
  810. uint8_t *data2;
  811. int row;
  812. int bytes_per_row;
  813. tsize_t outsize;
  814. char *outptr;
  815. int i;
  816. int k;
  817. int x;
  818. int y;
  819. uint64_t start;
  820. uint64_t end;
  821. logging_state_t logging2;
  822. meta_t in_meta;
  823. meta_t meta;
  824. int output_compression;
  825. int page_no;
  826. #if defined(SPANDSP_SUPPORT_TIFF_FX) && defined(HAVE_TIF_DIR_H)
  827. toff_t diroff;
  828. #endif
  829. source_file = (argc > 1) ? argv[1] : IN_FILE_NAME;
  830. printf("Processing '%s'\n", source_file);
  831. destination_file = OUT_FILE_NAME;
  832. output_compression = (argc > 2) ? atoi(argv[2]) : COMPRESSION_CCITT_T6;
  833. #if defined(SPANDSP_SUPPORT_TIFF_FX) && defined(HAVE_TIF_DIR_H)
  834. TIFF_FX_init();
  835. #endif
  836. if ((in_meta.tif = TIFFOpen(source_file, "r")) == NULL)
  837. {
  838. printf("Unable to open '%s'!\n", source_file);
  839. return 1;
  840. }
  841. if ((meta.tif = TIFFOpen(destination_file, "w")) == NULL)
  842. {
  843. printf("Unable to open '%s'!\n", destination_file);
  844. return 1;
  845. }
  846. span_log_init(&logging2, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_FLOW, "lab");
  847. /* The default luminant is D50 */
  848. set_lab_illuminant(&lab_param, 96.422f, 100.000f, 82.521f);
  849. set_lab_gamut(&lab_param, 0, 100, -85, 85, -75, 125, false);
  850. outptr = NULL;
  851. for (page_no = 0; ; page_no++)
  852. {
  853. if (read_file(&in_meta, page_no) < 0)
  854. {
  855. printf("Failed to read from %s\n", source_file);
  856. TIFFClose(in_meta.tif);
  857. TIFFClose(meta.tif);
  858. exit(2);
  859. }
  860. tif = in_meta.tif;
  861. nstrips = TIFFNumberOfStrips(tif);
  862. if (in_meta.compression == output_compression && nstrips == 1 && in_meta.tile_width == 0)
  863. {
  864. /* There might be no need to re-compress the image */
  865. }
  866. else
  867. {
  868. /* It looks like we need to decompress and recompress the image */
  869. }
  870. printf("Width %d, height %d, bits %d, samples %d\n", in_meta.image_width, in_meta.image_length, in_meta.bits_per_sample, in_meta.samples_per_pixel);
  871. totdata = read_decompressed_image(&in_meta, &data);
  872. off = totdata;
  873. bytes_per_row = TIFFScanlineSize(tif);
  874. printf("bits_per_sample %d, samples_per_pixel %d, w %d, h %d\n", in_meta.bits_per_sample, in_meta.samples_per_pixel, in_meta.image_width, in_meta.image_length);
  875. printf("total %d, off %d\n", totdata, (int) off);
  876. switch (in_meta.samples_per_pixel)
  877. {
  878. case 1:
  879. if (in_meta.bits_per_sample == 1)
  880. {
  881. printf("Bi-level\n");
  882. /* We have finished acquiring the image. Now we need to push it out */
  883. meta.pre_compressed = false;
  884. meta.image_width = in_meta.image_width;
  885. meta.image_length = in_meta.image_length;
  886. meta.x_resolution = in_meta.x_resolution;
  887. meta.y_resolution = in_meta.y_resolution;
  888. meta.resolution_unit = in_meta.resolution_unit;
  889. meta.bits_per_sample = in_meta.bits_per_sample;
  890. meta.samples_per_pixel = in_meta.samples_per_pixel;
  891. meta.compression = COMPRESSION_CCITT_T6;
  892. meta.photometric = PHOTOMETRIC_MINISWHITE;
  893. write_file(&meta, page_no, data);
  894. }
  895. else
  896. {
  897. printf("Gray scale, %d bits\n", in_meta.bits_per_sample);
  898. if (in_meta.bits_per_sample == 8)
  899. {
  900. /* Nothing needs to be done */
  901. }
  902. else if (in_meta.bits_per_sample == 16)
  903. {
  904. if ((outptr = malloc(in_meta.image_width*in_meta.image_length)) == NULL)
  905. printf("Failed to allocate buffer\n");
  906. for (i = 0; i < in_meta.image_width*in_meta.image_length; i++)
  907. outptr[i] = data[2*i];
  908. free(data);
  909. data = (uint8_t *) outptr;
  910. }
  911. else
  912. {
  913. uint32_t bitstream;
  914. int bits;
  915. int j;
  916. /* Deal with the messy cases where the number of bits is not a whole
  917. number of bytes. */
  918. if ((outptr = malloc(in_meta.image_width*in_meta.image_length)) == NULL)
  919. printf("Failed to allocate buffer\n");
  920. bitstream = 0;
  921. bits = 0;
  922. j = 0;
  923. for (i = 0; i < in_meta.image_width*in_meta.image_length; i++)
  924. {
  925. while (bits < in_meta.bits_per_sample)
  926. {
  927. bitstream = (bitstream << 8) | data[j++];
  928. bits += 8;
  929. }
  930. outptr[i] = bitstream >> (bits - 8);
  931. bits -= in_meta.bits_per_sample;
  932. }
  933. free(data);
  934. data = (uint8_t *) outptr;
  935. }
  936. off = in_meta.samples_per_pixel*in_meta.image_width*in_meta.image_length;
  937. /* We have finished acquiring the image. Now we need to push it out */
  938. meta.pre_compressed = false;
  939. meta.image_width = in_meta.image_width;
  940. meta.image_length = in_meta.image_length;
  941. meta.x_resolution = in_meta.x_resolution;
  942. meta.y_resolution = in_meta.y_resolution;
  943. meta.resolution_unit = in_meta.resolution_unit;
  944. meta.bits_per_sample = 8;
  945. meta.samples_per_pixel = in_meta.samples_per_pixel;
  946. meta.compression = COMPRESSION_JPEG;
  947. meta.photometric = PHOTOMETRIC_MINISBLACK;
  948. write_file(&meta, page_no, data);
  949. }
  950. break;
  951. case 3:
  952. printf("Photometric is %d\n", in_meta.photometric);
  953. /* We now have the image in memory in RGB form */
  954. if (in_meta.photometric == PHOTOMETRIC_ITULAB)
  955. {
  956. printf("ITU Lab\n");
  957. /* We are already in the ITULAB color space */
  958. if ((outptr = malloc(totdata)) == NULL)
  959. printf("Failed to allocate buffer\n");
  960. lab_to_srgb(&lab_param, (tdata_t) outptr, data, totdata/3);
  961. free(data);
  962. data = (uint8_t *) outptr;
  963. meta.pre_compressed = false;
  964. meta.image_width = in_meta.image_width;
  965. meta.image_length = in_meta.image_length;
  966. meta.x_resolution = in_meta.x_resolution;
  967. meta.y_resolution = in_meta.y_resolution;
  968. meta.resolution_unit = in_meta.resolution_unit;
  969. meta.bits_per_sample = 8;
  970. meta.samples_per_pixel = in_meta.samples_per_pixel;
  971. meta.compression = COMPRESSION_JPEG;
  972. meta.photometric = PHOTOMETRIC_RGB;
  973. }
  974. else
  975. {
  976. #if 1
  977. start = rdtscll();
  978. switch (in_meta.photometric)
  979. {
  980. case PHOTOMETRIC_CIELAB:
  981. printf("CIELAB\n");
  982. /* Convert this to sRGB first */
  983. /* The default luminant is D50 */
  984. set_lab_illuminant(&lab_param, 96.422f, 100.000f, 82.521f);
  985. set_lab_gamut(&lab_param, 0, 100, -128, 127, -128, 127, true);
  986. lab_to_srgb(&lab_param, data, data, in_meta.image_width*in_meta.image_length);
  987. break;
  988. case PHOTOMETRIC_RGB:
  989. printf("RGB\n");
  990. if (in_meta.bits_per_sample == 8)
  991. {
  992. }
  993. else if (in_meta.bits_per_sample == 16)
  994. {
  995. printf("Pack %d to %d\n", totdata, in_meta.samples_per_pixel*in_meta.image_width*in_meta.image_length);
  996. if ((outptr = malloc(in_meta.samples_per_pixel*in_meta.image_width*in_meta.image_length)) == NULL)
  997. printf("Failed to allocate buffer\n");
  998. for (i = 0; i < in_meta.image_width*in_meta.image_length; i++)
  999. {
  1000. outptr[in_meta.samples_per_pixel*i + 0] = (data[in_meta.samples_per_pixel*2*i + 1] << 4) | (data[in_meta.samples_per_pixel*2*i + 0] >> 4);
  1001. outptr[in_meta.samples_per_pixel*i + 1] = (data[in_meta.samples_per_pixel*2*i + 3] << 4) | (data[in_meta.samples_per_pixel*2*i + 2] >> 4);
  1002. outptr[in_meta.samples_per_pixel*i + 2] = (data[in_meta.samples_per_pixel*2*i + 5] << 4) | (data[in_meta.samples_per_pixel*2*i + 4] >> 4);
  1003. }
  1004. free(data);
  1005. data = (uint8_t *) outptr;
  1006. off = in_meta.samples_per_pixel*in_meta.image_width*in_meta.image_length;
  1007. in_meta.bits_per_sample = 8;
  1008. }
  1009. else
  1010. {
  1011. uint32_t bitstream;
  1012. int bits;
  1013. int j;
  1014. /* Deal with the messy cases where the number of bits is not a whole number of bytes. */
  1015. printf("Pack %d to %d\n", totdata, in_meta.samples_per_pixel*in_meta.image_width*in_meta.image_length);
  1016. if ((outptr = malloc(in_meta.samples_per_pixel*in_meta.image_width*in_meta.image_length)) == NULL)
  1017. printf("Failed to allocate buffer\n");
  1018. bitstream = 0;
  1019. bits = 0;
  1020. j = 0;
  1021. for (i = 0; i < in_meta.image_width*in_meta.image_length; i++)
  1022. {
  1023. for (k = 0; k < in_meta.samples_per_pixel; k++)
  1024. {
  1025. while (bits < in_meta.bits_per_sample)
  1026. {
  1027. bitstream = (bitstream << 8) | data[j++];
  1028. bits += 8;
  1029. }
  1030. outptr[in_meta.samples_per_pixel*i + k] = bitstream >> (bits - 8);
  1031. bits -= in_meta.bits_per_sample;
  1032. }
  1033. }
  1034. free(data);
  1035. data = (uint8_t *) outptr;
  1036. off = in_meta.samples_per_pixel*in_meta.image_width*in_meta.image_length;
  1037. in_meta.bits_per_sample = 8;
  1038. }
  1039. break;
  1040. }
  1041. #if 0
  1042. /* The default luminant is D50 */
  1043. set_lab_illuminant(&lab_param, 96.422f, 100.000f, 82.521f);
  1044. set_lab_gamut(&lab_param, 0, 100, -85, 85, -75, 125, false);
  1045. if (!t42_srgb_to_itulab_jpeg(&logging2, &lab_param, (tdata_t) &outptr, &outsize, data, off, in_meta.image_width, in_meta.image_length, 3))
  1046. {
  1047. printf("Failed to convert to ITULAB (B).\n");
  1048. return 1;
  1049. }
  1050. end = rdtscll();
  1051. printf("Duration %" PRIu64 "\n", end - start);
  1052. free(data);
  1053. data = (uint8_t *) outptr;
  1054. off = outsize;
  1055. #endif
  1056. #endif
  1057. meta.pre_compressed = false;
  1058. meta.image_width = in_meta.image_width;
  1059. meta.image_length = in_meta.image_length;
  1060. meta.x_resolution = in_meta.x_resolution;
  1061. meta.y_resolution = in_meta.y_resolution;
  1062. meta.resolution_unit = in_meta.resolution_unit;
  1063. meta.bits_per_sample = 8;
  1064. meta.samples_per_pixel = in_meta.samples_per_pixel;
  1065. meta.compression = COMPRESSION_JPEG;
  1066. meta.photometric = PHOTOMETRIC_RGB;
  1067. }
  1068. write_file(&meta, page_no, data);
  1069. break;
  1070. case 4:
  1071. printf("Photometric is %d\n", in_meta.photometric);
  1072. /* We now have the image in memory in RGB form */
  1073. if (in_meta.photometric == PHOTOMETRIC_ITULAB)
  1074. {
  1075. /* We are already in the ITULAB color space */
  1076. #if 0
  1077. if (!t42_itulab_to_itulab(&logging2, (tdata_t) &outptr, &outsize, data, off, in_meta.image_width, in_meta.image_length, 3))
  1078. {
  1079. printf("Failed to convert to ITULAB (C).\n");
  1080. return 1;
  1081. }
  1082. #else
  1083. outsize = 0;
  1084. #endif
  1085. free(data);
  1086. data = (uint8_t *) outptr;
  1087. off = outsize;
  1088. }
  1089. else
  1090. {
  1091. start = rdtscll();
  1092. switch (in_meta.photometric)
  1093. {
  1094. case PHOTOMETRIC_CIELAB:
  1095. printf("CIELAB\n");
  1096. /* TODO: This doesn't work yet */
  1097. /* Convert this to sRGB first */
  1098. /* The default luminant is D50 */
  1099. set_lab_illuminant(&lab_param, 96.422f, 100.000f, 82.521f);
  1100. set_lab_gamut(&lab_param, 0, 100, -128, 127, -128, 127, true);
  1101. lab_to_srgb(&lab_param, data, data, in_meta.image_width*in_meta.image_length);
  1102. break;
  1103. case PHOTOMETRIC_SEPARATED:
  1104. for (y = 0; y < in_meta.image_length; y++)
  1105. {
  1106. for (x = 0; x < in_meta.image_width; x++)
  1107. {
  1108. k = data[(y*in_meta.image_width + x)*4 + 0] + data[(y*in_meta.image_width + x)*4 + 3];
  1109. if (k > 255)
  1110. k = 255;
  1111. data[(y*in_meta.image_width + x)*3 + 0] = 255 - k;
  1112. k = data[(y*in_meta.image_width + x)*4 + 1] + data[(y*in_meta.image_width + x)*4 + 3];
  1113. if (k > 255)
  1114. k = 255;
  1115. data[(y*in_meta.image_width + x)*3 + 1] = 255 - k;
  1116. k = data[(y*in_meta.image_width + x)*4 + 2] + data[(y*in_meta.image_width + x)*4 + 3];
  1117. if (k > 255)
  1118. k = 255;
  1119. data[(y*in_meta.image_width + x)*3 + 2] = 255 - k;
  1120. }
  1121. }
  1122. off = 3*in_meta.image_width*in_meta.image_length;
  1123. in_meta.bits_per_sample = 8;
  1124. break;
  1125. }
  1126. /* The default luminant is D50 */
  1127. set_lab_illuminant(&lab_param, 96.422f, 100.000f, 82.521f);
  1128. set_lab_gamut(&lab_param, 0, 100, -85, 85, -75, 125, false);
  1129. //if (!t42_srgb_to_itulab_jpeg(&logging2, &lab_param, (tdata_t) &outptr, &outsize, data, off, in_meta.image_width, in_meta.image_length, 3))
  1130. {
  1131. printf("Failed to convert to ITULAB (D).\n");
  1132. return 1;
  1133. }
  1134. end = rdtscll();
  1135. printf("Duration %" PRIu64 "\n", end - start);
  1136. off = outsize;
  1137. in_meta.bits_per_sample = 8;
  1138. }
  1139. meta.pre_compressed = false;
  1140. meta.image_width = in_meta.image_width;
  1141. meta.image_length = in_meta.image_length;
  1142. meta.x_resolution = in_meta.x_resolution;
  1143. meta.y_resolution = in_meta.y_resolution;
  1144. meta.resolution_unit = in_meta.resolution_unit;
  1145. meta.bits_per_sample = 8;
  1146. meta.samples_per_pixel = 3;
  1147. meta.compression = COMPRESSION_JPEG;
  1148. meta.photometric = PHOTOMETRIC_RGB;
  1149. write_file(&meta, page_no, data);
  1150. break;
  1151. }
  1152. }
  1153. printf("XXX - image is %d by %d, %d bytes\n", in_meta.image_width, in_meta.image_length, (int) off);
  1154. /* We now have the image in memory in ITULAB form */
  1155. meta.pre_compressed = false;
  1156. meta.compressed_image_len = off;
  1157. meta.image_width = in_meta.image_width;
  1158. meta.image_length = in_meta.image_length;
  1159. meta.x_resolution = in_meta.x_resolution;
  1160. meta.y_resolution = in_meta.y_resolution;
  1161. meta.resolution_unit = in_meta.resolution_unit;
  1162. meta.bits_per_sample = 8;
  1163. meta.samples_per_pixel = 3;
  1164. meta.compression = COMPRESSION_JPEG;
  1165. #if 1
  1166. meta.photometric = PHOTOMETRIC_RGB;
  1167. #elif 1
  1168. /* Most image processors won't know what to do with the ITULAB colorspace.
  1169. So we'll be converting it to RGB for portability. */
  1170. /* If PHOTOMETRIC_ITULAB is not available the admin cannot enable color fax anyway.
  1171. This is done so that older libtiffs without it can build fine. */
  1172. meta.photometric = PHOTOMETRIC_ITULAB;
  1173. #else
  1174. meta.photometric = PHOTOMETRIC_YCBCR;
  1175. #endif
  1176. meta.YCbCrSubsampleHoriz = in_meta.YCbCrSubsampleHoriz;
  1177. meta.YCbCrSubsampleVert = in_meta.YCbCrSubsampleVert;
  1178. if ((tif = TIFFOpen(destination_file, "w")) == NULL)
  1179. {
  1180. printf("Unable to open '%s'!\n", destination_file);
  1181. return 1;
  1182. }
  1183. TIFFSetField(tif, TIFFTAG_SUBFILETYPE, FILETYPE_PAGE);
  1184. TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, meta.image_width);
  1185. /* libtiff requires IMAGELENGTH to be set before SAMPLESPERPIXEL,
  1186. or StripOffsets and StripByteCounts will have SAMPLESPERPIXEL values */
  1187. TIFFSetField(tif, TIFFTAG_IMAGELENGTH, meta.image_length);
  1188. TIFFSetField(tif, TIFFTAG_COMPRESSION, meta.compression);
  1189. TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, meta.bits_per_sample);
  1190. TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, meta.samples_per_pixel);
  1191. TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
  1192. TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
  1193. TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, meta.image_length);
  1194. TIFFSetField(tif, TIFFTAG_XRESOLUTION, meta.x_resolution);
  1195. TIFFSetField(tif, TIFFTAG_YRESOLUTION, meta.y_resolution);
  1196. TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, meta.resolution_unit);
  1197. TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, meta.photometric);
  1198. if (meta.samples_per_pixel > 1 && (meta.YCbCrSubsampleHoriz || meta.YCbCrSubsampleVert))
  1199. TIFFSetField(tif, TIFFTAG_YCBCRSUBSAMPLING, meta.YCbCrSubsampleHoriz, meta.YCbCrSubsampleVert);
  1200. TIFFSetField(tif, TIFFTAG_SOFTWARE, "spandsp");
  1201. TIFFSetField(tif, TIFFTAG_IMAGEDESCRIPTION, "Test");
  1202. TIFFSetField(tif, TIFFTAG_DATETIME, "2011/02/03 12:30:45");
  1203. TIFFSetField(tif, TIFFTAG_MAKE, "soft-switch.org");
  1204. TIFFSetField(tif, TIFFTAG_MODEL, "spandsp");
  1205. TIFFSetField(tif, TIFFTAG_HOSTCOMPUTER, "i7.coppice.org");
  1206. #if defined(SPANDSP_SUPPORT_TIFF_FX) && defined(HAVE_TIF_DIR_H)
  1207. /* Make space for this to be filled in later */
  1208. TIFFSetField(tif, TIFFTAG_GLOBALPARAMETERSIFD, 0);
  1209. #endif
  1210. if (meta.pre_compressed)
  1211. {
  1212. if (TIFFWriteRawStrip(tif, 0, (tdata_t) data, meta.compressed_image_len) == -1)
  1213. {
  1214. printf("Write error to TIFF file\n");
  1215. return 1;
  1216. }
  1217. free(data);
  1218. }
  1219. else
  1220. {
  1221. if (in_meta.samples_per_pixel > 1)
  1222. {
  1223. bytes_per_row = ((meta.bits_per_sample + 7)/8)*meta.image_width*meta.samples_per_pixel;
  1224. totdata = meta.image_length*bytes_per_row;
  1225. /* The default luminant is D50 */
  1226. set_lab_illuminant(&lab_param, 96.422f, 100.000f, 82.521f);
  1227. set_lab_gamut(&lab_param, 0, 100, -85, 85, -75, 125, false);
  1228. #if 0
  1229. start = rdtscll();
  1230. data2 = NULL;
  1231. totdata = 0;
  1232. if (!t42_itulab_to_JPEG(&logging2, &lab_param, (void **) &data2, &totdata, data, off))
  1233. {
  1234. printf("Failed to convert from ITULAB (A).\n");
  1235. return 1;
  1236. }
  1237. end = rdtscll();
  1238. printf("Duration %" PRIu64 "\n", end - start);
  1239. printf("Compressed length %d (%p)\n", totdata, data2);
  1240. if (TIFFWriteRawStrip(tif, 0, data2, totdata) < 0)
  1241. {
  1242. printf("Failed to convert from ITULAB (B).\n");
  1243. return 1;
  1244. }
  1245. free(data);
  1246. data = data2;
  1247. #elif 1
  1248. if ((data2 = malloc(totdata)) == NULL)
  1249. {
  1250. printf("Failed to allocate buffer\n");
  1251. exit(2);
  1252. }
  1253. start = rdtscll();
  1254. //if (!t42_itulab_jpeg_to_srgb(&logging2, &lab_param, data2, &off, data, off, &meta.image_width, &meta.image_length, &meta.samples_per_pixel))
  1255. {
  1256. printf("Failed to convert from ITULAB.\n");
  1257. return 1;
  1258. }
  1259. end = rdtscll();
  1260. printf("Duration %" PRIu64 "\n", end - start);
  1261. free(data);
  1262. data = data2;
  1263. #endif
  1264. }
  1265. off = 0;
  1266. bytes_per_row = ((meta.bits_per_sample + 7)/8)*meta.image_width*meta.samples_per_pixel;
  1267. for (row = 0; row < meta.image_length; row++)
  1268. {
  1269. if (TIFFWriteScanline(tif, &data[off], row, 0) < 0)
  1270. return 1;
  1271. off += bytes_per_row;
  1272. }
  1273. free(data);
  1274. }
  1275. if (!TIFFWriteDirectory(tif))
  1276. printf("Failed to write directory.\n");
  1277. #if defined(SPANDSP_SUPPORT_TIFF_FX) && defined(HAVE_TIF_DIR_H)
  1278. if (!TIFFCreateCustomDirectory(tif, &tiff_fx_field_array))
  1279. {
  1280. TIFFSetField(tif, TIFFTAG_PROFILETYPE, PROFILETYPE_G3_FAX);
  1281. TIFFSetField(tif, TIFFTAG_FAXPROFILE, FAXPROFILE_F);
  1282. TIFFSetField(tif, TIFFTAG_CODINGMETHODS, CODINGMETHODS_T4_1D | CODINGMETHODS_T4_2D | CODINGMETHODS_T6);
  1283. TIFFSetField(tif, TIFFTAG_VERSIONYEAR, "1998");
  1284. TIFFSetField(tif, TIFFTAG_MODENUMBER, 3);
  1285. diroff = 0;
  1286. if (!TIFFWriteCustomDirectory(tif, &diroff))
  1287. printf("Failed to write custom directory.\n");
  1288. if (!TIFFSetDirectory(tif, (tdir_t) page_no))
  1289. printf("Failed to set directory.\n");
  1290. if (!TIFFSetField(tif, TIFFTAG_GLOBALPARAMETERSIFD, diroff))
  1291. printf("Failed to set global parameters IFD.\n");
  1292. if (!TIFFWriteDirectory(tif))
  1293. printf("Failed to write directory.\n");
  1294. }
  1295. #endif
  1296. TIFFClose(tif);
  1297. printf("Done!\n");
  1298. return 0;
  1299. }
  1300. /*- End of function --------------------------------------------------------*/
  1301. /*- End of file ------------------------------------------------------------*/