void CDib::CreatePaletteFromImage( CPalette& palette)const
{
	if( palette.m_hObject )
		palette.DeleteObject();


	ASSERT( IsIndexed() == GetBPP() <= 8);
	if( GetBPP() <= 8)
	{
		GetPalette(palette);
		//CPalette* pal =  CPalette::FromHandle(m_hPalette);
		//PALETTEENTRY colors[256] = {0};
		//pal->GetPaletteEntries(0, 256, colors);
		
		//palette.SetPaletteEntries( 0, 256, colors );
	}
	else
	{
		LPLOGPALETTE pLogPal = (LPLOGPALETTE) new char[2 * sizeof(WORD) +
		256 * sizeof(PALETTEENTRY)];

		pLogPal->palVersion = 0x300;
		pLogPal->palNumEntries = 256;

		palette.CreatePalette(pLogPal);
		delete pLogPal;	
		pLogPal = NULL;

	
		
		CArray<COLORREF, COLORREF> colorArray;

		int height= GetHeight();
		int width = GetWidth();
		for(int i=0; i<width; i++)
		{
			for(int j=0; j<height; j++)
			{
				COLORREF color = GetPixel(i, j);
				if( UtilWin::FindInArray( colorArray, color) == -1)
					colorArray.Add(color);
				if( colorArray.GetSize() == 256)
					break;
			}
			
			if( colorArray.GetSize() == 256)
					break;
		}

		PALETTEENTRY pe[256]={0};
    
		
		for (int i = 0; i < colorArray.GetSize(); i++) 
		{
			COLORREF color = colorArray[i];
			pe[i].peRed = GetRValue(color);
			pe[i].peGreen = GetGValue(color);
			pe[i].peBlue = GetBValue(color);
		}

		palette.SetPaletteEntries(0, 256, pe);
	}
}
BOOL CDib::SetPalette( CPalette& palette )
{
	if( IsIndexed() )
	{
		int iColors = 0;
		if (!palette.GetObject(sizeof(iColors), &iColors)) 
		{
			TRACE("Failed to get num palette colors");
			return FALSE;
		}
		
		ASSERT(iColors>= 0 && iColors<=256);
//		iColors = min(iColors, GetColorTableEntries());
		PALETTEENTRY pe[256];
		palette.GetPaletteEntries(0, iColors, pe);

		RGBQUAD quad[256] = {0};
		for (int i = 0; i < iColors; i++) 
		{
			quad[i].rgbRed = pe[i].peRed;    
			quad[i].rgbGreen = pe[i].peGreen;    
			quad[i].rgbBlue = pe[i].peBlue;
		}

		SetColorTable(0, iColors, &(quad[0]));

		//update copy of the palette
		//GetPalette(m_palette);
	}
	

    return TRUE;
}
void CDib::Create(int nWidth, int nHeight, int nBPP, DWORD dwFlags )
{
	CImage::Create(nWidth, nHeight, nBPP, dwFlags );
	if( IsIndexed() )
	{
		CreateDefaultPalette();
	//	GetPalette(m_palette);
	}

}
Пример #4
0
void
Inode::IncrementNumLinks(Transaction& transaction)
{
	fNode.SetNumLinks(fNode.NumLinks() + 1);
	if (IsIndexed() && (fNode.NumLinks() >= EXT2_INODE_MAX_LINKS
		|| fNode.NumLinks() == 2)) {
		fNode.SetNumLinks(1);
		fVolume->ActivateDirNLink(transaction);
	}
}
void CDib::CreateCompatibleDIB( CDC& dc, int width, int height )
{
	Empty();
	CreateDib( CSize (width,height), dc.GetDeviceCaps( BITSPIXEL ) );
	if( IsIndexed() )
	{
		if( dc.GetCurrentPalette() )
			SetPalette( *(dc.GetCurrentPalette()) );
		else CreateDefaultPalette();
	}
	
}
Пример #6
0
bool 
UsdGeomPrimvar::ValueMightBeTimeVarying() const
{
    if (IsIndexed()) {
        if (UsdAttribute indicesAttr = _GetIndicesAttr(false)) {
            if (indicesAttr.ValueMightBeTimeVarying()) {
                return true;
            }
        }
    }

    return _attr.ValueMightBeTimeVarying();
}
Пример #7
0
bool 
UsdGeomPrimvar::GetTimeSamples(std::vector<double>* times) const
{
    bool success = _attr.GetTimeSamples(times);
    if (IsIndexed()) {
        if (UsdAttribute indicesAttr = _GetIndicesAttr(false)) {
            success = indicesAttr.GetTimeSamples(times) && success;
            _SortAndRemoveDupes(times);
        }
    }

    return success;
}
Пример #8
0
bool 
UsdGeomPrimvar::ComputeFlattened(VtValue *value, UsdTimeCode time) const
{
    VtValue attrVal;
    if (!Get(&attrVal, time)) {
        return false;
    }

    // If the primvar attr value is not an array or if the primvar isn't 
    // indexed, simply return the attribute value.
    if (!attrVal.IsArrayValued() || !IsIndexed()) {
        *value = VtValue::Take(attrVal);
        return true;
    }

    VtIntArray indices;
    if (!GetIndices(&indices, time)) {
        TF_CODING_ERROR("No indices authored for indexed primvar <%s>.", 
                        _attr.GetPath().GetText());
        return false;
    }

    // Handle all known supported array value types.
    bool foundSupportedType =
        _ComputeFlattenedArray<VtVec2fArray>(attrVal, indices, value)       || 
        _ComputeFlattenedArray<VtVec2dArray>(attrVal, indices, value)       || 
        _ComputeFlattenedArray<VtVec2iArray>(attrVal, indices, value)       || 
        _ComputeFlattenedArray<VtVec2hArray>(attrVal, indices, value)       || 
        _ComputeFlattenedArray<VtVec3fArray>(attrVal, indices, value)       || 
        _ComputeFlattenedArray<VtVec3dArray>(attrVal, indices, value)       || 
        _ComputeFlattenedArray<VtVec3iArray>(attrVal, indices, value)       || 
        _ComputeFlattenedArray<VtVec3hArray>(attrVal, indices, value)       || 
        _ComputeFlattenedArray<VtVec4fArray>(attrVal, indices, value)       || 
        _ComputeFlattenedArray<VtVec4dArray>(attrVal, indices, value)       || 
        _ComputeFlattenedArray<VtVec4iArray>(attrVal, indices, value)       || 
        _ComputeFlattenedArray<VtVec4hArray>(attrVal, indices, value)       ||
        _ComputeFlattenedArray<VtMatrix3dArray>(attrVal, indices, value)    || 
        _ComputeFlattenedArray<VtMatrix4dArray>(attrVal, indices, value)    ||
        _ComputeFlattenedArray<VtStringArray>(attrVal, indices, value)      ||
        _ComputeFlattenedArray<VtDoubleArray>(attrVal, indices, value)      ||
        _ComputeFlattenedArray<VtIntArray>(attrVal, indices, value)         ||
        _ComputeFlattenedArray<VtFloatArray>(attrVal, indices, value)       ||
        _ComputeFlattenedArray<VtHalfArray>(attrVal, indices, value);

    if (!foundSupportedType) {
        TF_WARN("Unsupported indexed primvar value type %s.", 
                attrVal.GetTypeName().c_str());
    }

    return !value->IsEmpty();
}
Пример #9
0
void SDTSPolygonReader::AssembleRings( SDTSTransfer * poTransfer,
                                       int iPolyLayer )

