Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6105755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:00:19+00:00 2026-05-23T14:00:19+00:00

So I had one large function that worked. Then I decided to modify it

  • 0

So I had one large function that worked. Then I decided to modify it and now I am just printing out a black square. I am attaching my code to see if any one would understand what is happening. These three function where once large function:
Function 1

int
readpgm (pgm_type * header, char input[80], char output[80])
{
    FILE *instream;

    int size, read;
    instream = fopen (input, "rb");

    fileChecker (instream);

    fscanf (instream, "%2s%d%d%d", header->filetype, &header->width,
        &header->height, &header->maxgray);

    if (!header->filetype[0] == 'P' || !header->filetype[1] == '5') {
        fatal ("Incorrect Type");
    }

    size = header->width * header->height;

    header->p = malloc (size * sizeof (char));

    read = fread (header->p, 1, size, instream);
    if (read != size) {
        fatal ("Incorrect Size");
    }

    return size;
}

void
crop (pgm_type * header, char output[80])
{
    printf ("Height: %i, Width: %i, Total pixels: %i \n", header->height,
        header->width, header->height * header->width);

    int temp, y1, y2, x1, x2, wide, high;

    printf ("Please Enter x1 y1 x2 y2 \n");


    scanf ("%i %i %i %i", &x1, &y1, &x2, &y2);
    if (y1 > y2) {
        temp = y1;
        y1 = y2;
        y2 = temp;
    }
    if (x1 > x2) {
        temp = x1;
        x1 = x2;
        x2 = temp;
    }
    wide = x2 - x1 + 1;
    high = y2 - y1 + 1;

    printFile (wide, high, x1, x2, y1, y1, header, output);
}

void
printFile (int wide, int high, int x1, int x2, int y1, int y2,
       pgm_type * header, char output[80])
{
    FILE *outstream;

    outstream = fopen (output, "wb");

    fileChecker (outstream);

    fprintf (outstream, "%2s\n%i %i\n%i\n", header->filetype, wide, high,
         header->maxgray);

    pixel image[header->height][header->width];
    pixel *pix = malloc ((wide * high) * sizeof (char));

    int a = 0;

    for (int b = 0; b < header->height; ++b) {
        for (int c = 0; c < header->width; ++c) {
            image[b][c] = header->p[a];
            ++a;
        }
    }

    int k = 0;
    for (int i = y1; i <= y2; ++i) {
        for (int j = x1; j <= x2; ++j) {
            pix[k] = image[i][j];
            ++k;
        }
    }

    fwrite (pix, 1, (wide * high) * sizeof (pixel), outstream);
    free (pix);
    fclose (outstream);
}

The first two function are call in main.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T14:00:20+00:00Added an answer on May 23, 2026 at 2:00 pm

    This works for me.

    //gcc c2.c -std=c99  -g -Wall -Wextra
    #include <stdio.h>
    #include <malloc.h>
    #include <assert.h>
    
    typedef struct pgm_type pgm_type;
    struct pgm_type{
      char filetype[2];
      int width,height,maxgray;
      unsigned char*p;
    };
    
    
    int
    readpgm (pgm_type * header, char*fn)
    {
        FILE *instream;
    
        int size, read;
        instream = fopen (fn,"rb");
    
        assert(instream);
    
        fscanf (instream, "%2s%d%d%d", header->filetype, &header->width,
            &header->height, &header->maxgray);
        printf("%2s %d %d %d\n",header->filetype,header->width,
           header->height,header->maxgray);
    
        if (!header->filetype[0] == 'P' || !header->filetype[1] == '5') {
          printf ("Incorrect Type");
        }
    
        size = header->width * header->height;
    
        header->p = malloc (size * sizeof (char));
    
        read = fread (header->p, 1, size, instream);
        if (read != size) {
          printf ("Incorrect Size");
        }
    
        return size;
    }
    
    void
    printFile (int x1, int x2, int y1, int y2,pgm_type * header, char*fn)
    {
        FILE *outstream;
        int wide=x2-x1+1,high=y2-y1+1;
        printf("cropping to %dx%d\n",wide,high);
        outstream = fopen (fn, "wb");
    
        assert (outstream);
    
        fprintf (outstream, "%2s\n%i %i\n%i\n", header->filetype, wide, high,
             header->maxgray);
    
        unsigned char image[header->height][header->width];
        unsigned char *pix = malloc ((wide * high) * sizeof (char));
    
        int a = 0;
    
        for (int b = 0; b < header->height; ++b) {
            for (int c = 0; c < header->width; ++c) {
                image[b][c] = header->p[a];
                ++a;
            }
        }
    
        int k = 0;
        for (int j = y1; j <= y2; ++j) {
            for (int i = x1; i <= x2; ++i) {
                pix[k] = image[j][i];
                ++k;
            }
        }
    
        fwrite (pix, wide,high, outstream);
        free (pix);
        fclose (outstream);
    }
    
    
    int
    main()
    {
      pgm_type h;
      readpgm(&h,"lena.pgm");
      printFile(64,129,32,230,&h,"o2.pgm");
      return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had only one function to Add another row to the table id=tblOtherParties and
At one point I had a nice little compression utility that smashed my Delphi
I had read somewhere about one specific feature that is present in awk but
If you had a fairly large amount of data (say a million rows) that
I'm teaching someone better practices by refactoring a large project they worked on. One
I had one Git repository (A) which contains the development of a project until
I've messed with Access a little bit in the past, had one class on
I had created one HTML page for my experiance. In this i had use
I had posted one question earlier jQuery inconsistency in setting readonly attribute in IE-8
For one reason or another I had to empty folder in my subversion repository

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.