-2

I'm trying to implement reading the BMP file in c. I came across basic bmp reading module from google as the below.

#include <stdio.h>
#include <malloc.h>
#include <memory.h>
#include <io.h>
#include <fcntl.h>
#include "ReadBMP.h"


char ReadBMPFile(const char *file_name, BMPDATA *ret_bmp_data)
{
    int fh;
    long file_length;
    void *bmp_file_data;

    fh = _open(file_name, _O_RDONLY, _O_BINARY);
    if (fh == -1)
    {
        return -1;
    }

    _lseek(fh, 0, SEEK_END);
    file_length = _tell(fh);
    _lseek(fh, 0, SEEK_SET);

    bmp_file_data = malloc(file_length);
    _read(fh, bmp_file_data, file_length);

    _close(fh);

    ReadBMPData(bmp_file_data, ret_bmp_data);

    free(bmp_file_data);
    return 0;
}

But I feel hard to use above function in my code.

#include <windows.h>
#include "ReadBMP.h"
#define WIDTHBYTES(bits) (((bits)+31)/32*4)
#define BYTE    unsigned char

int main(int argc, char *argv[])
{
    FILE *fp;
    int i, j, k,n;
    int width = 16;
    int height = 16;
    int bpp = 24;

    FILE *fp_new;
    char *filename_new;
    char ch;
    BMPDATA *ret_bmp_data;

    int val[1000];
    char filename[100];
    //char *filename = "test.bmp"; 

    // Check if a filename has been specified in the command
    if (argc < 2)
    {
        printf("Missing Filename\n");
        return(1);
    }
    else
    {
        filename_new = argv[1];
        printf("Filename : %s\n", filename_new);
    }
    
    ReadBMPFile(filename_new, ret_bmp_data);

When I do implement like this I've got a error message as the below

error C4700: uninitialized local variable 'ret_bmp_data' used

How do I initialize the local variable?

update

I've updated ReadBMP.c file at all.

#include <stdio.h>
#include <malloc.h>
#include <memory.h>
#include <io.h>
#include <fcntl.h>
#include "ReadBMP.h"

void DecodeData(const char *byte_data, int offset, BMPDATA *ret_bmp_data);
void DecodeInfomation(const char *byte_data, int offset, BMPDATA *ret_bmp_data);

char ReadBMPFile(const char *file_name, BMPDATA *ret_bmp_data)
{
    int fh;
    long file_length;
    void *bmp_file_data;

    fh = _open(file_name, _O_RDONLY, _O_BINARY);
    if (fh == -1)
    {
        return -1;
    }

    _lseek(fh, 0, SEEK_END);
    file_length = _tell(fh);
    _lseek(fh, 0, SEEK_SET);

    bmp_file_data = malloc(file_length);
    _read(fh, bmp_file_data, file_length);

    _close(fh);

    ReadBMPData(bmp_file_data, ret_bmp_data);

    free(bmp_file_data);
    return 0;
}

char ReadBMPData(const void *bmp_file_data, BMPDATA *ret_bmp_data)
{
    char *byte_data;   
    int offset;   

    byte_data = (char *)bmp_file_data;

    if ((byte_data[0] != 'B') || (byte_data[1] != 'M'))   
    {
        return -1;
    }

    memcpy(&offset, &byte_data[10], 4);

    DecodeInfomation(byte_data, offset, ret_bmp_data);
    DecodeData(byte_data, offset, ret_bmp_data);

    return 0;
}

void DecodeInfomation(const char *byte_data, int offset, BMPDATA *ret_bmp_data)
{
    if (offset == 26)
    {
        memcpy(&ret_bmp_data->width, &byte_data[18], 2);
        memcpy(&ret_bmp_data->height, &byte_data[20], 2);
        memcpy(&ret_bmp_data->bits_per_pixel, &byte_data[24], 2);
    }
    else
    {
        memcpy(&ret_bmp_data->width, &byte_data[18], 4);
        memcpy(&ret_bmp_data->height, &byte_data[22], 4);
        memcpy(&ret_bmp_data->bits_per_pixel, &byte_data[28], 2);
    }
}

void DecodeData(const char *byte_data, int offset, BMPDATA *ret_bmp_data)
{
    int data_size;

    if (ret_bmp_data->bits_per_pixel == 24)
    {
        data_size = ret_bmp_data->width * ret_bmp_data->height * 3;
        ret_bmp_data->data = malloc(data_size);
        memcpy(ret_bmp_data->data, &byte_data[offset], data_size);

    }
}

this is ReadBMP.h

#ifndef READBMP_H
#define READBMP_H

typedef struct BMPDATA
{
    short bits_per_pixel;
    int width;
    int height;
    unsigned char *data;
}BMPDATA;

#ifdef __cplusplus
extern "C"
{
#endif

char ReadBMPFile(const char *file_name, BMPDATA *ret_bmp_data);
char ReadBMPData(const void *bmp_file_data, BMPDATA *ret_bmp_data);

#ifdef __cplusplus
}
#endif


#endif

and

this is Source.cpp

#include <stdio.h>
#include <stdlib.h>     /* system, NULL, EXIT_FAILURE */
#include <windows.h>
#include "ReadBMP.h"
#define WIDTHBYTES(bits) (((bits)+31)/32*4)
#define BYTE    unsigned char

int main(int argc, char *argv[])
{
    FILE *fp;
    int i, j, k,n;
    int width = 16;
    int height = 16;
    int bpp = 24;

    ////////////////// add BMP read 2018-03-20 ///////////////
    FILE *fp_new;
    char *filename_new;
    char ch;
    BMPDATA *ret_bmp_data;

    int val[1000];
    char filename[100];
    //char *filename = "test.bmp"; 

    // Check if a filename has been specified in the command
    if (argc < 2)
    {
        printf("Missing Filename\n");
        return(1);
    }
    else
    {
        filename_new = argv[1];
        printf("Filename : %s\n", filename_new);
    }

    
    ReadBMPFile(filename_new, ret_bmp_data);
fclose(fp);
fclose(fp_new);
}
Community
  • 1
  • 1
start01
  • 121
  • 4
  • 13
  • 2
    By assigning a value to it before you using it? – Jabberwocky Mar 20 '18 at 13:21
  • @MichaelWalz By assigning a null pointer (`NULL`) to it. – flashingx Mar 20 '18 at 13:22
  • 1
    It's highly unlikely the function expects an uninitialized pointer as a parameter. What is it supposed to point to? Maybe if we saw `ReadBMPData()` we could tell. – 001 Mar 20 '18 at 13:23
  • 1
    @flashingx, I seriously doubt that `ReadBMPData(bmp_file_data, ret_bmp_data)` accepts a NULL pointer – Jabberwocky Mar 20 '18 at 13:24
  • You should probably read the chapter dealing with pointers in your C text book. And, yes, you forgot to show us the `ReadBMPData` function. – Jabberwocky Mar 20 '18 at 13:24
  • I'm really studying the c slowly, so would you please let me know little a bit more? what am i supposed to do to resolve this problem? – start01 Mar 20 '18 at 13:29
  • @start01 _you_ need to let us know more. Where is the `ReadBMPData`? Without that function it's hard help. – Jabberwocky Mar 20 '18 at 13:31
  • @MichaelWalz I don't know the function `_open` but as the function `ReadBMPData` takes a file pointer and said pointer I assumed said function points to some memory containing the requested data. Since this is the internet I'm not judging you for not trying to understand my reasoning. – flashingx Mar 20 '18 at 13:31
  • Looks like you should be passing it by reference not value – Chris Turner Mar 20 '18 at 13:38
  • @4386427 Thanks, it's working. – start01 Mar 20 '18 at 13:40
  • 1
    @ChrisTurner there is no "passing by reference" in C. – Jabberwocky Mar 20 '18 at 13:49
  • @MichaelWalz so what is it called where you pass a pointer to a variable to enable the called function to change the value of the variable and pass it back to the calling code? – Chris Turner Mar 20 '18 at 14:28
  • @ChrisTurner read [this](https://stackoverflow.com/questions/2229498/passing-by-reference-in-c) SO article, especially the second answer. – Jabberwocky Mar 20 '18 at 14:30

1 Answers1

2

Due to the error message, it seems that the BMPDATA variable in main isn't supposed to be a BMPDATA pointer but a BMPDATA object.

Like:

int main(int argc, char *argv[])
{
    ....

    BMPDATA ret_bmp_data;

    ....

    ReadBMPFile(filename_new, &ret_bmp_data);

    ....
}
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63