Return-Path: <tiff-bounces@remotesensing.org>
Received: from xserve.flids.com (adsl-065-083-169-202.sip.mco.bellsouth.net [65.83.169.202])
        by inmx009.isp.belgacom.be (8.12.11/8.12.11/Skynet-IN-2.32) with ESMTP id j4PDSUXC027621
        for <joris.at.lebbeke@skynet.be>; Wed, 25 May 2005 15:28:35 +0200
        (envelope-from <tiff-bounces@remotesensing.org>)
Received: from adsl-065-083-169-202.sip.mco.bellsouth.net (localhost [127.0.0.1])
	by xserve.flids.com (Postfix) with ESMTP
	id 2F43D259CFE4; Wed, 25 May 2005 09:27:59 -0400 (EDT)
X-Original-To: tiff@remotesensing.org
Delivered-To: tiff@remotesensing.org
Received: from mail.annetco.net (unknown [70.84.235.226])
	by xserve.flids.com (Postfix) with ESMTP id 1AEE1259CFC0
	for <tiff@remotesensing.org>; Wed, 25 May 2005 09:27:57 -0400 (EDT)
Received: (qmail 15834 invoked by uid 509); 25 May 2005 08:27:56 -0500
Received: from 210.245.109.186 by blue.annetco.net (envelope-from
	<anhtuantt@yahoo.com>, uid 508) with qmail-scanner-1.24-st-qms 
	(clamdscan: 0.81/695. spamassassin: 3.0.2. perlscan: 1.24-st-qms.  
	Clear:RC:0(210.245.109.186):SA:0(4.2/5.0):. 
	Processed in 5.138924 secs); 25 May 2005 13:27:56 -0000
X-Spam-Status: No, hits=4.2 required=5.0
X-Spam-Level: ++++
Received: from unknown (HELO tuannguyen) (anhtuantt@210.245.109.186)
	by mail.annetco.net with SMTP; 25 May 2005 08:27:50 -0500
Message-ID: <000701c5612d$8da12ce0$0302a8c0@tuannguyen>
From: "Nguyen Anh Tuan" <anhtuantt@yahoo.com>
To: "Joris" <joris.at.lebbeke@skynet.be>, <anhtuantt@annetco.com>,
        <tiff@remotesensing.org>
References: <20050525122047.19282.qmail@web31801.mail.mud.yahoo.com>
	<006001c56129$6915ffd0$9fabf651@dfgrrxsknr4e>
Subject: Re: [Tiff] Help me on convert from TIFF 24 bit or 8 bit grayscale
	tobilevel image
Date: Wed, 25 May 2005 20:27:48 +0700
MIME-Version: 1.0
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
X-Priority: 3
X-MSMail-Priority: Normal
X-Mailer: Microsoft Outlook Express 6.00.2800.1106
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
Cc: 
X-BeenThere: tiff@remotesensing.org
X-Mailman-Version: 2.1.2
Precedence: list
List-Id: TIFF and libtiff discussion  <tiff.remotesensing.org>
List-Unsubscribe: <http://xserve.flids.com/mailman/listinfo/tiff>,
	<mailto:tiff-request@remotesensing.org?subject=unsubscribe>
List-Archive: <http://xserve.flids.com/pipermail/tiff>
List-Post: <mailto:tiff@remotesensing.org>
List-Help: <mailto:tiff-request@remotesensing.org?subject=help>
List-Subscribe: <http://xserve.flids.com/mailman/listinfo/tiff>,
	<mailto:tiff-request@remotesensing.org?subject=subscribe>
Sender: tiff-bounces@remotesensing.org
Errors-To: tiff-bounces@remotesensing.org
X-UIDL: 000000000023ceae.0000026a
X-MSP-STORE: Wed, 25 May 2005 15:28:55 +0200
Status:   

Hello Joris,
Thanks for your reply quickly. You're alright, but right now i have a
problem on threshold algorithm. If you can, please help me.
Below,  this is the my threshold algorithm (convert 8 bit grayscale to one
bit image), the result image is very bad.

