! * \brief pixcompCreateFromFile() * * \param[in] filename * \param[in] comptype IFF_DEFAULT, IFF_TIFF_G4, IFF_PNG, IFF_JFIF_JPEG * \return pixc, or NULL on error * * * Notes: * (1) Use %comptype == IFF_DEFAULT to have the compression * type automatically determined. * (2) If the comptype is invalid for this file, the default will * be substit
| 288 | * </pre> |
| 289 | */ |
| 290 | PIXC * |
| 291 | pixcompCreateFromFile(const char *filename, |
| 292 | l_int32 comptype) |
| 293 | { |
| 294 | l_int32 format; |
| 295 | size_t nbytes; |
| 296 | l_uint8 *data; |
| 297 | PIX *pix; |
| 298 | PIXC *pixc; |
| 299 | |
| 300 | PROCNAME("pixcompCreateFromFile"); |
| 301 | |
| 302 | if (!filename) |
| 303 | return (PIXC *)ERROR_PTR("filename not defined", procName, NULL); |
| 304 | if (comptype != IFF_DEFAULT && comptype != IFF_TIFF_G4 && |
| 305 | comptype != IFF_PNG && comptype != IFF_JFIF_JPEG) |
| 306 | return (PIXC *)ERROR_PTR("invalid comptype", procName, NULL); |
| 307 | |
| 308 | findFileFormat(filename, &format); |
| 309 | if (format == IFF_UNKNOWN) { |
| 310 | L_ERROR("unreadable file: %s\n", procName, filename); |
| 311 | return NULL; |
| 312 | } |
| 313 | |
| 314 | /* Can we accept the encoded file directly? Remember that |
| 315 | * png is the "universal" compression type, so if requested |
| 316 | * it takes precedence. Otherwise, if the file is already |
| 317 | * compressed in g4 or jpeg, just accept the string. */ |
| 318 | if ((format == IFF_TIFF_G4 && comptype != IFF_PNG) || |
| 319 | (format == IFF_JFIF_JPEG && comptype != IFF_PNG)) |
| 320 | comptype = format; |
| 321 | if (comptype != IFF_DEFAULT && comptype == format) { |
| 322 | data = l_binaryRead(filename, &nbytes); |
| 323 | if ((pixc = pixcompCreateFromString(data, nbytes, L_INSERT)) == NULL) { |
| 324 | LEPT_FREE(data); |
| 325 | return (PIXC *)ERROR_PTR("pixc not made (string)", procName, NULL); |
| 326 | } |
| 327 | return pixc; |
| 328 | } |
| 329 | |
| 330 | /* Need to recompress in the default format */ |
| 331 | if ((pix = pixRead(filename)) == NULL) |
| 332 | return (PIXC *)ERROR_PTR("pix not read", procName, NULL); |
| 333 | if ((pixc = pixcompCreateFromPix(pix, comptype)) == NULL) { |
| 334 | pixDestroy(&pix); |
| 335 | return (PIXC *)ERROR_PTR("pixc not made", procName, NULL); |
| 336 | } |
| 337 | pixDestroy(&pix); |
| 338 | return pixc; |
| 339 | } |
| 340 | |
| 341 | |
| 342 | /*! |
no test coverage detected