Exemplo n.º 1
0
Arquivo: Noise.c Projeto: PelinV/Elman
//Noise::
unsigned int delete_noise(char* src){
	FILE * pFile;
	if((pFile = fopen(src, "rb")) == NULL){
		puts("Error: wrong source.\n");
		return 0;
	}

    // считываем заголовок файла
    BITMAPFILEHEADER header __attribute__((unused));
 
    header.bfType      = read_u16(pFile);
    header.bfSize      = read_u32(pFile);
    header.bfReserved1 = read_u16(pFile);
    header.bfReserved2 = read_u16(pFile);
    header.bfOffBits   = read_u32(pFile);

    // считываем заголовок изображения
    BITMAPINFOHEADER bmiHeader;

    bmiHeader.biSize          = read_u32(pFile);
    bmiHeader.biWidth         = read_s32(pFile);
    bmiHeader.biHeight        = read_s32(pFile);
    bmiHeader.biPlanes        = read_u16(pFile);
    bmiHeader.biBitCount      = read_u16(pFile);
    bmiHeader.biCompression   = read_u32(pFile);
    bmiHeader.biSizeImage     = read_u32(pFile);
    bmiHeader.biXPelsPerMeter = read_s32(pFile);
    bmiHeader.biYPelsPerMeter = read_s32(pFile);
    bmiHeader.biClrUsed       = read_u32(pFile);
    bmiHeader.biClrImportant  = read_u32(pFile);

	// unsigned int *img = new unsigned int[bmiHeader.biWidth*bmiHeader.biHeight];
	unsigned int *img = malloc(sizeof(unsigned int)*bmiHeader.biWidth*bmiHeader.biHeight);

	int i, j, k;

	for (i = 0; i < bmiHeader.biHeight * bmiHeader.biWidth; ++i)
	{
		img[i] = 0;
	}

	for (i = 0; i < bmiHeader.biHeight; ++i)
	{
		for (j = 0; j < bmiHeader.biWidth; ++j)
		{
			for (k = 0; k < 3; ++k)
			{
				img[i*bmiHeader.biWidth + j] |= getc(pFile) << (8*k);
			}
		}
		for (j = 0; j < bmiHeader.biWidth%4; ++j)
		{
			getc(pFile);
		}
	}

	for (i = 0, j = 0; i < bmiHeader.biWidth * bmiHeader.biHeight; ++i)
	{
		img[i] = (img[i] == 0)?0xFFFFFF:0;
	}

	// unsigned int *col = new unsigned int[bmiHeader.biHeight];
	unsigned int *col = malloc(sizeof(unsigned int)*bmiHeader.biHeight);

	// unsigned int *row = new unsigned int[bmiHeader.biWidth];
	unsigned int *row = malloc(sizeof(unsigned int)*bmiHeader.biWidth);

	for (i = 0; i < bmiHeader.biWidth; ++i)
	{
		row[i] = 0;
	}

	for (i = 0; i < bmiHeader.biWidth; ++i)
	{
		for (j = 0; j < bmiHeader.biHeight; ++j)
		{
			row[i] |= img[i + j*bmiHeader.biWidth];
		}
	}

	int n = 0;

	for (i = 0; i < bmiHeader.biWidth - 1; ++i)
	{
		if(row[i + 1] != row[i])
			n++;
	}
	n = n/2;

	int length = 0, start_l = 0, l = 0;
	int vertical = 0, start_v = 0;

	for (k = 0; k < n; ++k)
	{	
		l = length + start_l;
		length = 0;
		for (; l < bmiHeader.biWidth - 1; ++l)
		{
			if(row[0] != 0)
				start_l = 0;
			if(row[l] == 0 && row[l + 1] != row[l])
				start_l = l + 1;
			if(row[l] != 0 && row[l + 1] == row[l])
				++length;
			if(row[l] != 0 && row[l + 1] != row[l]){
				++length;
				break;
			}
		}

//COL
		for (i = 0; i < bmiHeader.biHeight; ++i)
		{
			col[i] = 0;
		}

		for (i = 0; i < bmiHeader.biHeight; ++i)
		{
			for (j = 0; j < length; ++j)
			{
				col[i] |= img[j + start_l + i*bmiHeader.biWidth];
			}
		}

		vertical = 1;

		for (i = 0; i < bmiHeader.biHeight - 1; ++i)
		{
			if(col[i] == 0 && col[i + 1] != 0)
				start_v = i + 1;
			if(col[i] != 0)
				++vertical;
		}

		// unsigned int* p = new unsigned int[length * vertical];
		unsigned int* p = malloc(sizeof(unsigned int)*length * vertical);

		for(i = 0; i < length; ++i)
		{
			for (j = 0; j < vertical; ++j)
			{
				p[i + j*length] = (img[(i + start_l) + (j + start_v)*bmiHeader.biWidth])?0:0xFFFFFF;
			}
		}
		// char *name = new char[5];
		char *name = malloc(sizeof(char)*5);;
		name[0] = k + 0x31;
		name[1] = '.';
		name[2] = 'b';
		name[3] = 'm';
		name[4] = 'p';
		CreateBMP(name, p, length, vertical);
		// delete[] p;
		free(p);
	}



	// CreateBMP((char*)"result.bmp", img, bmiHeader.biWidth, bmiHeader.biHeight);
	fclose(pFile);

	// delete[] col;
	free(col);
	// delete[] row;
	free(row);
	// delete[] img;
	free(img);

	return n;
}
Exemplo n.º 2
0
bool AP_Win32App::_pasteFormatFromClipboard(PD_DocumentRange * pDocRange, const char * szFormat,
											const char * szType, bool bWide)
{
	HANDLE	hData;
	bool 	bSuccess = false;	
  
	if (!(hData = m_pClipboard->getHandleInFormat(szFormat)))
		return bSuccess;		
 		
 	// It's a bitmap
 	if (g_ascii_strcasecmp(szFormat, AP_CLIPBOARD_BMP)==0)
	{			
 		HBITMAP					hBitmap;
 		PBITMAPINFO 			bi;
 		HWND		 			hWnd;
 		HDC 					hdc;
 		IE_ImpGraphic*			pIEG = NULL;
 		FG_Graphic* 			pFG = NULL;	
 		UT_Error 				errorCode;		
 		UT_ByteBuf 				byteBuf;				
 		IEGraphicFileType		iegft = IEGFT_BMP;	
 		XAP_Frame* 				pFrame;						
 		AP_FrameData* 			pFrameData;		
 		FL_DocLayout*			pDocLy;	
 		FV_View* 				pView;						
		UT_ByteBuf*				bBufBMP = new UT_ByteBuf;
 		
 		hBitmap = (HBITMAP)hData;					
 		hWnd =  GetDesktopWindow();
 		hdc = GetDC(hWnd);		
 		
 		// Create a BMP file from a BITMAP
 		bi =  CreateBitmapInfoStruct(hBitmap);						
 		CreateBMP(hWnd, *bBufBMP, bi, hBitmap,hdc);                  										
 		
 		// Since we are providing the file type, there is not need to pass the bytebuff filled up
 		errorCode = IE_ImpGraphic::constructImporter(*bBufBMP, iegft, &pIEG);				 				
		 				
 		if(errorCode != UT_OK)		
			return false;				  	
		 				 			
 		errorCode = pIEG->importGraphic(bBufBMP, &pFG); 		
 		
 		if(errorCode != UT_OK || !pFG)
		{
			DELETEP(bBufBMP);
			DELETEP(pIEG);
 			return false;
		}
		// sunk in importGraphic
		bBufBMP = NULL;
 		 
 		// Insert graphic in the view
 		pFrame = getLastFocussedFrame(); 						
 		pFrameData = (AP_FrameData*) pFrame->getFrameData();		
 		pDocLy =	pFrameData->m_pDocLayout;	
 		pView =  pDocLy->getView();		
 				
 		errorCode = pView->cmdInsertGraphic(pFG);	  		  		
 	
		DELETEP(pIEG);
 		//DELETEP(pFG);		
 		
 		bSuccess = true;
 	}
 	else	
	{
		unsigned char * pData = static_cast<unsigned char *>(GlobalLock(hData));
		UT_DEBUGMSG(("Paste: [fmt %s %s][hdata 0x%08lx][pData 0x%08lx]\n",
					 szFormat, szType,  hData, pData));
		UT_uint32 iSize = GlobalSize(hData);
		UT_uint32 iStrLen = bWide
			? wcslen(reinterpret_cast<const wchar_t *>(pData)) * 2
			: strlen(reinterpret_cast<const char *>(pData));
		UT_uint32 iLen = UT_MIN(iSize,iStrLen);

		
		IE_Imp * pImp = 0;
		IE_Imp::constructImporter(pDocRange->m_pDoc, IE_Imp::fileTypeForSuffix(szType), &pImp, 0);
		if (pImp)
		{
			const char * szEncoding = 0;
			if (bWide)
				szEncoding = XAP_EncodingManager::get_instance()->getUCS2LEName();
			else
				; // TODO Get code page using CF_LOCALE
			pImp->pasteFromBuffer(pDocRange,pData,iLen,szEncoding);
			delete pImp;
		}

		GlobalUnlock(hData);
		bSuccess = true;
	}
	return bSuccess;
}