Korean, Edit

Displaying and Outputting Images in C Language

Higher category : 【C Language】 Table of Contents for C Language


a. Github



Q. Answer the following questions about data representation and application.

1. Generate BMP files of various colors (single, 4-bit, 8-bit, 24-bit) using a paint program and represent them in HEX codes.


drawing


1 bit


drawing


4 bit


drawing


8 bit


drawing


24 bit


drawing



2. Exercise 

#include <stdio.h>
#include <stdlib.h>
#define width 100
#define height 100

unsigned char h[54];
unsigned char R[width][height], G[width][height], B[width][height];

int main(int argc, char *argv[]) {
    int i, j;
    FILE *in = fopen("image.bmp", "rb");	
    FILE *out = fopen("trans.bmp", "wb");
    // *in is a pointer to the memory address that holds image.bmp, which is a FILE variable.
    // rb stands for read binary, wb stands for write binary.
    
    if(in == NULL){	
        printf("File does not exist.");
        return 0;		
    }	
    
    for(i = 0; i < 54; i ++)	h[i] = getc(in);
    
    for(i = 0; i < width; i ++)
        for(j = 0; j < height; j ++){
            B[i][j] = getc(in);
            G[i][j] = getc(in);
            R[i][j] = getc(in);
        }
    for(i = 0; i < width; i ++)
        for(j = 0; j < height; j ++){
            if(B[i][j] == 0 && G[i][j] == 0 && R[i][j] == 0){
                B[i][j] = 255;
                G[i][j] = 255;
                R[i][j] = 255;			
            }
            else{
                B[i][j] = 0;
                G[i][j] = 0;


                R[i][j] = 0;
            }
        }

    for(i = 0; i < 54; i ++)	    fputc(h[i], out);
    for(i = 0; i < width; i ++)
        for(j = 0; j < height; j ++){
            fputc(B[i][j], out);	
            fputc(G[i][j], out);	
            fputc(R[i][j], out);	
        }
    fclose(in);
    fclose(out);
    return 0;
}


drawing


drawing


drawing



3. Understanding Image Technology

⑴ File Format

① BMP (bitmap) images store all information as a collection of bits.

② Formats like PNG, GIF, TIFF, JPEG use lossy compression techniques, causing information loss.

③ GIF reduces the color palette to 8-bit, resulting in significant data loss.

④ Lossy compression techniques vary in efficiency depending on color frequency and patterns in the image.

⑤ Run-length coding is used when reading images again.

⑵ Image Units

① Pixel is the smallest unit of an image.

② In bitmaps, pixel depth (color depth) is usually 24 bits.

○ True Color: 24-bit, 32-bit

○ High Color: 16-bit

○ 256 Color: 8-bit

○ In 24-bit, 8 bits are allocated for each of R, G, and B (8 bits represent 2 hexadecimal digits).

○ Example: 00 00 00 represents black, FF 00 00 represents red.

③ Resolution: The number of pixels in an image.

⑶ Image Binarization

① Header: It contains file format, pixel depth, and resolution information. (Static)

② Body: (Dynamic)



3. Calculate the size of the four types of files created in 1 and compare them with the actual sizes.

⑴ Header: 30,054 bytes (actual result) - 30,000 bytes = 54 bytes

⑵ Body: 24 bits/pixel (pixel depth) × 100 × 100 pixels (resolution) = 30,000 bytes

⑶ Actual disk allocation bits: 32,768 bytes (This is to reserve a larger space and prevent information loss.)


drawing



4. Compare and describe the representation methods of images and sounds.

Image Sound
Pixel Sample
Resolution (10-20 million pixels) Sampling rate (44.1 kHz, naturalness)
Color depth: 8, 16, 24, 32-bit Sound depth: 16, 32-bit
BMP // JPG, PNG WAV // MP3, WMA, OGG
Images and sounds are very similar. They can be combined, e.g., video (frames).  


Input: 2013.07.11 21:52

Modified: 2023.06.16 11:53

results matching ""

    No results matching ""