0

I have problem with my code. I got bmp image and encoded it in base64.Then i want it to decode , but my PC say: "Could not load image 'OUTPUT.PNG'".How can i fix it?

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>


const unsigned char base64[] = {
    // ASCII table 
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 64, 63,
    52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
    64,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
    15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
    64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
    41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
    64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64  
};

void deblock(const unsigned char *in, const unsigned char *base64, FILE * OutputFile, int len)
{
 if (base64[(unsigned char)in[0]] < 64  && base64[(unsigned char)in[1]] < 64)   
    fprintf(OutputFile, "%c",(base64[(unsigned char)in[0]] << 2 | base64[(unsigned char)in[1]] >> 4));
 if (base64[(unsigned char)in[1]] < 64  && base64[(unsigned char)in[2]] < 64)       
    fprintf(OutputFile, "%c",(base64[(unsigned char)in[1]] << 4 | base64[(unsigned char)in[2]] >> 2));
 if (base64[(unsigned char)in[2]] < 64  && base64[(unsigned char)in[3]] < 64)   
    fprintf(OutputFile, "%c", base64[(unsigned char)in[2]] << 6 | base64[(unsigned char)in[3]]);
}   

void decode (FILE *InputFile, FILE *OutputFile)
{

    int len = 0;
    unsigned char buffer[4] = "\0"; 

    while ((len = fread(buffer,sizeof(unsigned char),4,InputFile)) > 0)
    {   
        deblock(buffer,base64,OutputFile,len);
    }   
}


int check_decode(FILE *file1, FILE *file2)
{
    int k = 0;

    fseek (file1, 0, SEEK_END);
    if (0 == ((ftell(file1)) % 4))
    {
        fseek(file1,0,SEEK_SET);
        decode(file1,file2);
    }
    else 
    {
        k = -1;
    }
    return k;
} 


int main() 
{
    FILE *file1 = NULL;
    FILE *file2 = NULL;
    if (NULL == (file1 = fopen("INPUT1.txt", "rb")))
    {   
        printf("Can't open INPUT file!");
        return -1;
    }
    if (NULL == (file2 = fopen("OUTPUT.png", "wb    ")))
    {
        printf("Can't create OUTPUT file");
        fclose(file1);
        return -1;
    }   
    if ( -1 == check_decode(file1, file2)       )
    {
        printf("Eto ne base64\n ");
        return -1;
    }
    assert(file1);
    assert(file2);
    fclose(file1);
    fclose(file2);
    return 0;
}

I use this site to encode my image http://www.base64-image.de/step-2.php

  • All those `(unsigned char)` casts are useless. Elements of `in` are already have that type. – user694733 Dec 10 '14 at 11:06
  • http://stackoverflow.com/q/26175293/992406 also http://base64.sourceforge.net/b64.c – houssam Dec 10 '14 at 11:32
  • You *encode* a BMP image with Base64 and expect it to *decode* as a PNG image? Perhaps your PC only said it cannot "load image 'OUTPUT.PNG'" because it is not a PNG at all. – Jongware Dec 10 '14 at 14:00

1 Answers1

0

I got bmp image and encoded it in base64.Then i want it to decode , but my PC say: "Could not load image 'OUTPUT.PNG'".How can i fix it?

Since you got bmp image, just create OUTPUT.bmp rather than OUTPUT.png.

Armali
  • 18,255
  • 14
  • 57
  • 171