Best Regards,
Tuan
////////////////////////////////////////////////////////////////////////////
void COmrImage::LoadTIFFImage(TIFF *tif){
 this->fname = fname;
    unsigned short bitspersample, samplesperpixel;
    unsigned short config, photo_interp;
    int row;
    image_word *dest;
    int bpw; // bytes_per_word
    float xres, yres;
    TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &(this->size.cx));
    TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &(this->size.cy));
    TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &bitspersample);
    TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel);
    TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &config);
    TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photo_interp);

    TIFFGetField(tif, TIFFTAG_XRESOLUTION, &xres);
    TIFFGetField(tif, TIFFTAG_YRESOLUTION, &yres);
    if (xres != yres)
     AfxMessageBox("Horizontal and vertical resolution must be the same!");
    this->resolution = xres;
    bpw = sizeof(image_word);
    this->w_word = (TIFFScanlineSize(tif) + ((bpw / 2) + 1)) / bpw;
    this->offset = 0;
    this->origin.x = 0;
    this->origin.y = 0;
    this->data = (image_word *)malloc(this->w_word * this->size.cy *
sizeof(image_word));
    if ((config == PLANARCONFIG_SEPARATE) && (samplesperpixel >= 1))
          samplesperpixel -= 1;
   dest = this->data;
   for (row = 0; row < this->size.cy; row++)
   if (TIFFReadScanline(tif, (unsigned char*)dest, row,
        (unsigned int)samplesperpixel) == 1) {
     dest += this->w_word;
 }
 if(bitspersample == 8)    GreyToMonoImage();
// the result image is not correct
 this->SaveTIFFImage("threshold.tif");
}
void COmrImage::GreyToMonoImage()
{
    unsigned char *src, max = 0, min = 255, *gnew, mask = 0x80, threshold;
    int i, j;
    int bitfield = 0, byte = 0, extra = 0;
    int w_byte = this->w_word * sizeof(image_word);
    // Check if extra bits will be present and add an extra byte if so
    if ((w_byte % 8) != 0) extra = 1;
    gnew = (unsigned char *)calloc(this->GetSize().cy , ((w_byte / 8) +
extra));
    src = (unsigned char *)this->data;
    // Using max and min values, determine conversion threshold.
    for(i = 0; i < this->size.cy; i++)
    {
     for (j = 0; j < w_byte; j++)
  {
   if (src[j] > max)
   max = src[j];
   if (src[j] < min)
   min = src[j];
  }
  src += w_byte;
    }
   threshold = (unsigned char)((max+min)/2.2);
    src = (unsigned char *)this->data;
    for(i = 0; i < this->size.cy; i++)
    {
  for (j = 0; j < w_byte; j++)
  {
   if (src[j] >= threshold)
    gnew[byte] |= mask;
   if (bitfield >= 7)
   {
    byte++;
    bitfield = 0;
    mask = 0x80;
   }
   else
   {
    mask >>= 1;
    bitfield++;
   }
  }
  if (bitfield != 0)
  {
   byte++;
   bitfield = 0;
   mask = 0x80;
  }
  src += w_byte;
    }
    this->w_word = (this->w_word / 8) + extra;
   this->size.cx = this->w_word * BITS_PER_WORD ; //BITS_PER_WORD =32
    free(this->data);
    this->data = (image_word *)gnew;
}

----- Original Message -----
From: "Joris" <joris.at.lebbeke@skynet.be>
To: <anhtuantt@annetco.com>; <tiff@remotesensing.org>
Sent: Wednesday, May 25, 2005 7:58 PM
Subject: Re: [Tiff] Help me on convert from TIFF 24 bit or 8 bit grayscale
tobilevel image


> Tuan,
>
> > I'm a newer on this mailing list. Now I'm developping my image
processing
> program. I have tried converted a TIFF 24 bit RGB or  8 bit grayscale to
bilevel
> TIFF image, but i'm fail. Please help me deal with the issue.
>
> Strictly speaking, this is off-topic. It's not a TIFF issue, but a general
> imaging issue.
>
> You probably want to 'dither'. Dithering is the art of taking the error
after
> rounding one pixel, to the next, so that errors in neighbouring pixels
> compensate eachother, and thus the overall 'impression' the eye gets from
a
> block of neighbouring pixels is quite accurate despite huge individual
errors in
> individual pixels.
>
> Google on 'dither', there are a number of different techniques, each
having some
> advantages and some dissadvantages.
>
>
> Joris Van Damme
> info@awaresystems.be
> http://www.awaresystems.be/
> Download your free TIFF tag viewer for windows here:
> http://www.awaresystems.be/imaging/tiff/astifftagviewer.html
>
>

_______________________________________________
Tiff mailing list: Tiff@remotesensing.org
http://xserve.flids.com/mailman/listinfo/tiff
http://www.remotesensing.org/libtiff/
