Hello, I am a student who is learning C language. I'm asking you a question regarding the code.

Asked 2 years ago, Updated 2 years ago, 31 views

#include <stdio.h>
#include <stdlib.h>

#define WIDTH 512
#define HEIGHT 512 // WIDTH, defined as 512 for each HEIGHT
Typeef unsigned char BYTE; // Express unsigned char as BYTE 

int main()
{
    FLE *fp_in = 0, *fp_out = 0; // generate a pointer after substituting the garbage value 0 in fp_in and fp_out.
    BYTE **img_in = 0, **img_out = 0; // img_in and img_out are substituted with 0 to create a double pointer
    inti = 0, j = 0; // variable for repeat statements.

    fp_in = fopen ("Lena_512.raw", "rb"); // Lena data is read in binary form and stored at the address that fp_in points to.
    if (fp_in == NULL) {
        printf("File open failed\n"); //fp_in displays the following statement when no data is entered.
    }

    img_in = (BYTE **)malloc(sizeof(BYTE*)*HEIGHT); // allocate memory to img_in in a size of 1 byte by HEIGHT.
    for (i = 0; i < HEIGH; i++) {
        img_in[i] = (BYTE *)malloc(sizeof(BYTE)*HEIGH); // One pointer in each block acts as one row and finally creates a two-dimensional array.
    }
    for (i = 0; i < HEIGH; i++) {
        Read (img_in[i], sizeof(BYTE), WIDTH, fp_in); // read data in 1byte size to img_in[i] as many times as WIDTH.
    }

    img_out = (BYTE **)malloc(sizeof(BYTE*)*HEIGHT); // memory allocation as in img_in.
    for (i = 0; i < HEIGH; i++) {
        img_out[i] = (BYTE *)malloc(sizeof(BYTE)*WIDTH);
    }
    for (i = 0; i < HEIGH; i++) {
        for (j = 0; j < WIDTH; j++) {
            Imported data from img_out[i][j] = img_in[i][j]; //img_in into img_out.
        }
    }
    fp_out = fopen("[Output]Lena(512X512).raw", "wb"); // Write the output of Lena data in binary form and open the file. And save to the address of fp_out.
    if (fp_out == NULL) {
        printf ("File open failed\n"); // The following statement appears when no data is entered. 
    }
    for (i = 0; i < HEIGH; i++) {
        fwrite(img_out[i], sizeof(BYTE), WIDTH, fp_out); //img_out's array contains the contents stored in each of the first elements in column 1 in size 512 (i.e., 512 bytes) < where fp_out points to -> [Output] Lena (512X512).Write data to raw >.
    }

    for (i = 0; i < HEIGH; i++) {
        free(img_in[i]);
        free(img_out[i]);
    }
    free(img_in);
    free(img_out);
    // Turn off dynamic assignments.
    fclose(fp_in);
    fclose(fp_out);
    // A function that closes a file after completing the operation.

    return 0;
}

I'm designing a program called Lena_512.raw that changes the size of the photo file When you build it,

After that, it popped up.

It goes like this.

Press Start without Debug

It appears as above Can I have a solution?ㅠ<

c c++ code

2022-09-22 19:15

1 Answers

Let me explain the wrong code first.

img_in=(BYTE **)malloc(sizeof(BYTE*)*HEIGHT); // allocate memory to img_in by 1 byte size as HEIGHT.

BYTE * is a pointer type, so it is 4 bytes, not 1 Byte.

This means that you have assigned 4 x HEIGHT Bytes.

    for (i = 0; i < HEIGH; i++) {
        img_in[i] = (BYTE *)malloc(sizeof(BYTE)*HEIGH); // One pointer in each block acts as one row and finally creates a two-dimensional array.
    }

It should be WIDTH here. You put the output file correctly, but you put the source file wrong.

img_in[i] = (BYTE *)malloc(sizeof(BYTE)*WIDTH);     

Variables such as img_in, img_in[i] are pointer variables. Because there is no guarantee that malloc will always succeed, you should always check for a valid address before accessing the pointer.

if(NULL != img_in)
...

if(NULL != img_in[i]) 
...
 fp_in = fopen("Lena_512.raw", "rb"); // Lena data is read in binary form and stored at the address indicated by fp_in.
    if (fp_in == NULL) {
        printf("File open failed\n"); //fp_in displays the following statement when no data is entered.
    }
    ...

    fp_out = fopen("[Output]Lena(512X512).raw", "wb"); // Write the output of Lena data in binary form and open the file. And save to the address of fp_out.
    if (fp_out == NULL) {
        printf ("File open failed\n"); // The following statement appears when no data is entered. 
    }

If the file cannot be opened, the proper shutdown process will not be performed, and the following routine will continue to be executed.

If an error occurs or the pointer is NULL, it is not possible to process it normally, so it is recommended to close it with return -1 immediately.

As a result, this code does not reduce the size of the picture. Currently, the code can only read 512 x 512 pixels from the original file and save it as a 512 x 512 image.

To reduce the size of the picture, it is necessary to read the actual width x height of the original file and convert it to 512 x 512 in this case, pixel interpolation is required. It is better to use a library such as openCV than to implement it yourself.


2022-09-22 19:15

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.