agent/browser/ie/pagetest/PagetestReporting.cpp
/*-----------------------------------------------------------------------------
	Check the images to make sure they have been compressed appropriately
-----------------------------------------------------------------------------*/
Check the images to make sure they have been compressed appropriately
Check the images to make sure they have been compressed appropriately
void CPagetestReporting::CheckImageCompression()
{
	compressionScore = -1;
	int count = 0;
	int total = 0;
	DWORD totalBytes = 0;
	DWORD targetBytes = 0;
	int imgNum = 0;
	ATLTRACE(_T("[Pagetest] - CheckImageCompression\n"));
	POSITION pos = events.GetHeadPosition();
	while( pos )
	{
		CTrackedEvent * e = events.GetNext(pos);
		if( e && e->type == CTrackedEvent::etWinInetRequest && !e->ignore )
		{
			CWinInetRequest * w = (CWinInetRequest *)e;
			CString mime = w->response.contentType;
			mime.MakeLower();
			LPBYTE body = w->body;
			DWORD bodyLen = w->bodyLen;
			if( w->fromNet && 
				w->result == 200 && 
				mime.Find(_T("image/")) >= 0 && 
				body && bodyLen)
			{
				DWORD size = bodyLen;
				DWORD target = size;
				count++;
				CxImage img;
				if( DecodeImage(img, body, bodyLen, CXIMAGE_FORMAT_UNKNOWN) )
				{
					DWORD type = img.GetType();
					switch( type )
					{
						case CXIMAGE_FORMAT_GIF:
						case CXIMAGE_FORMAT_PNG:
								{
									w->compressionScore = 100;
								}
								break;
						case CXIMAGE_FORMAT_JPG:							
								{
									img.SetCodecOption(8, CXIMAGE_FORMAT_JPG);	
									img.SetCodecOption(16, CXIMAGE_FORMAT_JPG);	
									img.SetJpegQuality(85);
									target = size = bodyLen;
									BYTE * mem = NULL;
									int len = 0;
									if( img.Encode(mem, len, CXIMAGE_FORMAT_JPG) )
									{
										img.FreeMemory(mem);
										target = (DWORD)len < size ? (DWORD)len : size;
                    w->compressionScore = 100;
                    if (target && target < size && size > 1400)
                    {
                      double ratio = (double)size / (double)target;
                      if (ratio >= 1.5)
                        w->compressionScore = 0;
                      else if (ratio >= 1.1)
                        w->compressionScore = 50;
                    }
									}
								}
								break;
						default:
								{
									w->compressionScore = 0;
								}
								break;
					}
					if( target > size )
						target = size;
					totalBytes += size;
					targetBytes += target;
					w->compressTotal = size;
					w->compressTarget = target;
				}
				else
				{
					w->compressionScore = 0;
				}
				if( !w->compressionScore )
					w->warning = true;
				total += w->compressionScore;
			}
		}
	}
	compressTotal = totalBytes;
	compressTarget = targetBytes;
	if( count && totalBytes )
		compressionScore = targetBytes * 100 / totalBytes;
}
void CPagetestReporting :: CheckImageCompression ( ) { compressionScore = -1 ; int count = 0 ; int total = 0 ; DWORD totalBytes = 0 ; DWORD targetBytes = 0 ; int imgNum = 0 ; ATLTRACE ( _T ( " \n " ) ) ; POSITION pos = events . GetHeadPosition ( ) ; while ( pos ) { CTrackedEvent * e = events . GetNext ( pos ) ; if ( e && e -> type == CTrackedEvent :: etWinInetRequest && ! e -> ignore ) { CWinInetRequest * w = ( CWinInetRequest * ) e ; CString mime = w -> response . contentType ; mime . MakeLower ( ) ; LPBYTE body = w -> body ; DWORD bodyLen = w -> bodyLen ; if ( w -> fromNet && w -> result == 200 && mime . Find ( _T ( " " ) ) >= 0 && body && bodyLen ) { DWORD size = bodyLen ; DWORD target = size ; count ++ ; CxImage img ; if ( DecodeImage ( img , body , bodyLen , CXIMAGE_FORMAT_UNKNOWN ) ) { DWORD type = img . GetType ( ) ; switch ( type ) { case CXIMAGE_FORMAT_GIF : case CXIMAGE_FORMAT_PNG : { w -> compressionScore = 100 ; } break ; case CXIMAGE_FORMAT_JPG : { img . SetCodecOption ( 8 , CXIMAGE_FORMAT_JPG ) ; img . SetCodecOption ( 16 , CXIMAGE_FORMAT_JPG ) ; img . SetJpegQuality ( 85 ) ; target = size = bodyLen ; BYTE * mem = NULL ; int len = 0 ; if ( img . Encode ( mem , len , CXIMAGE_FORMAT_JPG ) ) { img . FreeMemory ( mem ) ; target = ( DWORD ) len < size ? ( DWORD ) len : size ; w -> compressionScore = 100 ; if ( target && target < size && size > 1400 ) { double ratio = ( double ) size / ( double ) target ; if ( ratio >= 1.5 ) w -> compressionScore = 0 ; else if ( ratio >= 1.1 ) w -> compressionScore = 50 ; } } } break ; default : { w -> compressionScore = 0 ; } break ; } if ( target > size ) target = size ; totalBytes += size ; targetBytes += target ; w -> compressTotal = size ; w -> compressTarget = target ; } else { w -> compressionScore = 0 ; } if ( ! w -> compressionScore ) w -> warning = true ; total += w -> compressionScore ; } } } compressTotal = totalBytes ; compressTarget = targetBytes ; if ( count && totalBytes ) compressionScore = targetBytes * 100 / totalBytes ; }
Check the images to make sure they have been compressed appropriately
Check the images to make sure they have been compressed appropriately
// all gif's and png's are considered compressed well
 // until we can integrate Page Speed's Image checking
 // optimized encoding
 // progressive
