|
/*Name: Coop, the code below reads and displays some pgm, ppm files, i want to extent it to read bmp files and to also show the actual values of the pixels in the image, please assist*/ //---------------------------------------------------------------------------
#include <vcl.h> #include <string.h> #include <dstring> #include <stdio.h> #pragma hdrstop #include <iostream> #include "my_Image.h" #include "main.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm"
String tempFileName; FILE * myFile; unsigned char * myImageArray;
char tempCFileName[100]; char type[2]; char comment[150]; int bitcounter = 0; my_Image *myImg = new my_Image(); int temp = 999; int array_counter = 0; TColor pictureColor;
TForm1 *Form1;
//--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- //---------------------------------------------------------------------------
void __fastcall TForm1::Open1Click(TObject *Sender) { if(FileOpenBox->Execute()) { free(myImageArray); array_counter = 0; bitcounter = 0; myPicture->Picture = NULL; tempFileName = FileOpenBox->FileName; strcpy(tempCFileName, tempFileName.c_str()); myFile = fopen(tempCFileName, "rb"); fscanf(myFile, "%s", type); //Application->MessageBoxA(NULL, type, MB_OK); myImg->setType(type);
while(!feof(myFile)) { fscanf(myFile,"%d",&temp); if(temp == 999) { fgets(comment, 150, myFile); //Application->MessageBoxA(NULL, comment, MB_OK); } else { if (bitcounter == 0) myImg->setWidth(temp); if (bitcounter == 1) myImg->setHeight(temp); if (bitcounter ==2 && myImg->getType()!=1) myImg->setColorDepth(temp); if (bitcounter >2) { if(array_counter ==0) myImageArray = new unsigned char[myImg->getWidth()*myImg->getHeight()*myImg->getBytesPP()]; myImageArray[array_counter] = temp; array_counter++; } bitcounter++; } temp = 999; } fclose(myFile); }
myPicture->Width = myImg->getWidth(); myPicture->Height = myImg->getHeight();
if(myImg->getType() == 1) { for(int j = 0; j < myImg->getArraySize(); j++) { if(myImageArray[j] == 0) pictureColor = RGB(255, 255, 255); if(myImageArray[j] == 1) pictureColor = RGB(0,0,0);
myPicture->Canvas->Pixels[j%myImg->getWidth()][j/myImg->getWidth()] = pictureColor; } }
if(myImg->getType() == 2) { int tempVal; for(int j = 0; j < myImg->getArraySize(); j++) { tempVal = myImageArray[j]; if(tempVal == 0) pictureColor = RGB(0, 0, 0); if(tempVal == myImg->getColorDepth()) pictureColor = RGB(255,255,255); if(tempVal > 0 && tempVal < myImg->getColorDepth()) pictureColor = RGB(tempVal,tempVal,tempVal);
myPicture->Canvas->Pixels[j%myImg->getWidth()][j/myImg->getWidth()] = pictureColor; } } if(myImg->getType()==3) { int value_counter = 0; for( int k = 0; k<myImg->getImageSize(); k++) { int red = myImageArray[k*myImg->getBytesPP()]; int green = myImageArray[k*myImg->getBytesPP()]; int blue = myImageArray[k*myImg->getBytesPP()]; pictureColor = RGB(red,green,blue);
myPicture->Canvas->Pixels[value_counter%myImg->getWidth()][value_counter/myImg->getWidth()] = pictureColor;
value_counter++; } } } //---------------------------------------------------------------------------
void __fastcall TForm1::Exit1Click(TObject *Sender) { exit(0); } //---------------------------------------------------------------------------
|