{
    if( bRingsAssembled )
        return;

    bRingsAssembled = TRUE;

/* -------------------------------------------------------------------- */
/*      To write polygons we need to build them from their related      */
/*      arcs.  We don't know off hand which arc (line) layers           */
/*      contribute so we process all line layers, attaching them to     */
/*      polygons as appropriate.                                        */
/* -------------------------------------------------------------------- */
    for( int iLineLayer = 0;
         iLineLayer < poTransfer->GetLayerCount();
         iLineLayer++ )
    {
        if( poTransfer->GetLayerType(iLineLayer) != SLTLine )
            continue;

        SDTSLineReader *poLineReader = reinterpret_cast<SDTSLineReader *>(
            poTransfer->GetLayerIndexedReader( iLineLayer ) );
        if( poLineReader == nullptr )
            continue;

        poLineReader->AttachToPolygons( poTransfer, iPolyLayer );
        poLineReader->Rewind();
    }

    if( !IsIndexed() )
        return;

/* -------------------------------------------------------------------- */
/*      Scan all polygons indexed on this reader, and assemble their    */
/*      rings.                                                          */
/* -------------------------------------------------------------------- */
    Rewind();

    SDTSFeature *poFeature = nullptr;
    while( (poFeature = GetNextFeature()) != nullptr )
    {
        SDTSRawPolygon  *poPoly
            = reinterpret_cast<SDTSRawPolygon *>( poFeature );

        poPoly->AssembleRings();
    }

    Rewind();
}
Пример #10
0
void CDib::CreateDefaultPalette()
{
	ASSERT( IsIndexed() );

    switch(GetBPP())
    {
    case 1: CreateDefaultPalette2(); break;
    case 4: CreateDefaultPalette16(); break;
    case 8: CreateDefaultPalette256(); break;
    case 16:
    case 24: 
    case 32: break;
    default: ASSERT(false);
    }
}
Пример #11
0
/*BOOL CDib::SetBitmap(CBitmap* pBitmap, CPalette& palette)
{
    BITMAP bm;
	pBitmap->GetBitmap(&bm);
    CSize size(bm.bmWidth, bm.bmHeight);
    
    CreateDib(size, bm.bmBitsPixel);
	
	SetPalette(palette);
	CreateSection();

	//SetPalette(*pDC->GetCurrentPalette());
	//CreateSection(pDC);
	
    
    ASSERT(m_lpImage); 
    return GetDIBits(  NULL,           // handle to device context
          (HBITMAP)(*pBitmap),      // handle to bitmap
          0,   // first scan line to set in destination bitmap
          bm.bmHeight,   // number of scan lines to copy
          m_lpImage,    // address of array for bitmap bits
          (BITMAPINFO*)m_lpBMIH, // address of structure with bitmap data
          DIB_RGB_COLORS) == bm.bmHeight;        // RGB or palette index

    //return pBitmap->GetBitmapBits(m_dwSizeImage, m_lpImage) != 0;
}
*/
BOOL CDib::SetBitmap(CDC& dc)
{
	BOOL bRep = false;
	if( dc.GetCurrentBitmap() )
	{
		CDC memDC;
		memDC.CreateCompatibleDC(&dc);

		bRep = true;
		CBitmap& bitmap = *dc.GetCurrentBitmap();
		//bitmap.FromHandle(hBitmap);
		BITMAP bm;
		bitmap.GetBitmap(&bm);
	    
		CSize size(bm.bmWidth, bm.bmHeight);
	    
		
		CreateDib(size, bm.bmBitsPixel);
		if( IsIndexed() )
		{
			if( dc.GetCurrentPalette() )
			{
				SetPalette(*dc.GetCurrentPalette());
				memDC.SelectPalette(dc.GetCurrentPalette(), false);
			}
		}

		

		memDC.SelectObject( (HBITMAP)*this);

		memDC.BitBlt( 0,0, size.cx, size.cy, &dc, 0,0, SRCCOPY); 
		//HBITMAP hBitmap = dc.SelectObject( (HBITMAP)*this );
		
		//bitmap.GetBitmapBits()

		/*for(int i=0; i<size.cx; i++)
		{
			for(int j=0; j<size.cy; j++)
			{
				SetPixel(i,j, dc.GetPixel(i, j) );
			}
		} 
		*/
	}    

	return bRep;
}
Пример #12
0
BOOL CDib::DrawTransparent (CDC* pDC, int x, int y, COLORREF crColor)
{
	ASSERT( IsIndexed() );

	CRect rect(y,x,y+GetWidth(), x-GetHeight());

	UtilWin::CIntArray selectionArray;
	GetSelectionIndex(crColor, selectionArray);


	HDC hDestDC = pDC->GetSafeHdc();

	if( selectionArray.GetSize()==1 )
	{
		return CImage::TransparentBlt(pDC->GetSafeHdc(), rect, crColor);
	}
	else if( selectionArray.GetSize()>=1 )
	{
		CDib dib(GetWidth(), GetHeight(), 32);

		/*for(int i=0; i<GetWidth(); i++)
		{
			for(int j=0; j<GetHeight(); j++)
			{
				if( 
				SetPixel(i,j,  );
			}
		}

		CImage::AlphaBlend(dib.GetDC(), rect, crColor);
		*/

		//return 
	}

	return CDib::Draw(pDC, x, y);
}
Пример #13
0
HGLOBAL CDib::CreateGlobalDIB()const
{
	int nColorTableEntries = this->GetColorTableEntries();
	DWORD dwLenHead = sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * nColorTableEntries;
	int dwSizeImage = ((GetWidth() * GetBPP()+31) & ~31) /8 * GetHeight(); 
    
    HGLOBAL hCopy = ::GlobalAlloc(GHND, dwLenHead + dwSizeImage); 
    LPBYTE pData = (LPBYTE)::GlobalLock(hCopy); 

    BITMAP bmp; 
    PBITMAPINFO pbmi = (PBITMAPINFO )pData; 
    WORD    cClrBits; 

    // Retrieve the bitmap color format, width, and height. 
//    if (!GetObject(hBmp, sizeof(BITMAP), (LPSTR)&bmp)) 
  //      return false;
	CBitmap* pBitmap = CBitmap::FromHandle( (HBITMAP)(*this));
	pBitmap->GetBitmap(&bmp);

    // Convert the color format to a count of bits. 
    cClrBits = (WORD)(bmp.bmPlanes * bmp.bmBitsPixel); 
    if (cClrBits == 1) 
        cClrBits = 1; 
    else if (cClrBits <= 4) 
        cClrBits = 4; 
    else if (cClrBits <= 8) 
        cClrBits = 8; 
    else if (cClrBits <= 16) 
        cClrBits = 16; 
    else if (cClrBits <= 24) 
        cClrBits = 24; 
    else cClrBits = 32; 

	ASSERT( cClrBits == GetBPP() );
    // Allocate memory for the BITMAPINFO structure. (This structure 
    // contains a BITMAPINFOHEADER structure and an array of RGBQUAD 
    // data structures.) 

     //if (cClrBits != 24) 
	/*if (cClrBits != 24) 
         pbmi = (PBITMAPINFO) LocalAlloc(LPTR, 
                    sizeof(BITMAPINFOHEADER) + 
                    sizeof(RGBQUAD) * (1<< cClrBits)); 

     // There is no RGBQUAD array for the 24-bit-per-pixel format. 

     else 
         pbmi = (PBITMAPINFO) LocalAlloc(LPTR, 
                    sizeof(BITMAPINFOHEADER)); 
*/
    // Initialize the fields in the BITMAPINFO structure. 

    pbmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); 
    pbmi->bmiHeader.biWidth = bmp.bmWidth; 
    pbmi->bmiHeader.biHeight = bmp.bmHeight; 
    pbmi->bmiHeader.biPlanes = bmp.bmPlanes; 
    pbmi->bmiHeader.biBitCount = bmp.bmBitsPixel; 
    if (cClrBits < 24) 
        pbmi->bmiHeader.biClrUsed = (1<<cClrBits); 

    // If the bitmap is not compressed, set the BI_RGB flag. 
    pbmi->bmiHeader.biCompression = BI_RGB; 

    // Compute the number of bytes in the array of color 
    // indices and store the result in biSizeImage. 
    // For Windows NT, the width must be DWORD aligned unless 
    // the bitmap is RLE compressed. This example shows this. 
    // For Windows 95/98/Me, the width must be WORD aligned unless the 
    // bitmap is RLE compressed.
    pbmi->bmiHeader.biSizeImage = dwSizeImage;//((pbmi->bmiHeader.biWidth * cClrBits +31) & ~31) /8
                                  //* pbmi->bmiHeader.biHeight; 
    // Set biClrImportant to 0, indicating that all of the 
    // device colors are important. 
     pbmi->bmiHeader.biClrImportant = 0; 
     
	 if( IsIndexed() )
	 {
		 GetColorTable(0, nColorTableEntries, pbmi->bmiColors);
	 }

	 pData += dwLenHead;
	 if( GetPitch() > 0 )
		memcpy( pData, GetPixelAddress(0,0), dwSizeImage);
	 else memcpy( pData, GetPixelAddress(0,GetHeight()-1), dwSizeImage);
     ::GlobalUnlock(hCopy); 


	 return hCopy;
 } 
