nkkav / libpnmio

C library for reading and writing PNM (PBM/PGM/PPM) and PFM images.
Other
17 stars 9 forks source link

Bitsize? #5

Closed zvezdochiot closed 2 years ago

zvezdochiot commented 3 years ago

https://github.com/nkkav/libpnmio/blob/fcb31cf3eadc5b48ec9cf867c2dc3c32ed2717b1/src/pnmio.h#L53-L59

https://github.com/nkkav/libpnmio/blob/fcb31cf3eadc5b48ec9cf867c2dc3c32ed2717b1/src/rnwimg.c#L123-L132

https://github.com/nkkav/libpnmio/blob/fcb31cf3eadc5b48ec9cf867c2dc3c32ed2717b1/src/rnwimg.c#L154-L162

I think this construction is incorrect, read_ {pbm, pgm, ppm, pfm} _header should also return the bitsize parameter, which should already be used for malloc.

nkkav commented 3 years ago

From these functions, I can return the number of bytes (not bits) that will need to be allocated with malloc.

I should also check whether malloc succeeded.

zvezdochiot commented 3 years ago

@nkkav say:

From these functions, I can return the number of bytes (not bits) that will need to be allocated with malloc.

I'm not sure my point is understood.

https://github.com/nkkav/libpnmio/blob/fcb31cf3eadc5b48ec9cf867c2dc3c32ed2717b1/src/rnwimg.c#L128 https://github.com/nkkav/libpnmio/blob/fcb31cf3eadc5b48ec9cf867c2dc3c32ed2717b1/src/rnwimg.c#L159

to

    read_ppm_header(imgin_file, &x_dim, &y_dim, &img_colors, &enable_ascii, &bit_size);
    img_data = malloc(bit_size);
nkkav commented 3 years ago

your point is understood. You prefer to not return but to update the pass-by-reference argument.

malloc takes number of bytes as input, not bits. sizeof() returns bytes, not bits. malloc() allocates bytes in a byte-addressable memory model, not bits. There is no bit concept in C except "bit-fields" (where the concept is defined very specifically and is not really about storage bits and the necessary value of CHAR_BIT. Bit-accurate types in the strong sense are only under discussion for C23.

zvezdochiot commented 3 years ago

@nkkav say:

malloc takes number of bytes as input, not bits.

The name doesn't matter. You know better. My English is bad. (https://translate.google.ru/)

nkkav commented 2 years ago

Finally done. Thank you