Author Topic: BMP image garbled picture  (Read 2525 times)

0 Members and 1 Guest are viewing this topic.

Offline Keoni29

  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2466
  • Rating: +291/-16
    • View Profile
    • My electronics projects at 8times8
BMP image garbled picture
« on: October 17, 2014, 03:39:55 am »
I made a test program that generates variable size bmp images. When I generate images with widths under 11 or so the images have weird colors in them at the top.
Code: [Select]
/*
    Create 24 bit Bitmap image.
    Date: 16 oct 2014
    Version: 1
    Error: Weird colors when making images smaller than ~11 pixels wide.
*/


#include <stdio.h>

/* Write little-endian 32 bit value to file. */
void fputlong(int val, FILE *fp){
    fputc((char)(val & 0xFF), fp);
    fputc((char)((val >> 8 )& 0xFF), fp);
    fputc((char)((val >> 16) & 0xFF), fp);   
    fputc((char)(val >> 24), fp);
}

/*
    argv[1] = char *src;
    argv[2] = char *dst;
*/
int main (int argc, char *argv[]) {
    FILE *dst;
    int ch, padding, xx, yy, w, h;
    char red, green, blue;
    /* Verify user input */
    if (argc < 4) {
        printf("Error: ");
    }
    switch (argc) {
        case 0:
        case 1: printf("Missing filename");
                break;
        case 2: printf("Missing image width");
                break;
        case 3: printf("Missing image height");
                break;
        case 4: printf("Missing RED color value");
                break;
        case 5: printf("Missing GREEN color value");
                break;
        case 6: printf("Missing BLUE color value");
    }
    if (argc < 4) {
        printf("\nUsage: bmptest file.bmp width height r(0-255) g(0-255) b(0-255)\n");   
        return 0;
    }
w = atoi(argv[2]);
    h = atoi(argv[3]);
    red = (char)atoi(argv[4]);
    green = (char)atoi(argv[5]);
    blue = (char)atoi(argv[6]);

    dst = fopen(argv[1], "wb+");

    /* Write file header */
    fputc('B', dst);    // File identifier
    fputc('M', dst);    //
    padding = (4 - ((w * 3) % 4))% 4;
fputlong(w * h * 3 + 50 + (padding * h), dst); // Filesize
    fputlong(0x00, dst);        // Reserved 4 bytes.
    fputlong(0x036, dst);     // Offset
fputlong((int)40,dst);
    fputlong(w, dst);        // Width of bitmap
    fputlong(h, dst);        // Height of bitmap
    fputc(0x01, dst);        // Number of color planes
    fputc(0x00, dst);
    fputc(24, dst);            // Number of bits per pixel
    fputc(0x00, dst);
    fputlong(0x00, dst);    // Compression method: none
    fputlong(0x00, dst);    // Image size. Dummy 0
    fputlong(0x2700, dst);    // Horizontal resolution
    fputlong(0x2700, dst);    // Vertical resolution
    fputlong(0x00, dst);    // Number of colors in palette (Default 2^n)
    fputlong(0x00, dst);    // All colors are equally important
    for (yy = 0; yy < h; yy ++) {
        for (xx = 0; xx < w; xx ++) {
fputc(blue, dst);
fputc(green, dst);
fputc(red, dst);
        }
for (xx = 0; xx < padding; xx ++) {
fputc(0x00, dst);
}
    }
    printf("Padding = %d; Filesize = %d\n", padding , w * h * 3 + 50 + (padding * h));
    fclose(dst);
    return 0;
}
Is this a problem with my code or with the decoder?
My bad. Padding should be at the end of every row of pixels >.<
« Last Edit: October 17, 2014, 07:31:18 am by Keoni29 »
If you like my work: why not give me an internet?