File: cricontrol.cpp

package info (click to toggle)
cuneiform 1.1.0%2Bdfsg-9
  • links: PTS
  • area: non-free
  • in suites: bookworm
  • size: 71,808 kB
  • sloc: ansic: 183,506; cpp: 101,929; makefile: 82; sh: 59
file content (786 lines) | stat: -rw-r--r-- 22,668 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
/*
Copyright (c) 1993-2008, Cognitive Technologies
All rights reserved.

Разрешается повторное распространение и использование как в виде исходного кода,
так и в двоичной форме, с изменениями или без, при соблюдении следующих условий:

      * При повторном распространении исходного кода должны оставаться указанное
        выше уведомление об авторском праве, этот список условий и последующий
        отказ от гарантий.
      * При повторном распространении двоичного кода в документации и/или в
        других материалах, поставляемых при распространении, должны сохраняться
        указанная выше информация об авторском праве, этот список условий и
        последующий отказ от гарантий.
      * Ни название Cognitive Technologies, ни имена ее сотрудников не могут
        быть использованы в качестве средства поддержки и/или продвижения
        продуктов, основанных на этом ПО, без предварительного письменного
        разрешения.

ЭТА ПРОГРАММА ПРЕДОСТАВЛЕНА ВЛАДЕЛЬЦАМИ АВТОРСКИХ ПРАВ И/ИЛИ ДРУГИМИ ЛИЦАМИ "КАК
ОНА ЕСТЬ" БЕЗ КАКОГО-ЛИБО ВИДА ГАРАНТИЙ, ВЫРАЖЕННЫХ ЯВНО ИЛИ ПОДРАЗУМЕВАЕМЫХ,
ВКЛЮЧАЯ ГАРАНТИИ КОММЕРЧЕСКОЙ ЦЕННОСТИ И ПРИГОДНОСТИ ДЛЯ КОНКРЕТНОЙ ЦЕЛИ, НО НЕ
ОГРАНИЧИВАЯСЬ ИМИ. НИ ВЛАДЕЛЕЦ АВТОРСКИХ ПРАВ И НИ ОДНО ДРУГОЕ ЛИЦО, КОТОРОЕ
МОЖЕТ ИЗМЕНЯТЬ И/ИЛИ ПОВТОРНО РАСПРОСТРАНЯТЬ ПРОГРАММУ, НИ В КОЕМ СЛУЧАЕ НЕ
НЕСЁТ ОТВЕТСТВЕННОСТИ, ВКЛЮЧАЯ ЛЮБЫЕ ОБЩИЕ, СЛУЧАЙНЫЕ, СПЕЦИАЛЬНЫЕ ИЛИ
ПОСЛЕДОВАВШИЕ УБЫТКИ, СВЯЗАННЫЕ С ИСПОЛЬЗОВАНИЕМ ИЛИ ПОНЕСЕННЫЕ ВСЛЕДСТВИЕ
НЕВОЗМОЖНОСТИ ИСПОЛЬЗОВАНИЯ ПРОГРАММЫ (ВКЛЮЧАЯ ПОТЕРИ ДАННЫХ, ИЛИ ДАННЫЕ,
СТАВШИЕ НЕГОДНЫМИ, ИЛИ УБЫТКИ И/ИЛИ ПОТЕРИ ДОХОДОВ, ПОНЕСЕННЫЕ ИЗ-ЗА ДЕЙСТВИЙ
ТРЕТЬИХ ЛИЦ И/ИЛИ ОТКАЗА ПРОГРАММЫ РАБОТАТЬ СОВМЕСТНО С ДРУГИМИ ПРОГРАММАМИ,
НО НЕ ОГРАНИЧИВАЯСЬ ЭТИМИ СЛУЧАЯМИ), НО НЕ ОГРАНИЧИВАЯСЬ ИМИ, ДАЖЕ ЕСЛИ ТАКОЙ
ВЛАДЕЛЕЦ ИЛИ ДРУГОЕ ЛИЦО БЫЛИ ИЗВЕЩЕНЫ О ВОЗМОЖНОСТИ ТАКИХ УБЫТКОВ И ПОТЕРЬ.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.
    * Neither the name of the Cognitive Technologies nor the names of its
      contributors may be used to endorse or promote products derived from this
      software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

//////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//
//
//
//   Использовать
//   Версии:     CImage.dll - 31 Jan 2000
//               RDIB.dll   - 31 Jan 2000
//////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "cricontrol.h"
#include "crimemory.h"
/////////////////////////////////////////////////
//#define RIMAGE_DUMP_ENABLE

#ifdef RIMAGE_DUMP_ENABLE
#include "PumaMemoryToFileDumper.h"
#endif
//////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//
CRIControl::CRIControl()
{
	mpSourceDIB      = NULL;
	mpDestinationDIB = NULL;
	mpBinarizator    = NULL;
	mpTurner         = NULL;
	mpInvertor       = NULL;
	mpRotator        = NULL;
	mp_TurnedDIB     = NULL;
	mbMarginsFlag    = FALSE;
	DIBOpeningType   = FALSE;
	mcLastDIBName[0] = 0x0;

	/*
#define DecFunction(a) FNCIMAGE##a a; if(!CIMAGE_GetExportData(CIMAGE_FN_##a, &a)) \
				printf("CIMAGE_GetExportData == FALSE : %-3i,%s \n",a,CIMAGE_GetReturnString(CIMAGE_GetReturnCode()));
// \
	DecFunction(GetData);
	DecFunction(ReplaceData);
	DecFunction(GetCallbackImage);
	DecFunction(WriteCallbackImage);
	DecFunction(WriteDIB);
	DecFunction(ReadDIB);
	DecFunction(GetImageInfo);
	DecFunction(DeleteImage);
	DecFunction(FreeCopedDIB);

#undef  DecFunction
  */
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//
CRIControl::~CRIControl()
{
	if ( mpBinarizator )
		delete mpBinarizator;

	if ( mpTurner )
		delete mpTurner;

	if ( mpInvertor )
		delete mpInvertor;

	if ( mpRotator )
		delete mpRotator;

	if ( mpDestinationDIB )
		delete mpDestinationDIB;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//
Bool32 CRIControl::SetMargins(PRIMAGEMARGINS pMargins)
{
	SetReturnCode_rimage(IDS_RIMAGE_UNDER_CONSTRUCTION);
	return FALSE;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//
Bool32 CRIControl::Binarise(char* cDIBIn, char* cDIBOut, uint32_t wFlag, uint32_t UseMargins)
{
	Bool32 Ret = TRUE;
	CTBinarize bType = CTBIN_UNKNOWN;
	// открываем исходный
	if ( !OpenSourceDIB(cDIBIn) )
	{
		return FALSE;
	}

		/*
    if ( !SupportedIndexColorImage(mpSourceDIB) )
    {
        CloseSourceDIB();
		SetReturnCode_rimage(IDS_RIMAGE_NOTSUPPORTED_INDEX_COLOR);
		return FALSE;
    }
		*/
	// создаем новый
	if ( ! CreateDestinatonDIB(1) )           // create DIB 1 bit per pixel
	{
        CloseSourceDIB();
		SetReturnCode_rimage(IDS_RIMAGE_CANNOT_CREATE_NEW_DIB);
		return FALSE;
	}
	//открываем бинаризатор
	if ( !mpBinarizator )
	{
		mpBinarizator = new CRIBinarizator(&mcProgress);
	}
	// закидываем туда картинки
	if ( !mpBinarizator->SetRasters(mpSourceDIB, mpDestinationDIB) )
	{
		SetReturnCode_rimage(IDS_RIMAGE_CANNOT_SET_DIB);
		Ret = FALSE;
	}

	if ( wFlag < 4 )
	{
		bType = CTBIN_DEZA;
	}
	else
	{
		bType = CTBIN_KRONROD;
	}
	// бинаризуем
	if ( !mpBinarizator->Binarize(bType, wFlag) )
	{
		SetReturnCode_rimage(IDS_RIMAGE_CANT_BINARYZE);
		Ret = FALSE;
	}
	//отписваем новый в контейнер и освобождаем
	if ( !CloseDestinationDIB(cDIBOut) )
	{
		SetReturnCode_rimage(IDS_RIMAGE_UNDER_CONSTRUCTION);
		Ret = FALSE;
	}

	//закрываем исходный
	if ( !CloseSourceDIB() )
	{
		SetReturnCode_rimage(IDS_RIMAGE_UNDER_CONSTRUCTION);
		Ret = FALSE;
	}
	return Ret;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//
Bool32 CRIControl::Rotate(char*  cDIBIn, char*  cDIBOut, int32_t High, int32_t Low, uint32_t UseMargins)
{
	Bool32 Ret = TRUE;
	Bool32 NoDest = FALSE;

	// открываем исходный
	if ( !OpenSourceDIB(cDIBIn) )
	{
		return FALSE;
	}

	if ( mpDestinationDIB )
	{
		SetReturnCode_rimage(IDS_RIMAGE_INTERNAL_MODULE_ERROR);
		return FALSE;
	}

	mpDestinationDIB = new CTDIB;

	//открываем вертелку
	if ( !mpRotator )
	{
		mpRotator = new CRRotator(&mcProgress);
	}

	// забываем старое имя
	mcLastDIBName[0] = 0x0;

	if ( !mpRotator->Rotate(mpSourceDIB, mpDestinationDIB, High, Low) )
	{
		uint16_t wRet = GetReturnCode_rimage();
// !!! Art Изменил - теперь она заносит не хендлы, а указатели, а то память утекала
//почему-то...
		//		Handle hDIBtoSet;
		pvoid  pDIBtoSet;

		if ( (wRet == IDS_RIMAGE_ZERO_NUMERATOR_OR_DENUMERATOR ||
			  wRet == IDS_RIMAGE_ANGLE_LEAST_MINIMUM )         &&
			  mpSourceDIB->GetDIBPtr( &pDIBtoSet )
			  /*mpSourceDIB->GetDIBHandle(&hDIBtoSet)*/ )
		{
//			SetDIB(cDIBOut, hDIBtoSet);
			WriteDIB(cDIBOut, pDIBtoSet);
			SetReturnCode_rimage(IDS_RIMAGE_ERR_NO);
			NoDest = Ret = TRUE;
		}
		else
		{
			SetReturnCode_rimage(IDS_RIMAGE_CANNOT_ROTATE_IMAGE);
			Ret = FALSE;
		}
	}
	else
	{
		WriteDIBtoBMP("Allex.DIBBeforeDeskew.bmp",mpSourceDIB);
		WriteDIBtoBMP("Allex.DIBAfterDeskew.bmp",mpDestinationDIB);
	}

	//отписваем новый в контейнер и освобождаем
	if ( !CloseDestinationDIB(cDIBOut) )
	{
		if ( NoDest == FALSE )
		{
			SetReturnCode_rimage(IDS_RIMAGE_CANNT_SAVE_OUTCOMING_DIB);
			Ret = FALSE;
		}
	}

	strcpy(mcLastDIBName, cDIBOut);

	//закрываем исходный
	if ( !CloseSourceDIB() )
	{
		SetReturnCode_rimage(IDS_RIMAGE_UNDER_CONSTRUCTION);
		Ret = FALSE;
	}
	return Ret;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//
Bool32 CRIControl::Turn(char*  cDIBIn, char*  cDIBOut, uint32_t wFlag, uint32_t UseMargins)
{
	int32_t     NewWidth;
	int32_t     NewHeight;
	Bool32    bRet = FALSE;
	//void *   pSDIB;

	if ( wFlag != RIMAGE_TURN_90 && wFlag != RIMAGE_TURN_270 && wFlag != RIMAGE_TURN_180 )
	{
		SetReturnCode_rimage(IDS_RIMAGE_INVALID_FUNCTION_PARAMETR);
		return FALSE;
	}

	if ( mp_TurnedDIB != NULL )
	{
		SetReturnCode_rimage(IDS_RIMAGE_INTERNAL_MODULE_ERROR);
		return FALSE;
	}
	// открываем исходный
	if ( !OpenSourceDIB(cDIBIn) )
	{
		return FALSE;
	}
	NewWidth = ( wFlag == RIMAGE_TURN_180 ? mpSourceDIB->GetImageWidth() : mpSourceDIB->GetImageHeight() );
	NewHeight = ( wFlag == RIMAGE_TURN_180 ? mpSourceDIB->GetImageHeight() : mpSourceDIB->GetImageWidth() );
	/*
	if ( !mpSourceDIB->GetDIBPtr( &pSDIB ) )
	{
		SetReturnCode_rimage(IDS_RIMAGE_INVALID_EXTERNAL_DIB);
		return FALSE;
	}
	*/
	//открываем вертелку
	if ( !mpTurner )
	{
		mpTurner = new CRTurner;
	}

	// генерим новенький
	if ( mpDestinationDIB )
	{
		SetReturnCode_rimage(IDS_RIMAGE_INTERNAL_MODULE_ERROR);
		return FALSE;
	}

	mpDestinationDIB = new CTDIB;
	RIMAGEComment("Turn - temporary destination DIB");
	mpDestinationDIB->SetExternals( RIMAGEAlloc, RIMAGEFree, RIMAGELock, RIMAGEUnlock);

	if ( mpDestinationDIB->CreateDIBBegin(NewWidth,
		                                  NewHeight,
										  mpSourceDIB->GetPixelSize()) &&
	     mpDestinationDIB->CopyPalleteFromDIB(mpSourceDIB) &&
		 mpDestinationDIB->CopyDPIFromDIB(mpSourceDIB) &&
		 mpDestinationDIB->CreateDIBEnd()  )
	{
		bRet = mpTurner->TurnDIB(mpSourceDIB, mpDestinationDIB, wFlag );
	}



	// вертим
	/*
	if ( !(mp_TurnedDIB = mpTurner->TurnDIB(pSDIB, wFlag)) )
	{
		SetReturnCode_rimage(IDS_RIMAGE_CANT_TURN_DIB);
		return FALSE;
	}
	*/

	/*
	//отписваем новый в контейнер и освобождаем
	if ( !WriteDIB(cDIBOutt, mp_TurnedDIB) )
	{
		SetReturnCode_rimage(IDS_RIMAGE_UNABLE_WRITE_DIB);
		return FALSE;
	}

	if ( !mpTurner->FreeDIB(mp_TurnedDIB) )
	{
		SetReturnCode_rimage(IDS_RIMAGE_INVALID_EXTERNAL_DIB);
		return FALSE;
	}

	mp_TurnedDIB = NULL;
	*/
	//отписваем новый в контейнер и освобождаем
	if ( !CloseDestinationDIB(cDIBOut) )
	{
		SetReturnCode_rimage(IDS_RIMAGE_CANNT_SAVE_OUTCOMING_DIB);
		bRet = FALSE;
	}
	//закрываем исходный
	if ( !CloseSourceDIB() )
	{
		SetReturnCode_rimage(IDS_RIMAGE_UNDER_CONSTRUCTION);
		bRet = FALSE;
	}
	return bRet;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//
Bool32 CRIControl::Inverse(char*  cDIBIn, char*  cDIBOut, uint32_t UseMargins)
{
	Bool32    bErrors = TRUE;
	// копируем из исходного DIB в обрабатываемый
	// получаем заполненный mpDescinationDIB и пустой mpSourceDIB
	if ( !OpenDestinationDIBfromSource( cDIBIn ) )
	{
		return FALSE;
	}

	//открываем инвертор
	if ( !mpInvertor )
	{
		mpInvertor = new CRInvertor;
	}

	// Инвертируем
	if ( !mpInvertor->Inverse(mpDestinationDIB) )
	{
		SetReturnCode_rimage(IDS_RIMAGE_CANNOT_INVERT_IMAGE);
		bErrors = FALSE;
	}

	//отписваем новый в контейнер и освобождаем
	if ( !SetDestinationDIBtoStorage(cDIBOut) )
	{
		SetReturnCode_rimage(IDS_RIMAGE_UNABLE_WRITE_DIB);
		bErrors = FALSE;
	}

	return bErrors;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// взять без копировыания
Bool32 CRIControl::GetDIB(char*  cDIB, Handle* phDIB)
{
	// берем с копированием, что б маска была!
	if ( CIMAGE_ReadDIB((puchar)cDIB, phDIB, TRUE) )
		return TRUE;

	SetReturnCode_rimage(IDS_RIMAGE_NO_IMAGE_FOUND);
	return FALSE;

}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// положить без копировыания
Bool32 CRIControl::SetDIB(char*  cDIB, Handle hDIB)
{
	if ( CIMAGE_WriteDIB((puchar)cDIB, hDIB, TRUE) )
		return TRUE;

	SetReturnCode_rimage(IDS_RIMAGE_UNABLE_WRITE_DIB);
	return FALSE;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// положитьь c копировыанием
Bool32 CRIControl::WriteDIB(char*  cDIB, Handle hDIB)
{
	if ( CIMAGE_WriteDIB((puchar)cDIB, hDIB, FALSE) )
		return TRUE;

	SetReturnCode_rimage(IDS_RIMAGE_NO_IMAGE_FOUND);
	return FALSE;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// взять с копировыанием
Bool32 CRIControl::ReadDIB(char*  cDIB, Handle* phDIB)
{
	if ( CIMAGE_ReadDIB((puchar)cDIB, phDIB, FALSE) )
		return TRUE;

	SetReturnCode_rimage(IDS_RIMAGE_UNABLE_WRITE_DIB);
	return FALSE;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// Close Source DIB
Bool32 CRIControl::CloseSourceDIB()
{

	Handle    hDIB;

	if ( mpSourceDIB == NULL )
	{
		SetReturnCode_rimage(IDS_RIMAGE_NOT_OPENED);
		return FALSE;
	}

	if ( mpSourceDIB->GetDIBHandle(&hDIB) )
	{
		RIMAGEUnlock(hDIB);
	}

	delete mpSourceDIB;
	CIMAGE_FreeCopedDIB(hDIB);

	return TRUE;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//
Bool32 CRIControl::OpenSourceDIB(char*  cDIBName)
{
	Handle  hDIBIn;
	pvoid   pDIB;

	if ( !ReadDIB(cDIBName, &hDIBIn) )
	{
		SetReturnCode_rimage(IDS_RIMAGE_NO_IMAGE_FOUND);
		return FALSE;
	}

	if ( !(pDIB = RIMAGELock(hDIBIn) ) )
	{
		SetReturnCode_rimage(IDS_RIMAGE_INTERNAL_MODULE_ERROR);
		return FALSE;
	}

	mpSourceDIB = new CTDIB(hDIBIn);

	if ( !mpSourceDIB->SetDIBbyPtr(pDIB) )
	{
		delete mpSourceDIB;
		mpSourceDIB = NULL;
		SetReturnCode_rimage(IDS_RIMAGE_DIB_NOT_ATTACHED);
		return FALSE;
	}

	return TRUE;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
//Создаем временный DIB куда отпишем, что получили
Bool32 CRIControl::CloseDestinationDIB(char*  cDIBName)
{
	Handle hDIB = NULL;
	pvoid  pDIB = NULL;

	if ( DIBOpeningType == TRUE )
	{
		return SetDestinationDIBtoStorage(cDIBName);
	}

	if ( !mpDestinationDIB )
		return FALSE;

	if ( !mpDestinationDIB->GetDIBHandle(&hDIB) ||
		 !mpDestinationDIB->GetDIBPtr( &pDIB )     )
	{
		delete mpDestinationDIB;
		mpDestinationDIB = NULL;
		return FALSE;
	}

	if ( !WriteDIB(cDIBName, pDIB) )
	{
		SetReturnCode_rimage(IDS_RIMAGE_NO_IMAGE_FOUND);
		return FALSE;
	}

	/*
	while ( mpDestinationDIB )
	{
		if ( mpDestinationDIB->DestroyDIB() )
		{
			hDIB = NULL;
		}
		else
		{
			if ( mpDestinationDIB->GetDIBHandle(&hDIB) )
			{
				RIMAGEUnlock(hDIB);
				RIMAGEFree(hDIB);
			}
			else
			{
				delete mpDestinationDIB;
				mpDestinationDIB = NULL;
			}
		}
	}
	*/
	delete mpDestinationDIB;
	mpDestinationDIB = NULL;

	return TRUE;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////
// Creating new DIB by CTDIB class and 4 RIMAGE functions
Bool32 CRIControl::CreateDestinatonDIB(uint32_t BitCount)
{
	uint32_t wNewHeight;
	uint32_t wNewWidth;
	uint32_t wXResolution;
	uint32_t wYResolution;
	CTDIBRGBQUAD       BWQuads[2] = {{0x00,0x00,0x00,0x00},{0xff,0xff,0xff,0x00} };


	if ( mpSourceDIB == NULL )
	{
		SetReturnCode_rimage(IDS_RIMAGE_NOT_OPENED);
		return FALSE;
	}

	mpDestinationDIB = new CTDIB;

	RIMAGEComment("CreateDestinationDIB - temporary DIB");
	if ( !mpDestinationDIB->SetExternals(RIMAGEAlloc, RIMAGEFree, RIMAGELock, RIMAGEUnlock) )
	{
		SetReturnCode_rimage(IDS_RIMAGE_PREPARE_TO_CREATE);
		delete mpDestinationDIB;
		mpDestinationDIB = NULL;
		return FALSE;
	}

	wNewHeight = (mbMarginsFlag ? std::abs(int32_t(mrMargins.rmBottomMarg) - int32_t(mrMargins.rmTopMarg)) : mpSourceDIB->GetLinesNumber());
	wNewWidth = (mbMarginsFlag ? std::abs(int32_t(mrMargins.rmLeftMarg) - int32_t(mrMargins.rmRightMarg)) : mpSourceDIB->GetLineWidth());
	mpSourceDIB->GetResolutionDPM( &wXResolution, &wYResolution);

	if ( !mpDestinationDIB->CreateDIBBegin( wNewWidth, wNewHeight, BitCount) )
	{
		return FALSE;
	}

	if ( !mpDestinationDIB->SetResolutionDPM(wXResolution, wYResolution) )
	{
		//return FALSE;
	}

	if ( !mpDestinationDIB->SetRGBQuad(0, BWQuads[0] ) )
	{
		return FALSE;
	}

	if ( !mpDestinationDIB->SetRGBQuad(1, BWQuads[1] ) )
	{
		return FALSE;
	}

	if ( !mpDestinationDIB->CreateDIBEnd() )
	{
		return FALSE;
	}
	return TRUE;
}

Bool32 CRIControl::OpenDestinationDIBfromSource(char*  cDIBName)
{
	Handle  hDIBIn;
	pvoid   pDIB;

	if ( mpSourceDIB != NULL )
		return FALSE;

	if ( !ReadDIB(cDIBName, &hDIBIn) )
	{
		SetReturnCode_rimage(IDS_RIMAGE_NO_IMAGE_FOUND);
		return FALSE;
	}

	if ( !(pDIB = RIMAGELock(hDIBIn) ) )
	{
		SetReturnCode_rimage(IDS_RIMAGE_INTERNAL_MODULE_ERROR);
		return FALSE;
	}

	mpDestinationDIB = new CTDIB(hDIBIn);

	if ( !mpDestinationDIB->SetDIBbyPtr(pDIB) )
	{
		delete mpSourceDIB;
		mpDestinationDIB = NULL;
		SetReturnCode_rimage(IDS_RIMAGE_DIB_NOT_ATTACHED);
		return FALSE;
	}

	DIBOpeningType = TRUE;
	return TRUE;
}

Bool32 CRIControl::SetDestinationDIBtoStorage(char*  cDIBName)
{
	Handle hSDIB;
	Bool32  bErrors = TRUE;

	if ( DIBOpeningType == FALSE)
	{
		return CloseDestinationDIB(cDIBName);
	}
	// записывваем в блок выделеный в CIMAGE при открытии
	if ( mpDestinationDIB == NULL )
		return FALSE;

	if ( !mpDestinationDIB->GetDIBHandle(&hSDIB) )
		return FALSE;

	if ( !SetDIB(cDIBName, hSDIB ) )
	{
		SetReturnCode_rimage(IDS_RIMAGE_NO_IMAGE_FOUND);
		return FALSE;
	}


	if ( !CIMAGE_FreeCopedDIB(hSDIB) )
	{
		SetReturnCode_rimage(IDS_RIMAGE_CIMAGE_MEMORY_ERROR);
		bErrors = FALSE;
	}

	delete mpDestinationDIB;
	mpDestinationDIB = NULL;
	DIBOpeningType = FALSE;

	return bErrors;
}

Bool32 CRIControl::Roll(char* cDIBIn, char* cDIBOut, int32_t Num, int32_t Denum, uint32_t bUseMargins)
{
	Bool32 Ret = TRUE;

	// открываем исходный
	if ( !OpenSourceDIB(cDIBIn) )
	{
		return FALSE;
	}

	if ( mpDestinationDIB )
	{
		SetReturnCode_rimage(IDS_RIMAGE_INTERNAL_MODULE_ERROR);
		return FALSE;
	}
	mpDestinationDIB = new CTDIB;

	//открываем вертелку
	if ( !mpRotator )
	{
		mpRotator = new CRRotator;
	}

	if ( !mpRotator->Roll(mpSourceDIB, mpDestinationDIB, Num, Denum) )
	{
		SetReturnCode_rimage(IDS_RIMAGE_CANNOT_ROTATE_IMAGE);
		Ret = FALSE;
	}

	//отписваем новый в контейнер и освобождаем
	if ( !CloseDestinationDIB(cDIBOut) )
	{
		SetReturnCode_rimage(IDS_RIMAGE_CANNT_SAVE_OUTCOMING_DIB);
		Ret = FALSE;
	}

	//закрываем исходный
	if ( !CloseSourceDIB() )
	{
		SetReturnCode_rimage(IDS_RIMAGE_UNDER_CONSTRUCTION);
		Ret = FALSE;
	}
	return Ret;
}

Bool32 CRIControl::SetProgressCallBacks(PRIMAGECBPRogressStart pcbStart, PRIMAGECBPRogressStep pcbStep, PRIMAGECBPRogressFinish pcbFinish)
{
	return mcProgress.SetExternals((CRPStart)pcbStart, (CRPFinish)pcbFinish, (CRPStep)pcbStep);
}

Bool32 CRIControl::StartProgress()
{
	return mcProgress.Start();
}

Bool32 CRIControl::RotatePoint(char* cDIB, int32_t iX, int32_t iY, int32_t * prX, int32_t * prY)
{
	Bool32 bRet = FALSE;

	if ( mpRotator && strcmp(cDIB, mcLastDIBName) == 0 )
	{
		bRet = mpRotator->RotatePoint(iX, iY, prX, prY);
	}

	return bRet;
}

Bool32 CRIControl::WriteDIBtoBMP(const char *cName, PCTDIB pDIB)
{
#ifdef RIMAGE_DUMP_ENABLE

	uint32_t  wBMPSize = pDIB->GetDIBSize() + 14;
	PumaMemoryToFileDumper BMPDump(cName);

	BMPDump.AddDump("BM",2);
	BMPDump.AddDump((char *)&wBMPSize,4);
	wBMPSize = 0;
	BMPDump.AddDump((char *)&wBMPSize,4);
	wBMPSize = 14 + pDIB->GetHeaderSize() + pDIB->GetRGBPalleteSize();
	BMPDump.AddDump((char *)&wBMPSize,4);
	BMPDump.AddDump(pDIB->GetPtrToHeader(), pDIB->GetDIBSize());
	BMPDump.EndDump();

#endif
	return TRUE;
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////
// end of file