Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

biWidth wrong in tagBITMAPINFOHEADER when using twain

Posted on 2008-06-18
6
Medium Priority
?
535 Views
Last Modified: 2013-11-20
I use the twain driver to scan an image. Then I read the raw image data i get a wrong width for the following dpi-resolutions:
96 dpi -> the width is to low by 3
100 dpi -> the width is to low by 2

I have attached an image to show this.

On higher Resolutions (150, 300, 360dpi) it always works.

What did I wrong? Is there a differnet way to get the hight and width of an hDIB?

The strange thing is it that these resolutions work perfect in other programms (e.g. IrfanView)

Please Help

Greedings Sonja
bool CScanDialog::PDFAddPicturePage(HDIB hDIB)
{
  HPDF_Page  page;
  HPDF_Image image;
 
  BYTE * BGRimage;
  BGRimage = (BYTE *) GlobalLock(hDIB);
 
  /*
    typedef struct tagBITMAPINFOHEADER{
      DWORD  biSize; 
      LONG   biWidth; 
      LONG   biHeight; 
      WORD   biPlanes; 
      WORD   biBitCount; 
      DWORD  biCompression; 
      DWORD  biSizeImage; 
      LONG   biXPelsPerMeter; 
      LONG   biYPelsPerMeter; 
      DWORD  biClrUsed; 
      DWORD  biClrImportant; 
    } BITMAPINFOHEADER, *PBITMAPINFOHEADER; 
  */
 
  long img_width    = * (BGRimage + 1 * sizeof(DWORD) + 3); img_width *= 0x100;
       img_width   += * (BGRimage + 1 * sizeof(DWORD) + 2); img_width *= 0x100;
       img_width   += * (BGRimage + 1 * sizeof(DWORD) + 1); img_width *= 0x100;
       img_width   += * (BGRimage + 1 * sizeof(DWORD)    );
  long img_height   = * (BGRimage + 1 * sizeof(DWORD) + 1 * sizeof(long) +3);img_height *= 0x100;
       img_height  += * (BGRimage + 1 * sizeof(DWORD) + 1 * sizeof(long) +2);img_height *= 0x100;
       img_height  += * (BGRimage + 1 * sizeof(DWORD) + 1 * sizeof(long) +1);img_height *= 0x100;
       img_height  += * (BGRimage + 1 * sizeof(DWORD) + 1 * sizeof(long) +0);
  DWORD img_header_length  = * (BGRimage +1); img_header_length *= 0x100;
        img_header_length += * (BGRimage);
 
  BGRimage += img_header_length; // Offset of the header
  BGRimage += (0xFF * 4);        // Offset of the color table
  /*Here some  code that writes the image to a pdf file*/
}

Open in new window

Scan.JPG
0
Comment
Question by:Sonja_M
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 21814498
In the above code, there's no reference to any DPI values nor are you setting 'biWidth'. Could you show the complete code and explain the calculations in the above snippet?
0
 

Author Comment

by:Sonja_M
ID: 21814894
Hi jkr!

To do a scan you simply have to set some parameters for the scanner and then start the scan. The DPI-Value is stored in

#define SCANNER_DPI 96

The code above is run after the scan. I get a handle to a hDIB-Image and try to use this data. The follwing is not possible, because the compile can not convert void to tagBITMAPINFOHEADER:

tagBITMAPINFOHEADER * MyDibHead = BGRimage;

So what I did is to read the raw data in line 25-28 from biWidth. Later on I do not need the dpi anymore. I just need the pixels, but I only ant to use around 100dpi then scanning.

I hope that got clearer.

Greedings

Sonja
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 796 total points
ID: 21815491
This is well possible, just use a cast, i.e.
// cast 1
BITMAPINFOHEADER * MyDibHead = (BITMAPINFOHEADER *) BGRimage;
 
// cast 2
BITMAPINFOHEADER * MyDibHead = static_cast<BITMAPINFOHEADER *>(BGRimage);

Open in new window

0
Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

 
LVL 19

Accepted Solution

by:
alb66 earned 1200 total points
ID: 21819747
Hi Sonja,
rimember that a line of a bitmap is always paddeed  to a DWORD boundary.

Example:
If MyDibHead.biWidth == 22 the line lenght is 24
0
 

Author Closing Comment

by:Sonja_M
ID: 31470175
Thank you very much, you both helped me a lot.

@jkr: First I tryed to cast the header - but i alway got a "can not cast"-error. Solution 1 worked (i thought I tryed this already).

@alb66: Where did you got this from? It was the solution - of course - now it works perfect. I have a white extra line now sometimes but that does not bother me. I was more afraid that a differend scanner sends me the picture with one pixel more.

Here is my final code (got much shorter):


bool CScanDialog::PDFAddPicturePage(HDIB hDIB)
{
    BYTE * BGRimage;
    BGRimage = (BYTE *) GlobalLock(hDIB);

    BITMAPINFOHEADER * MyDibHead = (BITMAPINFOHEADER *) BGRimage;
 
    long img_height = MyDibHead->biHeight;
    long img_width  = (((MyDibHead->biWidth + 3) / 4) * 4);

    BGRimage += MyDibHead->biSize;
    /*Here some  code that writes the image to a pdf file*/
}
0
 
LVL 19

Expert Comment

by:alb66
ID: 21863479
For example, you can find that information in the BITMAPINFO documentation:
http://msdn.microsoft.com/en-us/library/ms532284(VS.85).aspx

<<<<< A DIB consists of two distinct parts: a BITMAPINFO structure describing the dimensions and colors of the bitmap, and an array of bytes defining the pixels of the bitmap. The bits in the array are packed together, but each scan line must be padded with zeroes to end on a LONG data-type boundary. >>>>>
0

Featured Post

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I. Introduction In a previous article (http://www.experts-exchange.com/Web_Development/Document_Imaging/A_6537-PaperPort-Upgrade-How-to-download-and-install-updated-versions-of-PaperPort-11-and-12.html) (now deprecated), I discussed how to upgrad…
This article shows how to convert a multi-page PDF file into multiple image files, with one image file created for each page of the PDF. It does this by utilizing an excellent, free software package called GraphicsMagick. The solution is amazingly s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question