Пример #14
0
bool CxImageJ2K::Encode(CxFile * hFile)
{
	if (EncodeSafeCheck(hFile)) return false;

	if (head.biClrUsed!=0 && !IsGrayScale()){
		strcpy(info.szLastError,"J2K can save only RGB or GrayScale images");
		return false;
	}

    int i,x,y;
    j2k_image_t *img;
    j2k_cp_t *cp;
    j2k_tcp_t *tcp;
    j2k_tccp_t *tccp;

	img = (j2k_image_t *)calloc(sizeof(j2k_image_t),1);
	cp = (j2k_cp_t *)calloc(sizeof(j2k_cp_t),1);

    cp->tx0=0; cp->ty0=0;
    cp->tw=1; cp->th=1;
    cp->tcps=(j2k_tcp_t*)calloc(sizeof(j2k_tcp_t),1);
    tcp=&cp->tcps[0];

	long w=head.biWidth;
	long h=head.biHeight;
 
	tcp->numlayers=1;
	for (i=0;i<tcp->numlayers;i++) tcp->rates[i]=(w*h*GetJpegQuality())/600;


    if (IsGrayScale()) {
        img->x0=0;
		img->y0=0;
		img->x1=w;
		img->y1=h;
        img->numcomps=1;
        img->comps=(j2k_comp_t*)calloc(sizeof(j2k_comp_t),1);
        img->comps[0].data=(int*)calloc(w*h*sizeof(int),1);
        img->comps[0].prec=8;
        img->comps[0].sgnd=0;
        img->comps[0].dx=1;
        img->comps[0].dy=1;
		for (i=0,y=0; y<h; y++) {
			for (x=0; x<w; x++,i++){
				img->comps[0].data[i]=GetPixelIndex(x,h-1-y);
			}
		}
    } else if (!IsIndexed()) {
        img->x0=0;
		img->y0=0;
		img->x1=w;
		img->y1=h;
        img->numcomps=3;
        img->comps=(j2k_comp_t*)calloc(img->numcomps*sizeof(j2k_comp_t),1);
        for (i=0; i<img->numcomps; i++) {
            img->comps[i].data=(int*)calloc(w*h*sizeof(int),1);
            img->comps[i].prec=8;
            img->comps[i].sgnd=0;
            img->comps[i].dx=1;
            img->comps[i].dy=1;
        }
		RGBQUAD c;
        for (i=0,y=0; y<h; y++) {
			for (x=0; x<w; x++,i++){
				c=GetPixelColor(x,h-1-y);
				img->comps[0].data[i]=c.rgbRed;
				img->comps[1].data[i]=c.rgbGreen;
				img->comps[2].data[i]=c.rgbBlue;
			}
		}
    } else {
        return 0;
    }
	
    cp->tdx=img->x1-img->x0;
	cp->tdy=img->y1-img->y0;

    tcp->csty=0;
    tcp->prg=0;
    tcp->mct=img->numcomps==3?1:0;
    tcp->tccps=(j2k_tccp_t*)calloc(img->numcomps*sizeof(j2k_tccp_t),1);

    int ir=0; /* or 1 ???*/

    for (i=0; i<img->numcomps; i++) {
        tccp=&tcp->tccps[i];
        tccp->csty=0;
        tccp->numresolutions=6;
        tccp->cblkw=6;
        tccp->cblkh=6;
        tccp->cblksty=0;
        tccp->qmfbid=ir?0:1;
        tccp->qntsty=ir?J2K_CCP_QNTSTY_SEQNT:J2K_CCP_QNTSTY_NOQNT;
        tccp->numgbits=2;
        tccp->roishift=0;
        j2k_calc_explicit_stepsizes(tccp, img->comps[i].prec);
    }

    BYTE* dest=(BYTE*)calloc(tcp->rates[tcp->numlayers-1]+2,1);
    long len = j2k_encode(img, cp, dest, tcp->rates[tcp->numlayers-1]+2);

    if (len==0) {
		strcpy(info.szLastError,"J2K failed to encode image");
    } else {
		hFile->Write(dest, len, 1);
	}
	
	free(dest);
	j2k_destroy(&img,&cp);

	return (len!=0);
}