0

EDIT: I will improve this question. I will clarify it right in a little days.

first, I am writing a litlle bmp image analyzer. I have the following problem: The image is stored on plain bytes, without format as an array.

The image is 24 bits, and requires 3 bytes per pixel. I have tried with a solution that I have found on this stackoverflow page, but I can not adapt it for structures.

I have tried but it references invalid areas and bytes. Here's my complete code if you want to see it in TinyPaste (just for a better highlighting): The code in TinyPaste

EDIT 1: This code is in C++, I want to translate it to pure C for portability reasons. This is just the example from I taken the idea of convert a linear array to bidimensional. I have tried to adapt it to pure C for structs but I fail.

This snippet was taken from a stackoverflow question that made me think about this

//The resulting array
unsigned int** array2d;

// Linear memory allocation
unsigned int* temp = new unsigned int[sizeX * sizeY];

// These are the important steps:
// Allocate the pointers inside the array,
// which will be used to index the linear memory
array2d = new unsigned int*[sizeY];

// Let the pointers inside the array point to the correct memory addresses
for (int i = 0; i < sizeY; ++i)
{
    array2d[i] = (temp + i * sizeX);
}



// Fill the array with ascending numbers
for (int y = 0; y < sizeY; ++y)
{
    for (int x = 0; x < sizeX; ++x)
    {
        array2d[y][x] = x + y * sizeX;
    }
}

I adapt it to reference structs, but it fails. I have tried multiplying by three in this line:

 array2d[i] = (temp + i * sizeX /* multiply by 3*/); 

But it still without work. I have also done the related castings from char to the struct bmp_pixel(char r, char g, char b).

Can somebody tell me how to adapt it to pure C for structs?? Thanks.

Community
  • 1
  • 1
  • "*does not work*" is the error description with the least amount of helpful information possible. – alk May 07 '15 at 15:36
  • Sorry. I am trying to say that in one byte that works, but with structs it points to strange positions of memory or data content. I am looking for mor e info. – stevestickson1010 May 07 '15 at 15:45
  • The code contains the 'new' reserved word from C++ and #includes iostream. There fore it is C++ code, not C code. Suggest updating the tags to indicate C++ not C – user3629249 May 07 '15 at 16:10
  • a row of pixels, in a .BMP file will be rounded up to an even multiple of 4 bytes. The code doesn't seem to be paying attention to the image pixel width nor allowing for the round up of line widths. – user3629249 May 07 '15 at 16:12
  • a .bmp file needs to be defined as array of characters, then extract fields as needed. Because some fields are not aligned on appropriate boundaries. So the struct bmp_format is not correct. you might want to look here: and pay close attention to the details – user3629249 May 07 '15 at 16:15
  • when opening the files, for input, do not use "r+b" unless you plan on writing an that file also. for output do not use "w+b" unless you plan on writing non printable characters AND plan on reading that file back. Suggest: for input using "rb" and for output using "w" – user3629249 May 07 '15 at 16:21
  • what is this line: cout << "Hello world!" << endl; on the end of the code supposed to accomplish? it has nothing to do with the rest of the code. – user3629249 May 07 '15 at 16:23
  • regarding these two line: ' bmp_pixel bmp_pixel; bmp_format bmp_format;' trying to define variables with the same name as a type (in this case a struct type) is a very bad idea. use unique names. perhaps: struct bmp_pixel pixel; and struct bmp_format format; also, the posted code does not compile, with errors about incomplete type definitions – user3629249 May 07 '15 at 16:27
  • this line: 'printf("reading from %d \n", ftell(bmp_file));' will give 0, as the file pointer has already been reset to the beginning of the file via the prior line: 'fseek (bmp_file, 0, SEEK_SET);' also, fseek and ftell should have their returned value checked to assure the operation was successful – user3629249 May 07 '15 at 16:31
  • the returned value from fopen() needs to be checked to assure the operation was successful. the returned value from fread() should not be cast. the returned value from fread() needs to be checked to assure the operation was successful – user3629249 May 07 '15 at 16:34
  • why are you using calloc() and printf() in C++ source code? suggest using 'new' and 'cout' – user3629249 May 07 '15 at 16:35
  • regarding this line: ' printf("%s \n", buffer);' buffer[] contains almost all non printable characters. so printing it, to a terminal, is a very bad idea as an control characters (and there will be plenty) will mess up the terminal display. Similar considerations need to be applied to the printing of an instance of the bmp_format struct – user3629249 May 07 '15 at 16:38
  • this comment: '/// array_size = width * height.' is not true (see above comment about row width rounding up.) – user3629249 May 07 '15 at 16:42
  • this line: 'bitmap_2d = (struct bmp_pixel **) malloc(bmp_format.height * sizeof(bmp_pixel));' only allocates enough room for 1 pixel for each row in the image. – user3629249 May 07 '15 at 16:46
  • @user3629249 I am avoiding verifications to keep the code more smaller to read. I will improve my question to clarify what I want. The idea is just adapt C++ code to C. I have done translations from new to malloc(items * size) but it doesn't work. – stevestickson1010 May 08 '15 at 04:49
  • Again this "it doesn't work" stuff. Be precise, both with what you did, what happened and what you expected to happen. – Ulrich Eckhardt May 08 '15 at 05:46

0 Answers0