help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Loading Images?


From: Joao Cardoso
Subject: Re: Loading Images?
Date: Fri, 27 Sep 96 23:34:56 +0100

address@hidden wrote:
 | From: address@hidden
 | Subject: Loading Images?
 | To: address@hidden
 |
 | How do you load an image into an Octave matrix?  The loadimage command
 | only works with "Octave format" image files.  Any standard image type
 | would be great, such a ppm, gif, tiff.

Not as general as that, but I have a simple C program to deal with
color xpm; you get a matrix with the x,y coordinates of each point,
together with its color. I guess you can convert it to generate Octave
format image files.I enclose it.

I was not able to compile the HDF NCSA library on my system...

Joao

/*
        reads a .xpm file (created/converted for example with xpaint or xv)
        and writes the x and y coordinates of each point, together
        with the point (converted to a number);
        This eases the generation on cluster data, for further analysys.
        The output format is octave compatible.

        use: xpm2cluster in_file > out_file

        v1.0    up to 64 colors;

*/

#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

char f_name[] = "# name: %s\n";
char f_type[] = "# type: matrix\n";
char f_rows[] = "# rows: %d\n";
char f_cols[] = "# columns: 3\n";

main(int argc, char **argv)
{
        FILE*fd;
        char    line[1024], sym[64], c, *tmp;
        int     xdim, ydim, n_clus, pat, no_out=0, no_lab=0;
        int     i,j;
        time_t  clock;
        long    pos;

        if (argc < 2)
        {
                fprintf(stderr,"Use: xpm2octave in_file > out_file\n");
                exit(0);
        }

        if ((fd = fopen(argv[1], "r")) == NULL)
        {
                fprintf(stderr, "Unable to open file %s\n", argv[1]);
                exit(0);
        }

        fgets(line, sizeof(line), fd);  /* header XPM... */
        fgets(line, sizeof(line), fd);  /* header static... */
        fgets(line, sizeof(line), fd);  /* xdim, ydim, ncluster, ...*/
        sscanf(line,"%*1s%d%d%d", &xdim, &ydim, &n_clus);

        fgets(line, sizeof(line), fd);  /* jumps over background color */
        n_clus--;

        for(i=0; i<n_clus; i++)
        {
                fgets(line, sizeof(line), fd);
                sscanf(line,"%*1s%c", &sym[i]);
        }
        sym[i] = '\0';
        pat = 0;

        pos = ftell(fd);

        for (i=0; i<ydim; i++)
        {
                fgets(line, sizeof(line), fd);
                for (j=0; j<xdim; j++)
                {
                        sscanf(line+j+1,"%c", &c);
                        if (strchr(sym, c))
                                pat++;
                }
        }

        *strchr(argv[1], '.') = '\0';
        printf(f_name, argv[1]);
        printf(f_type);
        printf(f_rows, pat-1);
        printf(f_cols);

        fseek(fd, pos, SEEK_SET);

        for (i=0; i<ydim; i++)
        {
                fgets(line, sizeof(line), fd);
                for (j=0; j<xdim; j++)
                {
                        sscanf(line+j+1,"%c", &c);
                        if (tmp = strchr(sym, c))
                                printf("%d %d\t%d\n", j, i, tmp - sym + 1);
                }
        }
}



--
Joao Cardoso, INESC  |  e-mail: address@hidden
R. Jose Falcao 110   |  tel:    + 351 2 2094345
4000 Porto, Portugal |  fax:    + 351 2 2008487


reply via email to

[Prev in Thread] Current Thread [Next in Thread]