Exemple #1
0
/**
 * Assign num and compute parents.
 */
void Graph::assignNum( Vertex v )
{
v.num = counter++;
v.visited = true;
for each Vertex w adjacent to v
    if( !w.visited )
    {
        w.parent = v;
        assignNum( w );
    }
}
Exemple #2
0
IOPair::IOPair(char fileName[]){
    FILE *file;
    file = std::fopen(fileName, "r");
    char line[512], intbuf[100], ch;
    int type, nc, nr, maxval, i, j, k, found;
    
    if ((file = fopen(fileName, "r")) == NULL) {
        printf("IMGOPEN: Couldn't open '%s'\n", fileName);
        return;
    }
    
    //Set name, get traits of picture, assign number
    this->entireFile = getNameFromFile(fileName);
    getTraitsFromName();
    assignNum();

    //Scan pnm type information, expecting P5
    std::fgets(line, 511, file);
    sscanf(line, "P%d", &type);
    if (type != 5 && type != 2) {
        printf("ERROR: Only handles pgm files (type P5 or P2)\n");
        fclose(file);
        return;
    }
    
    //Get dimensions of pgm
    fgets(line, 511, file);
    sscanf(line, "%d %d", &nc, &nr);
    this->rows = nr;
    this->cols = nc;
    sscanf(line, "%d", &maxval);
    if (maxval > 255){
        printf("ERROR: Only handles pgm files of 8 bits or less.\n");
        fclose(file);
        return;
    }
    
    
    //Get maxval
    fgets(line, 511, file);
    sscanf(line, "%d", &maxval);
    if (maxval > 255) {
        printf("IMGOPEN: Only handles pgm files of 8 bits or less\n");
        fclose(file);
        return;
    }
    
    //Initialize GrayMap according to dimensions (all 0)
    initGrayMap(nr, nc);

    
    //Read in pixels to grayMap
    if (type==5){
        for (int i=0; i<nr; i++){
            for (int j=0; j<nc; j++){
                int rawPixel = fgetc(file);
                this->grayMap[i][j] = rawPixel/255.0;
            }
        }
    }
    else if (type == 2) {
        for (i = 0; i < nr; i++) {
            for (j = 0; j < nc; j++) {
                
                k = 0;  found = 0;
                while (!found) {
                    ch = (char) fgetc(file);
                    if (ch >= '0' && ch <= '9') {
                        intbuf[k] = ch;  k++;
                    } else {
                        if (k != 0) {
                            intbuf[k] = '\0';
                            found = 1;
                        }
                    }
                }
                int rawPixel = atoi(intbuf);
                this->grayMap[i][j] = rawPixel/255.0;
            }
        }
    }
    fclose(file);
}