[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

reading image pixel

Posted on 1999-11-03
8
Medium Priority
?
359 Views
Last Modified: 2010-04-15
hi experts,
i do not know whether this is the right topic or not.  
the problem is, i have 2 image files. it is the same image, but slightly different of their position. the background of this image is white, and the image colour is black. my task is to calculate the difference of that 2 images(in pixels). can i do this with c? can c language differentiate the colour difference? some people told me to use cortex, but i do not know where to get it. can this be done by using only c without other libraries?
 
0
Comment
Question by:kudak
  • 3
  • 2
  • 2
  • +1
8 Comments
 

Author Comment

by:kudak
ID: 2181935
Edited text of question.
0
 
LVL 10

Expert Comment

by:rbr
ID: 2182247
In general yes. With C you can do almost everything. In which format are the image stored?
0
 

Author Comment

by:kudak
ID: 2182367
i do not know which format. let'd say bitmap. do you know free online website for this kind of stuff? i've read 3 c books and could not find one. sample of programing is really appreciated.
thanks
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 10

Expert Comment

by:rbr
ID: 2182392
Check
http://www.wotsit.org/
list of many file formats.
0
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 2183677
Is a programm, that read BMP
file in memory   help you?
I have such programm in "C".
0
 
LVL 8

Expert Comment

by:Brain2000
ID: 2184485
If you have an MSDN CD, lookup the structures for BITMAPFILEHEADER and BITMAPHEADER.  That will get you started.  There are functions that can read in a BMP file into a Device Context, and you can manipulate it from there.
0
 

Author Comment

by:kudak
ID: 2184759
can u give me the sample of coding alexvirochovsky.
i know c, but the problem is i do not know how to start with this project
0
 
LVL 14

Accepted Solution

by:
AlexVirochovsky earned 300 total points
ID: 2185145
// this programm  tested for Borlandc 3.1
//for display Bitmap to screen
// i use BGI Graphic.
//
#include <alloc.h>
#include <conio.h>
#include <dos.h>
#include <dir.h>
#include <io.h>
#include <fcntl.h>
#include <graphics.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//h file
typedef unsigned char BYTE;
typedef unsigned int  WORD;
typedef long       DWORD;
typedef struct tagBITMAPFILEHEADER{
    WORD   bfType;
    DWORD  bfSize;
    WORD   bfReserv1;
    WORD   bfReserv2;
    DWORD  bfOffbits;
} BITMAPFILEHEADER;
typedef struct tagBITMAPINFOHEADER{
    DWORD   biSize;
    DWORD   biWidth;
    DWORD   biHeight;
    WORD    biPlanes;
    WORD    biBitCount;
    DWORD   biCompression;
    DWORD   biSizeImage;
    DWORD   biXPelPerMeter;
    DWORD   biYPelPerMeter;
    DWORD   biClrUsed;
    DWORD   biClrImpotant;
} BITMAPINFOHEADER;
typedef struct tagRGBQUAD
{
    BYTE    rgbBlue;
    BYTE    rgbGreen;
    BYTE    rgbRed;
    BYTE    rgbReserved;
} RGBQUAD;
typedef struct tagBITMAPINFO{
    BITMAPINFOHEADER  bmiHeader;
    RGBQUAD           bmiColors[1];
} BITMAPINFO;
#define GALLOC(P,S)   if ((P = farmalloc(S))==NULL) error(0)
#define VALLOC(P,N,E) if ((P = (E *)calloc(N,sizeof(E)))==NULL) error(0)

/* constants for the biCompression field */
#define BI_RGB      0L
#define BI_RLE8     1L
#define BI_RLE4     2L
//c file
static RGBQUAD *rgbTable = NULL;
static BYTE *szFile = NULL;
static int nColor;
static BITMAPFILEHEADER bmHeader;
static BITMAPINFO bmInfo;
       void main (int argc, char **argv);
       int  ReadBitMap(char *name, int *nX, int *nY, int nType, int nYMax);
static int  GetColor256(RGBQUAD *rgbTable);
static BYTE Normal(BYTE color);
       int  ReadBit(char *name);
       void FreeMap(void);
static void Error_Graph(void);
       void   error (int n);
/*=========================================================================*/
/*  Name       : main    */
/*  Descript   : test programme    */
/*  Parameters : name of bmp file    */
/*=========================================================================*/
void cdecl main (int argc, char **argv)
{
  int
    gdriver = VGA, gmode = VGAMED; //graphic initialization
  /* initialize graphics and local variables */
  initgraph(&gdriver, &gmode, "");
/* read result of initialization */
  Error_Graph();
  cleardevice();
  if (argc >= 2)
    {
      int nX,nY;
      ReadBitMap(argv[1],&nX,&nX,0,480);
      getch();
    }
  closegraph();     /* clean up */
}  // end main
/*=========================================================================*/
/*  Name       : Error_Graph       */
/*  Descript   : If there are errors ==> exit    */
/*  Parameters : None    */
/*  Return     : None    */
/*  Call       : main    */
/*=========================================================================*/
void Error_Graph(void)
{
  int errorcode = graphresult();

  if (errorcode != grOk)   // an error occurred
   {                                    // message of error
     printf("graph error: %s\n", grapherrormsg(errorcode));
     printf("press any symbol\n");
     getch();
     exit(1); // terminate with an error code
   }
} // end Error_Graph
/*=========================================================================*/
/*  Name       : ReadBitMap    */
/*  Descript   : Read BMP file (without Compression) and Display in 1-st   */
/*        :  Page of Graphik. Memory    */
/*  Parameters : name of file, # of column, # of lines    */
/*             : nType = 0: disply pixels with y < MAXY    */
/*        :       = 1 display pixels only > MAXY (in begin of screen) */
/*  Return     : -1: no such file, 0: OK    */
/*=========================================================================*/
int ReadBitMap(char *name, int *nX, int *nY, int nType, int nYMax)
{
   int x, y,color, n, ind, ind1, i, ret = -1;
   long lLen;

   if (ReadBit(name) == 0) //OK?
     {
       ind = 0;
       for (y = 0; y < bmInfo.bmiHeader.biHeight; y++)
{
   ind1 = 0;
   for (x = 0; x < bmInfo.bmiHeader.biWidth; x++)
      {
n = szFile[ind]; //type of BMP;
switch(nColor)
  {
     case 2:          //monochrom
  color = szFile[ind] & (1<< (7 - ind1) );
  if (color) color = 1;
  ind1++;
  if (ind1 >= 8)
    {
      ind1 = 0; ind++;
    }
  break;
     case 16:          //16 colors
  if (!ind1)
    {
      color = n/16; ind1 = 1;
    }
  else
    {
      color = n%16; ind++; ind1 = 0;
    }
  break;
     case 256: //256 colors
  color = n; ind++;
  break;
     default:  //error (can be 24 : without RGBcolors: not in use for Dos
  ind++;
  break;
  }
if (!nType && bmInfo.bmiHeader.biHeight - y < nYMax)
   putpixel(x, bmInfo.bmiHeader.biHeight - y,
    GetColor256(&rgbTable[color]) );
else if (nType && bmInfo.bmiHeader.biHeight - y >= nYMax)
   putpixel(x, bmInfo.bmiHeader.biHeight - y - nYMax,
    GetColor256(&rgbTable[color]) );
      }
   if (ind1 || ind%4) //next?
     {
       ind /= 4; ind = (ind+1)*4;
     }
}
       FreeMap(); //delete RGB table
       ret = 0;
       *nX = bmInfo.bmiHeader.biWidth;  //return values
       *nY = bmInfo.bmiHeader.biHeight;
     }
  return ret;
} // end ReadBitMap
/*=========================================================================*/
/*  Name       : GetColor256    */
/*  Descript   : Get colr from Predefined table by real color    */
/*  Parameters : Color of Pixel    */
/*  Return     : index of Borlandc color/0(Black): no such    */
/*=========================================================================*/
int GetColor256(RGBQUAD *rgbT)
{
  int
    color, i;
  static RGBQUAD rgbTest[16] =
    {
      {  0,   0,   0, 0},
      {127,   0,   0, 0},
      {  0, 127,   0, 0},
      {127, 127,   0, 0},
      {  0,   0, 127, 0},
      {127,   0, 127, 0},
      {  0, 127, 127, 0},
      {127, 127, 127, 0},
      { 63,  63,  63, 0},
      {255,   0,   0, 0},
      {  0, 255,   0, 0},
      {255, 255,   0, 0},
      {  0,   0, 255, 0},
      {255,   0, 255, 0},
      {  0, 255, 255, 0},
      {255, 255, 255, 0}
    };

  for (i = 0; i < 16; i++)
    if (rgbT->rgbBlue == rgbTest[i].rgbBlue  &&
rgbT->rgbGreen == rgbTest[i].rgbGreen && 
rgbT->rgbRed == rgbTest[i].rgbRed)
      return i;
  return 0;
}
/*=========================================================================*/
/*  Name       : Normal    */
/*  Descript   : From Windows color goto Dos color    */
/*  Parameters : Color(from 0 to 255)    */
/*  Return     : 0/127/255:  nearest    */
/*=========================================================================*/
BYTE Normal(BYTE color)
{
  if (color != 0 && color <= 63)
    color = 0;
  else if (color != 127 && color > 63 && color <= 192)
    color = 127;
  else if (color != 255 && color > 192)
    color = 255;

  return color;
}
/*=========================================================================*/
/*  Name       : ReadBit    */
/*  Descript   : Read BMP file (without Compression)    */
/*  Parameters : name of file    */
/*  Return     : -1: no such file, 0: OK    */
/*=========================================================================*/
int ReadBit(char *name)
{
   int fIn = open(name, O_RDONLY | O_BINARY);
   int n,i, ret = -1;
   long lLen;

   rgbTable = NULL;
   szFile = NULL;

   if (fIn >= 0)
     {
       read(fIn, &bmHeader, sizeof(bmHeader));    //read Header
       if (bmHeader.bfType == 19778)        //"BM"
{
   read(fIn, &bmInfo, sizeof(BITMAPINFOHEADER ));    //read Header
   switch (bmInfo.bmiHeader.biBitCount)       //find N of colors
      {
case  1:  nColor = 2;  break; //MONO
case  4:  nColor = 16; break; //16 colors
case  8:  nColor = 256;break; //256 colors
case 24:  nColor = 0;break; //2**24 colors
      }
   if (nColor)
     {
       VALLOC(rgbTable,nColor,RGBQUAD);
       read(fIn, rgbTable, nColor*sizeof(RGBQUAD)); //read Color Table

       for (n = 0; n < nColor; n++)
{
    rgbTable[n].rgbBlue = Normal(rgbTable[n].rgbBlue);
    rgbTable[n].rgbGreen = Normal(rgbTable[n].rgbGreen);
    rgbTable[n].rgbRed = Normal(rgbTable[n].rgbRed);
}
     }
   lseek(fIn, bmHeader.bfOffbits ,  SEEK_SET); //read file
   lLen = bmHeader.bfSize - bmHeader.bfOffbits;
   GALLOC(szFile,lLen); //alloc place for Bits
   n = lLen/30000l;
   if (lLen %30000l) n++;
   for (i = 0; i < n; i++) //read bits
      read(fIn, szFile+i*30000l,30000);
   ret = 0;
}
       close(fIn);
     }
  return ret;
} // end ReadBitMap
/*=========================================================================*/
/*  Name       : reeMap    */
/*  Descript   : Free color Table    */
/*=========================================================================*/
void FreeMap(void)
{
  if (rgbTable)
    {
      free(rgbTable); rgbTable = NULL;
    }

  if (szFile)
    {
      farfree(szFile); szFile = NULL;
    }
}
/*=========================================================================*/
/* Description: error - Exits program in case of fatal error.              */
/* Parameter  : nEInd   - index of code of error message to display.    */
/* szText  - added test of error message    */
/* Return val : None.    */
/*=========================================================================*/
void error (int n)
{
   fcloseall ();
   clrscr (); //clear screen
   exit (n); //exit from utile
} // end error

Best Regards, Alex
Ask, if you have some Q about programm.
0

Featured Post

The Firewall Audit Checklist

Preparing for a firewall audit today is almost impossible.
AlgoSec, together with some of the largest global organizations and auditors, has created a checklist to follow when preparing for your firewall audit. Simplify risk mitigation while staying compliant all of the time!

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

591 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