【无标题】

int main(int argc, const char *argv[]) {
    FILE *fp = fopen("./rising_freedom.bmp", "r+");
    if (NULL == fp) {
        perror("fopen");
        return 1;
    }

    int bmp_size = 0, bmp_h = 0, bmp_w = 0;
    fseek(fp, 2, SEEK_SET);
    fread(&bmp_size, 4, 1, fp);
    printf("The BMP size is %d\n", bmp_size);

    fseek(fp, 18, SEEK_SET);
    fread(&bmp_h, 4, 1, fp);
    bmp_h=bmp_h*2;
    fseek(fp, 18, SEEK_SET);
    fwrite(&bmp_h, 4, 1, fp);
    fseek(fp, 22, SEEK_SET);
    fread(&bmp_w, 4, 1, fp);
    bmp_w=bmp_w*2;
    fseek(fp, 22, SEEK_SET);
    fwrite(&bmp_w, 4, 1, fp);
    printf("The image size is %d*%d\n", bmp_w, bmp_h);

    unsigned char bgr[3] = {0};
    fseek(fp, 54, SEEK_SET);
    fread(bgr, 3, 1, fp);
    printf("The first pixel is %d %d %d\n", bgr[0], bgr[1], bgr[2]);

    fseek(fp, 54 + bmp_h * bmp_w * 3, SEEK_SET);
    fread(bgr, 3, 1, fp);
    printf("The last pixel is %d %d %d\n", bgr[0], bgr[1], bgr[2]);

    fclose(fp);

   FILE* fp2 = fopen("./rising_freedom.bmp", "r+");
    if (NULL == fp2) {
        perror("fopen");
        return 1;
    }

    unsigned char sgr[3] = {0, 255, 255};
    unsigned char egr[3] = {0, 0, 255};
    unsigned char gr[3] = {0, 0, 0};

     // Reset file position to start
    fseek(fp2, 54, SEEK_SET); // Move to pixel data start

    for (int i = 0; i < bmp_h; i++) {
        for (int j = 0; j < bmp_w; j++) {
            if(i<bmp_h/3)
            {
                fwrite(sgr, 3, 1, fp2);
            }else if(i>bmp_h/3&&i<bmp_h/3*2)
            {
                fwrite(egr, 3, 1, fp2);
            }
            else if(i>bmp_h/3*2)
            {
                fwrite(gr, 3, 1, fp2);
            }
        }
    }

    fclose(fp2);
    return 0;
}

int main(int argc, const char *argv[]) 
{
    FILE *fp = fopen("./rising_freedom11.bmp", "r+");
    if (NULL == fp) {
        perror("fopen");
        return 1;
    }
    unsigned char black[3] = {0, 0, 255};

    int bmp_size = 0, bmp_h = 0, bmp_w = 0, temp=54;
    fseek(fp, 2, SEEK_SET);
    fread(&bmp_size, 4, 1, fp);
    printf("The BMP size is %d\n", bmp_size);

    fseek(fp, 18, SEEK_SET);
    fread(&bmp_h, 4, 1, fp);
    bmp_h=bmp_h*2;
    fseek(fp, 18, SEEK_SET);
    fwrite(&bmp_h, 4, 1, fp);
    fseek(fp, 22, SEEK_SET);
    fread(&bmp_w, 4, 1, fp);
    bmp_w=bmp_w*2;
    fseek(fp, 22, SEEK_SET);
    fwrite(&bmp_w, 4, 1, fp);
    fseek(fp, temp, SEEK_SET);
    for(int i=0;i<=bmp_h*2;i++)
    {
        for(int j=0;j<=bmp_w*2;j++)
        {
            if(i<=bmp_h&&j<=bmp_w)
            {
                temp=temp+1;
                fseek(fp,temp,SEEK_SET);

            }
            else
            {
            fwrite(black, 3, 1, fp);
            }
        }
        
    }
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值