Example #1
0
static int JBIGDecode(TIFF* tif, uint8* buffer, tmsize_t size, uint16 s)
{
	struct jbg_dec_state decoder;
	int decodeStatus = 0;
	unsigned char* pImage = NULL;
	(void) size, (void) s;

	if (isFillOrder(tif, tif->tif_dir.td_fillorder))
	{
		TIFFReverseBits(tif->tif_rawdata, tif->tif_rawdatasize);
	}

	jbg_dec_init(&decoder);

#if defined(HAVE_JBG_NEWLEN)
	jbg_newlen(tif->tif_rawdata, (size_t)tif->tif_rawdatasize);
	/*
	 * I do not check the return status of jbg_newlen because even if this
	 * function fails it does not necessarily mean that decoding the image
	 * will fail.  It is generally only needed for received fax images
	 * that do not contain the actual length of the image in the BIE
	 * header.  I do not log when an error occurs because that will cause
	 * problems when converting JBIG encoded TIFF's to
	 * PostScript.  As long as the actual image length is contained in the
	 * BIE header jbg_dec_in should succeed.
	 */
#endif /* HAVE_JBG_NEWLEN */

	decodeStatus = jbg_dec_in(&decoder, (unsigned char*)tif->tif_rawdata,
				  (size_t)tif->tif_rawdatasize, NULL);
	if (JBG_EOK != decodeStatus)
	{
		/*
		 * XXX: JBG_EN constant was defined in pre-2.0 releases of the
		 * JBIG-KIT. Since the 2.0 the error reporting functions were
		 * changed. We will handle both cases here.
		 */
		TIFFErrorExt(tif->tif_clientdata,
			     "JBIG", "Error (%d) decoding: %s",
			     decodeStatus,
#if defined(JBG_EN)
			     jbg_strerror(decodeStatus, JBG_EN)
#else
			     jbg_strerror(decodeStatus)
#endif
			     );
		return 0;
	}

	pImage = jbg_dec_getimage(&decoder, 0);
	_TIFFmemcpy(buffer, pImage, jbg_dec_getsize(&decoder));
	jbg_dec_free(&decoder);
	return 1;
}
Example #2
0
static int JBIGDecode(TIFF* tif, tidata_t buffer, tsize_t size, tsample_t s)
{
        struct jbg_dec_state decoder;
        int decodeStatus = 0;
        unsigned char* pImage = NULL;
	(void) size, (void) s;

        if (isFillOrder(tif, tif->tif_dir.td_fillorder))
        {
                TIFFReverseBits(tif->tif_rawdata, tif->tif_rawdatasize);
        }

        jbg_dec_init(&decoder);

#if defined(HAVE_JBG_NEWLEN)
        jbg_newlen(tif->tif_rawdata, tif->tif_rawdatasize);
        /*
         * I do not check the return status of jbg_newlen because even if this
         * function fails it does not necessarily mean that decoding the image
         * will fail.  It is generally only needed for received fax images
         * that do not contain the actual length of the image in the BIE
         * header.  I do not log when an error occurs because that will cause
         * problems when converting JBIG encoded TIFF's to 
         * PostScript.  As long as the actual image length is contained in the
         * BIE header jbg_dec_in should succeed.
         */
#endif /* HAVE_JBG_NEWLEN */

        decodeStatus = jbg_dec_in(&decoder, tif->tif_rawdata,
                                  tif->tif_rawdatasize, NULL);
        if (JBG_EOK != decodeStatus)
        {
                TIFFError("JBIG", "Error (%d) decoding: %s",
                          decodeStatus, jbg_strerror(decodeStatus, JBG_EN));
                return 0;
        }
        
        pImage = jbg_dec_getimage(&decoder, 0);
        _TIFFmemcpy(buffer, pImage, jbg_dec_getsize(&decoder));
        jbg_dec_free(&decoder);
        return 1;
}
Example #3
0
int main(int argc, char** argv)
{
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("latin1"));
    AppliArgs args(QStringList() << "~help,h" << "~version,v" << "output=1,o");
    QTextStream out(stdout), err(stderr);
    unsigned long int length;
    struct jbg_dec_state sd;
    unsigned char *buffer;
    QString hw("%1 %2\n");
    QByteArray data;
    bool argsErr;
    QFile input;

    // Parse arguments
    argsErr = !args.parse(argc, argv, 1);
    if (argsErr || args.isOptionSet("help")) {
        args.printErrors(err);

        out << QString(_("Usage: %1 [options] <JBIG file>")).arg(args.
            applicationName()) << endl;
        out << _("Available options:") << endl;
        out << _("  --help, -h                Print this help message") << endl;
        out << _("  --output, -o              Select the output file") << endl;
        out << _("  --version, -v             Print the version information") <<
            endl;
        return argsErr ? 1 : 0;
    } else if (args.isOptionSet("version")) {
        out << _("(C) jbgtopbm, 2007 by Aurélien Croc") << endl;
        out << _("This project is under the GNU General Public Licence "
            "version 2") << endl;
        out << _("More information => http://splix.ap2c.org") << endl << endl;
        return 0;
    }


    // Open the input and the output file
    input.setFileName(args.parameter(0));
    if (!input.open(QIODevice::ReadOnly)) {
        err << _("Error: cannot open file ") << args.parameter(0) << endl;
        return -1;
    }
    data = input.readAll();
    buffer = (unsigned char *)data.data();
    length = data.length();
    if (args.isOptionSet("output"))
        output.setFileName(args.optionArg("output", 0));
    else
        output.setFileName("/dev/stdout");
    if (!output.open(QIODevice::WriteOnly)) {
        err << _("Error: cannot open file ") << output.fileName() << endl;
        return -1;
    }


    // Decompress the image
    jbg_dec_init(&sd);
    while (length) {
        unsigned long size;
        int res;

        size = *(unsigned long*)buffer;
        printf("Taille=%lu\n", size);
        buffer += sizeof(size);
        length -= sizeof(size);
        res = jbg_dec_in(&sd, buffer, size, NULL);
        if (res == JBG_EOK) {
            out << _("Processed.") << endl;
            break;
        } else if (res != JBG_EAGAIN) {
            err << _("JBG not ok ") << res << endl;
            break;
        }
        length -= size;
        buffer += size;
    }

    // Store the image
    output.write("P4\n");
    output.write("# Image created by jbgtopbm, (C) 2007 by Aurélien Croc "
        "(AP²C)\n");
    hw = hw.arg(jbg_dec_getwidth(&sd)).arg(jbg_dec_getheight(&sd));
    output.write(hw.toAscii());
    output.write((const char *)jbg_dec_getimage(&sd, 0), jbg_dec_getsize(&sd));
    output.close();

    return 0;
}