sprhawk / leptonica

Automatically exported from code.google.com/p/leptonica
0 stars 0 forks source link

Change pixWrite() to automatically add file extension if missing in filename #21

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Run adaptnorm_reg.c under Windows

What is the expected output? What do you see instead?
Output is as expected but not as convenient as it could be under Windows.

What version of the product are you using? On what operating system?
leptonlib-1.63.
Microsoft Visual Studio 2008 SP1 with latest updates also applied.
Windows XP Pro SP3.

Please provide any additional information below.
Under Windows, it's very difficult to view "junk" images created by prog
programs when they don't have an extension. You have to look thru the code
to determine the appropriate extension type, and then manually rename the
file before you can open it (Or you could unilaterally open all
extensionless files with IrfanView I suppose).

I propose changing pixWrite() in writefile.c so that it automatically adds
an extension to the filename by doing the following:

l_int32
pixWrite(const char  *filename, 
     PIX         *pix, 
     l_int32      format)
{
FILE  *fp;
char  *pextension;
char  *filebuf;

    PROCNAME("pixWrite");

    if (!pix)
        return ERROR_INT("pix not defined", procName, 1);
    if (!filename)
        return ERROR_INT("filename not defined", procName, 1);

    splitPathAtExtension(filename, NULL, &pextension);
    if (strlen(pextension) == 0) {
        if ((filebuf = (char *)CALLOC(strlen(filename) + 10, sizeof(char)))
             == NULL) {
            if (pextension)
                FREE (pextension);
            return ERROR_INT("filebuf not made", procName, 1);
        }

        if (format == IFF_DEFAULT)
            format = pixGetInputFormat(pix);
        if (format == IFF_UNKNOWN) {
            if (pixGetDepth(pix) == 1)
                format = IFF_TIFF_G4;
            else
                format = IFF_PNG;
        }

        strcpy(filebuf, filename);
        strcat(filebuf, ".");
        strcat(filebuf, ImageFileFormatExtensions[format]);
    } else {
        filebuf = (char *) filename;
    }
    if (pextension)
        FREE (pextension);

    fp = fopen(filebuf, "wb+");
    if (filebuf != filename)
        FREE(filebuf);
    if (fp == NULL)
        return ERROR_INT("stream not opened", procName, 1);

    if (pixWriteStream(fp, pix, format)) {
        fclose(fp);
        return ERROR_INT("pix not written to stream", procName, 1);
    }

    fclose(fp);
    return 0;
}

Original issue reported on code.google.com by tomp2...@gmail.com on 3 Dec 2009 at 6:05

GoogleCodeExporter commented 9 years ago
This is an excellent suggestion, even without the issue with windows.
It will be included in 1.64.

Thanks Tom!!

Original comment by dan.bloo...@gmail.com on 3 Dec 2009 at 4:42

GoogleCodeExporter commented 9 years ago
Ooops.   See Issue 14: I mistakenly put responses for pixWrite() there.

Summary here:
  Proposing to keep pixWrite() default operation as is, but to make it easy to have
  pixWrite() modify the input filename when there is no extension.

  I've attached the current version of writefile.c, which has the proposed
  changes to both pixDisplay() and pixWrite().

Original comment by dan.bloo...@gmail.com on 4 Dec 2009 at 12:46

Attachments:

GoogleCodeExporter commented 9 years ago
Latest writefile.c doesn't compile because things like L_DISPLAY_WITH_IV and 
IFF_JP2
are missing. I guess I need the latest imageio.h and some other header file.

Original comment by tomp2...@gmail.com on 4 Dec 2009 at 1:59

GoogleCodeExporter commented 9 years ago
Here are the header files.  There are a significant number of changes to 
readfile.c as 
well (from the jp2 addition).

I can bundle up the current library source and send it if you wish.

Original comment by dan.bloo...@gmail.com on 4 Dec 2009 at 2:07

Attachments:

GoogleCodeExporter commented 9 years ago
Better send the current library source. Things are still changing quite a bit 
for
1.64 I gather? I might as well bite the bullet now and see if everything still
compiles :)

Original comment by tomp2...@gmail.com on 4 Dec 2009 at 2:19

GoogleCodeExporter commented 9 years ago
fixed in 1.64.  Adding filename extensions is OFF by default.

Original comment by dan.bloo...@gmail.com on 3 Jan 2010 at 11:33