NO

Author Topic: Load JPEG File WinAPI Using libJPEG-Turbo  (Read 3262 times)

arielvw

  • Guest
Load JPEG File WinAPI Using libJPEG-Turbo
« on: November 18, 2015, 04:02:34 PM »
how to load a JPEG file using LibJPEG,, this is my code

Procedure Open Dialog
Code: [Select]
void OpenDialog(HWND hWnd)
{
    OPENFILENAME    ofn     = {sizeof(ofn)};

    ofn.hwndOwner       = hWnd;
    ofn.lpstrFilter     = TEXT("File BMP (*.BMP)\0*.BMP\0File JPG (*.JPG)\0*.JPG\0");
    ofn.nFilterIndex    = 1;
    ofn.lpstrFile       = szNamaFile;
    ofn.lpstrFile[0]    = '\0';
    ofn.lpstrTitle      = TEXT("Test ...");
    ofn.nMaxFile        = sizeof(szNamaFile);
    ofn.Flags           = OFN_FILEMUSTEXIST;

  if(GetOpenFileName(&ofn))
  {
      read_jpeg_file(ofn.lpstrFile);
  }
}

Procedure Read JPEG
Code: [Select]
void read_jpeg_file(char *szNamaFile)
{
    size_t i;
    unsigned char* raw_image;
    JSAMPROW row_pointer[1];
    unsigned long location = 0;

    struct jpeg_error_mgr jerr;
    struct jpeg_decompress_struct cinfo ;

    errno_t err;
    FILE *infile;
    err = fopen_s(&infile, szNamaFile, "rb" );

    if (infile == NULL )
    {
        printf("Error opening jpeg file %s\n!", szNamaFile );
        exit(EXIT_FAILURE);
    }
    cinfo.err = jpeg_std_error(&jerr);

    /* create decompressor */
    jpeg_create_decompress(&cinfo);

    /* this makes the library read from infile */
    jpeg_stdio_src(&cinfo, infile );

    /* read jpeg header */
    jpeg_read_header(&cinfo, TRUE);

    /* decompress */
    jpeg_start_decompress(&cinfo);

    /*allocate memory */
    raw_image = (unsigned char*)malloc( cinfo.output_width*cinfo.output_height*cinfo.num_components );

    /* now actually read the jpeg into the raw buffer */
    row_pointer[0] = (unsigned char *)malloc( cinfo.output_width*cinfo.num_components );
    /* read scanlines */
    while (cinfo.output_scanline < cinfo.output_height) {
        jpeg_read_scanlines( &cinfo, row_pointer, 1 );
        for(i = 0; i < cinfo.image_width*cinfo.num_components; i++)
            raw_image[location++] = row_pointer[0][i];
    } 

    /* clean up */
    jpeg_finish_decompress(&cinfo);
    jpeg_destroy_decompress(&cinfo);
    fclose( infile );
    free( row_pointer[0] );
}

WM_COMMAND
Code: [Select]
case WM_COMMAND:
        if(HIWORD(wParam) != BN_CLICKED)
            break;
        switch(LOWORD(wParam))
        {
            case IDB_LOAD:
                OpenDialog(hWnd);
                break;
        }
        break;
Image doesn't show,, how to fix it?

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Load JPEG File WinAPI Using libJPEG-Turbo
« Reply #1 on: November 19, 2015, 12:44:20 PM »
Look here
« Last Edit: November 20, 2015, 12:09:07 PM by TimoVJL »
May the source be with you