EDN Admin
Well-known member
When I write the code:
<pre class="prettyprint #define _CRT_SECURE_NO_WARNINGS /* to suppress Visual Studio 2010 compiler warning */
#include "MoreTypes.h"
#include <stdio.h>
#include <Windows.h>
#include "Bmp24LineReader.h"
#include "Dynamic2DCharArray.h"
#include "Hw3Types.h"
#include "IspImage.h"
#include "IspImageAnalysis.h"
#include <string.h>
/**
* LoadPicture() change picture from colors to gray.
*
* accepts:
* -------
* Picture - the color picture.
* Pitch - the size of line in bytes
* Height - the height of the picture in pixeles
* Width - the width of the picture in pixles
*
* Returns:
* -------
* This function doesnt return anything - it change a global variables:
* PicGrayArr - the gray picture
* ArrOfEvents - array that signals everytime row has been changed to gray
*/
void LoadPicture(ScandAPictureRowStruct* ThreadData)
{
EReadRowRes res;
Byte_t* LineBuffer;
Byte_t Red=0, Green=0, Blue=0, Gray=0;
int j, RowNum=0;
char num=0;
DWORD dwWaitResult=0, ReleaseRes;
int Pitch=0, Width=0, Height=0;
printf("Enter Thread Functionn");
Height = ThreadData->Height;
Width = ThreadData->Width;
Pitch= ThreadData->Pitch;
//creating space for the line
LineBuffer = (Byte_t*) malloc (sizeof(Byte_t) * Pitch);
if (LineBuffer==NULL)
{
//handle error
printf("Memory Allocation Failedn");
//handle error
}
//reading the picture and making in gray
for (RowNum=0; RowNum < Height; RowNum++)
{
//reading color line
res = ReadRow (picture, LineBuffer);
if (res==FAILURE)
{
CloseBmp24LineReader( &picture );
//handle error
}
if (res != END_OF_FILE)
{
//convert pixel from color to gray
for (j=0; j<Width; j=j+3)
{
Blue = *(LineBuffer+j);
Green = *(LineBuffer+j+1);
Red = *(LineBuffer+j+2);
num=ConvertPixelColorToGrayscale(Blue, Green, Red); // Convert Color Pixel to Gray Pixel
PicGrayArr[RowNum][j]=num;
}
//signal that the line was loaded
SetEvent(ArrOfEvents[RowNum]);
}
}
ReleaseRes = ReleaseSemaphore(semap, 1, /* Signal that exactly one cell was emptied */NULL ); /* We dont care what the previous count was */
if ( ReleaseRes == FALSE )
{
//handle error
}
}
void main(int argc, char *argv[])
{
// ******* Variables *********////////
int Pitch=0, i=0, j=0;
char num=0;
char* PicFileName=NULL, *OutputFileName=NULL, *str=NULL;
int Height=0, Width=0;
int FaceWidth=0, FaceHeight =0, NumOfAreasVertical = 0, NumOfAreasHorizontal = 0, NumOfMaxThreads;
DWORD threadID, WaitRes;
Byte_t *LineBuffer=NULL, **LineBufferPointerArr=NULL;
ScandAPictureRowStruct ThreadData;
HANDLE ReadPicThread;
// // ***** Variables ********///////
//
//
//// printf("hellon");
//
// // ****** Input Handling ***********//
// if (argc!=8)
// {
// printf ("Input Errorn");
// //handle error
// }
PicFileName = argv[1];
OutputFileName = argv[2];
FaceWidth = atoi(argv [3]);
FaceHeight = atoi(argv[4]);
NumOfAreasVertical = atoi(argv[5]);
NumOfAreasHorizontal = atoi(argv [6]);
NumOfMaxThreads = atoi(argv [7]);
//
// // ****** Input Handling ************//
//
//create semaphore
semap = CreateSemaphore( NULL, // default sec. attr.
NumOfMaxThreads, // initial count
NumOfMaxThreads, // max count
NULL ); // no name
//
// //create events. We create an event for each row. Once the event is signaled it means that we have already loaded the current row
ArrOfEvents = (HANDLE*) malloc ( sizeof(HANDLE) * Height);
if (ArrOfEvents == NULL)
{
printf("error!n");
//handle error
}
//read the picture
picture= OpenBmp24LineReader(PicFileName);
if ( !IsInitialized( picture ) )
{
printf("error!n");
// handle error
}
Pitch = GetPitch(picture); // Get number of bytes in a row
Width = GetWidth (picture);
Height = GetHeight (picture);
PicGrayArr = CreateDynamic2DCharArr (Height, Width);
//initial event array
for(i=0; i < Height; i++)
{
ArrOfEvents = CreateEvent(NULL, TRUE /* manual reset*/, FALSE /*initial state is not signaled*/ , NULL /* Object not named*/ );
if (ArrOfEvents==NULL)
{
printf("Create Event Errorn");
// handle error
}
}
////creating thread to read the picture
ThreadData.picture = picture;
ThreadData.Pitch = Pitch;
ThreadData.Height = Height;
ThreadData.Width = Width;
printf("Before Seman");
printf("Before Threadn");
ReadPicThread=CreateThread( NULL, 0 /*use default stack size*/, (LPTHREAD_START_ROUTINE) LoadPicture /* thread function*/,
&ThreadData /* argument to thread function */,0 ,&threadID /* returns the thread identifier */ );
WaitRes = WaitForSingleObject(ReadPicThread, INFINITE );
if ( WaitRes != WAIT_OBJECT_0 )
{
//hanle error
}
CloseBmp24LineReader( &picture );
free (LineBufferPointerArr);
FreeDynamic2DCharArr(PicGrayArr);
} [/code]
<pre class="prettyprint" style="font-size:12px <br/> [/code]
I find out that when the program reaches the line of:
<span class="x_Apple-tab-span" style="white-spacere printf("Before Seman");<br/>
its stuck, and exits without printing the line.
Howeven, once I omit the lines:
for(i=0; i < Height; i++)<br/>
<span class="x_Apple-tab-span" style="white-spacere {<br/>
<span class="x_Apple-tab-span" style="white-spacere ArrOfEvents = CreateEvent(NULL, TRUE /* manual reset*/, FALSE /*initial state is not signaled*/ , NULL /* Object not named*/ ); <br/>
<span class="x_Apple-tab-span" style="white-spacere if (ArrOfEvents==NULL)<br/>
<span class="x_Apple-tab-span" style="white-spacere {<br/>
<span class="x_Apple-tab-span" style="white-spacere printf("Create Event Errorn"); <br/>
<span class="x_Apple-tab-span" style="white-spacere //<span class="x_Apple-tab-span" style="white-spacere
handle error<br/>
<span class="x_Apple-tab-span" style="white-spacere }<br/>
<span class="x_Apple-tab-span" style="white-spacere }
The problem disappears, meaning the program doesnt get stuck at all and continues to run.
What may be the problem with the Create Events?
View the full article
<pre class="prettyprint #define _CRT_SECURE_NO_WARNINGS /* to suppress Visual Studio 2010 compiler warning */
#include "MoreTypes.h"
#include <stdio.h>
#include <Windows.h>
#include "Bmp24LineReader.h"
#include "Dynamic2DCharArray.h"
#include "Hw3Types.h"
#include "IspImage.h"
#include "IspImageAnalysis.h"
#include <string.h>
/**
* LoadPicture() change picture from colors to gray.
*
* accepts:
* -------
* Picture - the color picture.
* Pitch - the size of line in bytes
* Height - the height of the picture in pixeles
* Width - the width of the picture in pixles
*
* Returns:
* -------
* This function doesnt return anything - it change a global variables:
* PicGrayArr - the gray picture
* ArrOfEvents - array that signals everytime row has been changed to gray
*/
void LoadPicture(ScandAPictureRowStruct* ThreadData)
{
EReadRowRes res;
Byte_t* LineBuffer;
Byte_t Red=0, Green=0, Blue=0, Gray=0;
int j, RowNum=0;
char num=0;
DWORD dwWaitResult=0, ReleaseRes;
int Pitch=0, Width=0, Height=0;
printf("Enter Thread Functionn");
Height = ThreadData->Height;
Width = ThreadData->Width;
Pitch= ThreadData->Pitch;
//creating space for the line
LineBuffer = (Byte_t*) malloc (sizeof(Byte_t) * Pitch);
if (LineBuffer==NULL)
{
//handle error
printf("Memory Allocation Failedn");
//handle error
}
//reading the picture and making in gray
for (RowNum=0; RowNum < Height; RowNum++)
{
//reading color line
res = ReadRow (picture, LineBuffer);
if (res==FAILURE)
{
CloseBmp24LineReader( &picture );
//handle error
}
if (res != END_OF_FILE)
{
//convert pixel from color to gray
for (j=0; j<Width; j=j+3)
{
Blue = *(LineBuffer+j);
Green = *(LineBuffer+j+1);
Red = *(LineBuffer+j+2);
num=ConvertPixelColorToGrayscale(Blue, Green, Red); // Convert Color Pixel to Gray Pixel
PicGrayArr[RowNum][j]=num;
}
//signal that the line was loaded
SetEvent(ArrOfEvents[RowNum]);
}
}
ReleaseRes = ReleaseSemaphore(semap, 1, /* Signal that exactly one cell was emptied */NULL ); /* We dont care what the previous count was */
if ( ReleaseRes == FALSE )
{
//handle error
}
}
void main(int argc, char *argv[])
{
// ******* Variables *********////////
int Pitch=0, i=0, j=0;
char num=0;
char* PicFileName=NULL, *OutputFileName=NULL, *str=NULL;
int Height=0, Width=0;
int FaceWidth=0, FaceHeight =0, NumOfAreasVertical = 0, NumOfAreasHorizontal = 0, NumOfMaxThreads;
DWORD threadID, WaitRes;
Byte_t *LineBuffer=NULL, **LineBufferPointerArr=NULL;
ScandAPictureRowStruct ThreadData;
HANDLE ReadPicThread;
// // ***** Variables ********///////
//
//
//// printf("hellon");
//
// // ****** Input Handling ***********//
// if (argc!=8)
// {
// printf ("Input Errorn");
// //handle error
// }
PicFileName = argv[1];
OutputFileName = argv[2];
FaceWidth = atoi(argv [3]);
FaceHeight = atoi(argv[4]);
NumOfAreasVertical = atoi(argv[5]);
NumOfAreasHorizontal = atoi(argv [6]);
NumOfMaxThreads = atoi(argv [7]);
//
// // ****** Input Handling ************//
//
//create semaphore
semap = CreateSemaphore( NULL, // default sec. attr.
NumOfMaxThreads, // initial count
NumOfMaxThreads, // max count
NULL ); // no name
//
// //create events. We create an event for each row. Once the event is signaled it means that we have already loaded the current row
ArrOfEvents = (HANDLE*) malloc ( sizeof(HANDLE) * Height);
if (ArrOfEvents == NULL)
{
printf("error!n");
//handle error
}
//read the picture
picture= OpenBmp24LineReader(PicFileName);
if ( !IsInitialized( picture ) )
{
printf("error!n");
// handle error
}
Pitch = GetPitch(picture); // Get number of bytes in a row
Width = GetWidth (picture);
Height = GetHeight (picture);
PicGrayArr = CreateDynamic2DCharArr (Height, Width);
//initial event array
for(i=0; i < Height; i++)
{
ArrOfEvents = CreateEvent(NULL, TRUE /* manual reset*/, FALSE /*initial state is not signaled*/ , NULL /* Object not named*/ );
if (ArrOfEvents==NULL)
{
printf("Create Event Errorn");
// handle error
}
}
////creating thread to read the picture
ThreadData.picture = picture;
ThreadData.Pitch = Pitch;
ThreadData.Height = Height;
ThreadData.Width = Width;
printf("Before Seman");
printf("Before Threadn");
ReadPicThread=CreateThread( NULL, 0 /*use default stack size*/, (LPTHREAD_START_ROUTINE) LoadPicture /* thread function*/,
&ThreadData /* argument to thread function */,0 ,&threadID /* returns the thread identifier */ );
WaitRes = WaitForSingleObject(ReadPicThread, INFINITE );
if ( WaitRes != WAIT_OBJECT_0 )
{
//hanle error
}
CloseBmp24LineReader( &picture );
free (LineBufferPointerArr);
FreeDynamic2DCharArr(PicGrayArr);
} [/code]
<pre class="prettyprint" style="font-size:12px <br/> [/code]
I find out that when the program reaches the line of:
<span class="x_Apple-tab-span" style="white-spacere printf("Before Seman");<br/>
its stuck, and exits without printing the line.
Howeven, once I omit the lines:
for(i=0; i < Height; i++)<br/>
<span class="x_Apple-tab-span" style="white-spacere {<br/>
<span class="x_Apple-tab-span" style="white-spacere ArrOfEvents = CreateEvent(NULL, TRUE /* manual reset*/, FALSE /*initial state is not signaled*/ , NULL /* Object not named*/ ); <br/>
<span class="x_Apple-tab-span" style="white-spacere if (ArrOfEvents==NULL)<br/>
<span class="x_Apple-tab-span" style="white-spacere {<br/>
<span class="x_Apple-tab-span" style="white-spacere printf("Create Event Errorn"); <br/>
<span class="x_Apple-tab-span" style="white-spacere //<span class="x_Apple-tab-span" style="white-spacere
handle error<br/>
<span class="x_Apple-tab-span" style="white-spacere }<br/>
<span class="x_Apple-tab-span" style="white-spacere }
The problem disappears, meaning the program doesnt get stuck at all and continues to run.
What may be the problem with the Create Events?
View the full article