The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Page 2 -
Need help resizing a bitmap file
Page 2 - Discuss Need help resizing a bitmap file in the C Programming forum on Dev Shed. Need help resizing a bitmap file C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 29th, 2013, 10:18 PM
|
 |
Contributed User
|
|
|
|
|
> Ok, but for sure, in your humble opinion, strcat is the way to go to append the pixels to the buffer?
No NO NO NO NO!!!!!!!!
You're taking it all way too literally.
strcat is USELESS for appending pixels to the buffer.
I'm trying to get you to understand that the way strcat works is the same way that your 'append pixels to the buffer' would work.
|

January 29th, 2013, 11:37 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 15
Time spent in forums: 4 h 26 m 49 sec
Reputation Power: 0
|
|
|
Ok, so the loop to write out n number of times would be in my loop to write out the pixels:
for int i=0;i<factor;i++)
{
fwrite(buffer, sizeof(RGBTRIPLE),factor, outptr)
}
Cool. Thank you both for that. But the loop to factor the pixels out? I dont follow on that. What is wrong with what I had(besides not working like how it should of course):
buffer[k]=triple;?
|

January 30th, 2013, 12:47 AM
|
 |
Contributed User
|
|
|
|
|
> buffer[k]=triple;?
Because k goes 0 1 2 0 1 2 0 1 2 for each pixel you read.
buffer index needs to go 0 1 2 3 4 5 6 7 8 .... if you want three copies of first pixel in 0 1 2, three copies of the second pixel in 3 4 5 and three copies of the third pixel in 6 7 8
|

January 30th, 2013, 12:52 AM
|
|
|
Some of the issues are documented in the code:
Code:
// Issue.You're using OldHeight and OldWidth in your outer and inner loops
// You should be using your newly calculated width and height for your
// new bitmap image
// iterate over infile's scanlines
for (int i = 0; i < abs(OldHeight); i++) {
// iterate over pixels in scanline
for (int j = 0; j < Oldwidth; j++) {
// temporary storage
RGBTRIPLE triple;
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
// Issue. Here you continually update the same area in buffer
// buffer[0] to buffer[factor-1]
// You must "expand" the pixels here to completely fill in the buffer file
// This means you must use a temporary index variable to iterate thru the
// buffer variable in order to fill it with the expanded RGB pixels
//iterate over each pixel factor times
for (int k = 0; k < factor; k++) {
buffer[k] = triple;
}
// Issue. Here you are ONLY writing THREE bytes in triple to your output file,
// You should be writing the completely filled in buffer variable.
// In other words, the fwrite is completely wrong
// write RGB triple to outfile
// fwrite(&triple, sizeof(RGBTRIPLE), bi.biWidth, outptr);
}
// Issue. The following is totally meaningless. You should skipping over the padding at the end
// of the old file row. padding, if correctly computed will range between zero and three.
// Why are you adding 54?
// skip over padding, if any
fseek(inptr, 54 + (Oldwidth * sizeof(RGBTRIPLE) * i), SEEK_SET);
// Ok. You got this one almost right. You should be using the width of the new file
for (int r = 0; r < factor; r++) {
fwrite(buffer, sizeof(RGBTRIPLE), bi.biWidth, outptr);
}
// Ok. the following is correct
// write padding to outfile
for (int p = 0; p < padding; p++)
fputc(0x00, outptr);
}
You're putting the cart before the horse on this assignment. You should create the header for the new BMP file first by cloning the old header file and then updating the required fields such as biWidth, biHeight, biSizeImage and bfSize. The new BMP file will NOT display properly in a Paint program if these fields are not updated to reflect the properties of the new file.
Once you have correctly built the new header for the BMP file, you can then concentrate on adding the pixel data to the file.
To reiterate, build the BMP header file first and then add the data to the file.
Finally, you don't come close to building a valid header for your new BMP file in the code that you have provided so far.
|

January 31st, 2013, 01:55 AM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 15
Time spent in forums: 4 h 26 m 49 sec
Reputation Power: 0
|
|
|
Ok, so after getting some time to review over what I need to do, I realized that I need the memcpy function to copy the pixels into the buffer(the destination address). That works like a charm. However, to continously append to the destination address, everything I'm doing doesnt seem to work. For example:
in my code, I have buffer incrementing by one to move to the next memory location:
buffer=buffer+1;
The program compiles for sure, but it crashes on me everytime. would I need to, for example in my memcpy
implement something of this nature:
memcpy(destination address+num bytes,source address, num)?
Or is my question a question of preference?
|

January 31st, 2013, 02:55 AM
|
 |
Contributed User
|
|
|
|
I dunno - I did this, and it seemed to work (for 1 test)
Code:
// iterate over infile's scanlines
for (int i = 0, BHeight = abs(OldHeight); i < BHeight; i++) {
int bp = 0;
// iterate over pixels in scanline
for (int j = 0; j < Oldwidth; j++) {
// temporary storage
RGBTRIPLE triple = { 0xde, 0xad, 0xca };
// read RGB triple from infile
fread(&triple, sizeof(RGBTRIPLE), 1, inptr);
//iterate over each pixel factor times
for (int k = 0; k < factor; k++) {
buffer[bp++] = triple;
}
}
// write RGB triple to outfile
// fwrite(&triple, sizeof(RGBTRIPLE), bi.biWidth, outptr);
for (int k = 0; k < factor; k++) {
fwrite(buffer, 1, sizeof(RGBTRIPLE) * bi.biWidth, outptr);
for (int p = 0; p < padding; p++)
fputc(0x00, outptr);
}
// skip over padding, if any
fseek(inptr, oldpadding, SEEK_CUR);
}
Like I said, you're over thinking the buffer appending.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|