Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb I want to make Blue of RGB 0.

    I'm a beginner just start to study C language.
    I want to make Blue of RGB of an bmp image. Then export the image Blue-0.
    You know it doesn't work this way.
    I don't know how I debug it.

    Help me T_T


    ######this is my source code######

    ‎#include <stdio.h>
    #include <malloc.h>

    int main()
    {
    char firstCharacter;
    char secondCharacter;
    int filesize;
    int reserverd;
    int data_offset;
    int size;
    int width;
    int planes;
    int bit_count;
    int compression;
    int image_size;
    int x_pixels_per_m;
    int y_pixels_per_m;
    int colors_used;
    int colors_important;

    FILE * fp = fopen("D:\\flower.bmp","rb");

    fread (&firstCharacter,sizeof(char),1,fp);
    fread (&secondCharacter,sizeof(char),1,fp);
    fread (&filesize,sizeof(int),1,fp);
    fread (&reserverd,sizeof(int),1,fp);
    fread (&data_offset,sizeof(int),1,fp);
    fread (&size,sizeof(int),1,fp);
    fread (&width,sizeof(int),1,fp);
    fread (&planes,sizeof(int),1,fp);
    fread (&bit_count,sizeof(int),1,fp);
    fread (&compression,sizeof(int),1,fp);
    fread (&image_size,sizeof(int),1,fp);
    fread (&x_pixels_per_m,sizeof(int),1,fp);
    fread (&y_pixels_per_m,sizeof(int),1,fp);
    fread (&colors_used,sizeof(int),1,fp);
    fread (&colors_important,sizeof(int),1,fp);

    printf ("first character is %c \n",firstCharacter);
    printf ("second character is %c \n",secondCharacter);
    printf ("filesize is %d \n",filesize);
    printf ("reserverd is %d \n",reserverd);
    printf ("data_offset is %d \n",data_offset);
    printf ("size is %d \n",size);
    printf ("width is %d \n",width);
    printf ("planes is %d \n",planes);
    printf ("bit_count is %d \n",bit_count);
    printf ("compression is %d \n",compression);
    printf ("image_size is %d \n",image_size);
    printf ("x_pixels_per_m is %d \n",x_pixels_per_m);
    printf ("y_pixels_per_m is %d \n",y_pixels_per_m);
    printf ("colors_used is %d \n",colors_used);
    printf ("colors_important is %d \n",colors_important);

    fclose(fp);

    char * image_data_size = (char *)malloc ( image_size);
    fread (image_data_size, image_size,1,fp);

    unsigned int padding;
    padding = 8-((width*3)%8);
    for (int i = 0; i < width ; i++)
    {
    for (int j = 0;j <size ; j++)
    {
    image_data_size [i*(width*3+padding) + j*3] = 0;// B color of first pixel

    }
    }
    FILE * wp;
    wp = fopen ("D:\\newfile.bmp","wb+");
    fwrite (&firstCharacter, sizeof (char),1,wp);
    fwrite (&secondCharacter,sizeof(char),1,wp);
    fwrite (&filesize,sizeof(int),1,wp);
    fwrite (&reserverd,sizeof(int),1,wp);
    fwrite (&data_offset,sizeof(int),1,wp);
    fwrite (&size,sizeof(int),1,wp);
    fwrite (&width,sizeof(int),1,wp);
    fwrite (&planes,sizeof(int),1,wp);
    fwrite (&bit_count,sizeof(int),1,wp);
    fwrite (&compression,sizeof(int),1,wp);
    fwrite (image_data_size,image_size,1,wp);
    fwrite (&x_pixels_per_m,sizeof(int),1,wp);
    fwrite (&y_pixels_per_m,sizeof(int),1,wp);
    fwrite (&colors_used,sizeof(int),1,wp);
    fwrite (&colors_important,sizeof(int),1,wp);
    ///,write header information as same as you read
    // now write the image pixel data which is changed one

    fclose (wp);
    free (image_data_size);

    return 0;
    }
    Last edited by noeloasis; 02-08-2012 at 08:58 AM.

  • #2
    New Coder
    Join Date
    Aug 2007
    Posts
    59
    Thanks
    3
    Thanked 1 Time in 1 Post
    Code:
    #include <iostream>
    using namespace std;
    This runs in prompt conveniently, so just adding i/ostream temporarily can be used for debugging if you do not have the software that includes a debugger. by adding
    Code:
    cout << "1 ";
    every few lines with a differeant number or character to identify the position the code is at before it crashes,

    This code crashes at this point
    Code:
    image_data_size [i*(width*3+padding) + j*3] = 0;// B color of first pixel
    With no error, But DevC++ debugger gives the error

    "An access violation (segmentation Fault) raised in your program."


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •