示例#1
0
/*  
 *  DCFInit
 *
 *  Description:
 *      This function will initialize a dynamic count filter.
 *
 *  Parameters:
 *      dcf: [in/out]
 *          The dynamic count filter to be initialized.
 *      CBFVCounterSize: [in]
 *          The counter size of the Counting Bloom Filter Vector.
 *      length: [in]
 *          The number of counters.
 *      hashNum: [in]
 *          The hash function number.
 *      lambda: [in]
 *          Used to adjust the threshold for delayed OFV shrinking.
 *          Ranges from 0 to 1.
 *
 *  Returns:
 *      1 if successful, 0 if it failed.
 *
 *  Comments:
 *
 */
int DCFInit(DynamicCountFilter *dcf, unsigned CBFVCounterSize, 
			unsigned length, char hashNum, double lambda)
{
	if (length == 0 || !PowerOfTwo(length))  /* length must be power of 2 */
		return 0;
	if (lambda > 1 || lambda < 0)            /* lambda ranges from 0 to 1 */
		return 0;

	/* 
	 * use calloc instead of malloc in order to  
	 * initialize the allocated memory to all 0s
	 */
	if (length < 8)
		length = 8;
	dcf->CBFV = (char *)calloc(length / 8 * CBFVCounterSize, 1);
	dcf->CBFV_Counter_Size = CBFVCounterSize;
	dcf->OFV = NULL;
	dcf->OFV_Counter_Size = 0;
	dcf->Length = length;
	dcf->Hash_Num = hashNum;
	dcf->Lambda = lambda;
	dcf->Counter_Level = (Counter_Level_Item *)calloc(1, sizeof(Counter_Level_Item));
	if (lambda == 0)
		dcf->Counter_Level[0].Above_Threshold = dcf->Length;
	else
		dcf->Counter_Level[0].Below_Threshold = dcf->Length;
	return 1;
}
示例#2
0
/*  
 *  BFInit
 *
 *  Description:
 *      This function will initialize a bloom filter with specified
 *      bit array size and hash function number.
 *
 *  Parameters:
 *      bf: [in/out]
 *          The bloom filter to be initialized.
 *      m: [in]
 *          The bit array size in bits.
 *      k: [in]
 *          The hash function number.
 *
 *  Returns:
 *      1 if successful, 0 if it failed.
 *
 *  Comments:
 *
 */
int BFInit(BloomFilter *bf, unsigned m, char k)
{
	if (m == 0 || !PowerOfTwo(m))  /* m must be power of 2 */
		return 0;

	/* 
	 * use calloc instead of malloc in order to  
	 * initialize the bit array to all 0s
	 */
	if (m < 8)
		m = 8;
	bf->Bit_Array = (char *)calloc(m/8, 1);
	bf->Length = m;
	bf->Hash_Num = k;
	return 1;
}
示例#3
0
文件: image.cpp 项目: DuMuT6p/Epiar
/**\brief Converts an SDL surface to an OpenGL texture. Will free 's' by design. Do not do anything with it after this point.
 */
bool Image::ConvertToTexture( SDL_Surface *s ) {
	assert(s);

	// delete an old loaded image if one eixsts
	if( image ) {
		glDeleteTextures( 1, &image );
		image = 0;

		LogMsg(WARN, "Loading an image after another is loaded already. Deleting old ... " );
	}

	// Check to see if we need to expand the image
	int expanded_w = PowerOfTwo(s->w);
	int expanded_h = PowerOfTwo(s->h);

	if(expanded_w == 1) expanded_w = 2; // many cards won't accept 1 as a power of two
	if(expanded_h == 1) expanded_h = 2;

	if((expanded_w != s->w) || (expanded_h != s->h)) {
		// Expand the canvas (needed)
		SDL_Surface *newSurface = NULL;
		newSurface = ExpandCanvas( s, expanded_w, expanded_h ); // ExpandCavas will set new scale_w/scale_h
		s = newSurface;
	}

	// real width/height always equal the expanded canvas (or original canvas if no expansion)'s w/h
	real_w = s->w;
	real_h = s->h;

	// check the pixel format, since it could depend on the file format:
	GLenum internal_format;
 	GLenum img_format, img_type;
	switch (s->format->BitsPerPixel) {
		case 32:
			img_format = GL_RGBA;
			if(s->format->Bmask != 0x00ff0000)
				img_format = GL_BGRA;
			img_type = GL_UNSIGNED_BYTE;
			internal_format = GL_RGBA8;
			break;
		case 24:
			img_format = GL_RGB;
			img_type = GL_UNSIGNED_BYTE;
			internal_format = GL_RGB8;
			break;
		case 16:
			img_format = GL_RGBA;
			img_type = GL_UNSIGNED_SHORT;
			internal_format = GL_RGB5_A1;
			break;
		default:
			img_format = GL_LUMINANCE;
			img_type = GL_UNSIGNED_BYTE;
			internal_format=GL_LUMINANCE8;
			break;
	}

	// generate the texture
	glGenTextures( 1, &image );

	// use the bitmap data stored in the SDL_Surface
	glBindTexture( GL_TEXTURE_2D, (unsigned int)image );

	// upload the texture data, letting OpenGL do any required conversion.
	glTexImage2D( GL_TEXTURE_2D, 0, internal_format, real_w, real_h, 0, img_format, img_type, s->pixels );

	// linear filtering
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

	SDL_FreeSurface( s );

	return( true );
}
		Image* TextureManager::LoadPng(const char* filename,int ColorMode,int Swizzle,int Vram)
		{
			unsigned short *Buffer;
			//unsigned short *swizzled_pixels = NULL;

			int OutBytesPerPixel;
			int Power2Width = 0;
			int Power2Height = 0;

			png_structp png_ptr;
			png_infop info_ptr;
			unsigned int sig_read = 0;
			png_uint_32 width, height,x, y;
			int bit_depth, color_type, interlace_type;
			unsigned int* line;
			FILE *fp;

			if(ColorMode == GU_PSM_4444 || ColorMode == GU_PSM_5650 || ColorMode == GU_PSM_5551)
				OutBytesPerPixel = 2;
			else
				OutBytesPerPixel = 4;

			if ((fp = fopen(filename, "rb")) == NULL)
			{
				printf("Can't open file %s\n",filename);
				return NULL;
			}

			png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
			if (png_ptr == NULL)
			{
				fclose(fp);
				return NULL;
			}

			info_ptr = png_create_info_struct(png_ptr);
			if (info_ptr == NULL)
			{
				fclose(fp);
				png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
				return NULL;
			}

			png_init_io(png_ptr, fp);
			png_set_sig_bytes(png_ptr, sig_read);
			png_read_info(png_ptr, info_ptr);
			png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, int_p_NULL, int_p_NULL);
			png_set_strip_16(png_ptr);
			png_set_packing(png_ptr);

			if (color_type == PNG_COLOR_TYPE_PALETTE)
				png_set_palette_to_rgb(png_ptr);
			if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
				png_set_expand_gray_1_2_4_to_8(png_ptr);
			if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
				png_set_tRNS_to_alpha(png_ptr);

			png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);

			line = (unsigned int*)malloc(width * 4);
			if (!line)
			{
				fclose(fp);
				png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
				return NULL;
			}

			Power2Width = PowerOfTwo(width);
			Power2Height = PowerOfTwo(height);
			//Buffer = (unsigned short*)memalign(16,Power2Width*Power2Height*OutBytesPerPixel);
			//Buffer = (unsigned short*)malloc(sizeof(unsigned short) * (Power2Width*Power2Height*OutBytesPerPixel));
			Buffer = (unsigned short*)malloc(Power2Width*Power2Height*OutBytesPerPixel);

			for (y = 0; y < height; y++)
			{
				png_read_row(png_ptr, (unsigned char*) line, NULL);

				for (x = 0; x < width; x++)
				{
					unsigned int *Buffer32 = (unsigned int*)Buffer;
					unsigned int color32 = line[x];
					unsigned short color16;

					if(ColorMode == GU_PSM_5551)
					{
						color16 = Color8888To5551(color32);
						Buffer[y*Power2Width+x] = color16;
					}
					else if(ColorMode == GU_PSM_4444)
					{
						color16 = Color8888To4444(color32);
						Buffer[y*Power2Width+x] = color16;
					}
					else if(ColorMode == GU_PSM_5650)
					{
						color16 = Color8888To5650(color32);
						Buffer[y*Power2Width+x] = color16;
					}
					else
					{
						Buffer32[y*Power2Width+x] = color32;
					}
				}
			}

			free(line);
			png_read_end(png_ptr, info_ptr);
			png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
			fclose(fp);

			Image *Image1 = new Image();

			Image1->Width = width;
			Image1->Height = height;
			Image1->power2Width = Power2Width;
			Image1->power2Height = Power2Height;
			Image1->vRam = Vram;
			Image1->ColorMode = ColorMode;
			Image1->Swizzle = Swizzle;

			/*if (Vram == 1)
			{
				swizzled_pixels = (unsigned short*)getStaticVramTexture(Power2Width,Power2Height,ColorMode);//valloc(Image1->power2Height*Image1->power2Width*OutBytesPerPixel);

			}else
			{
				swizzled_pixels = (unsigned short*)memalign(16,Image1->power2Height*Image1->power2Width*OutBytesPerPixel);
				//swizzled_pixels = (unsigned short*)malloc(sizeof(unsigned short) * (Power2Width*Power2Height*OutBytesPerPixel));
			}

			swizzle_fast((u8*)swizzled_pixels,(const u8*)Buffer,Image1->power2Width*OutBytesPerPixel,Image1->power2Height);*/

			if (Vram == 1)
			{
				Image1->ImageData = (unsigned short*)getStaticVramTexture(Power2Width,Power2Height,ColorMode);

			}else
			{
				Image1->ImageData = (unsigned short*)malloc(Image1->power2Height*Image1->power2Width*OutBytesPerPixel);
			}
			swizzle_fast((u8*)Image1->ImageData,(const u8*)Buffer,Image1->power2Width*OutBytesPerPixel,Image1->power2Height);

			free(Buffer);

			//clear the cache or there will be some errors
			sceKernelDcacheWritebackInvalidateAll();

			return Image1;
			/*unsigned short *Buffer;
			unsigned short *swizzled_pixels = NULL;

			int OutBytesPerPixel;
			int Power2Width = 0;
			int Power2Height = 0;

			png_structp png_ptr;
			png_infop info_ptr;
			unsigned int sig_read = 0;
			png_uint_32 width, height,x, y;
			int bit_depth, color_type, interlace_type;
			unsigned int* line;
			FILE *fp;

			if(ColorMode == GU_PSM_4444 || ColorMode == GU_PSM_5650 || ColorMode == GU_PSM_5551)
				OutBytesPerPixel = 2;
			else
				OutBytesPerPixel = 4;

			if ((fp = fopen(filename, "rb")) == NULL)
			{
				printf("Can't open file %s\n",filename);
				return NULL;
			}

			png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
			if (png_ptr == NULL)
			{
				fclose(fp);
				return NULL;
			}

			info_ptr = png_create_info_struct(png_ptr);
			if (info_ptr == NULL)
			{
				fclose(fp);
				png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
				return NULL;
			}

			png_init_io(png_ptr, fp);
			png_set_sig_bytes(png_ptr, sig_read);
			png_read_info(png_ptr, info_ptr);
			png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, int_p_NULL, int_p_NULL);
			png_set_strip_16(png_ptr);
			png_set_packing(png_ptr);

			if (color_type == PNG_COLOR_TYPE_PALETTE)
				png_set_palette_to_rgb(png_ptr);
			if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
				png_set_expand_gray_1_2_4_to_8(png_ptr);
			if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
				png_set_tRNS_to_alpha(png_ptr);

			png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);

			line = (unsigned int*) malloc(width * 4);
			if (!line)
			{
				fclose(fp);
				png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
				return NULL;
			}

			Power2Width = PowerOfTwo(width);
			Power2Height = PowerOfTwo(height);
			Buffer = (unsigned short*)memalign(16,width*height*OutBytesPerPixel);

			for (y = 0; y < height; y++)
			{
				png_read_row(png_ptr, (unsigned char*) line, NULL);

				for (x = 0; x < width; x++)
				{
					unsigned int *Buffer32 = (unsigned int*)Buffer;
					unsigned int color32 = line[x];
					unsigned short color16;

					if(ColorMode == GU_PSM_5551)
					{
						color16 = Color8888To5551(color32);
						Buffer[y*width+x] = color16;
					}
					else if(ColorMode == GU_PSM_4444)
					{
						color16 = Color8888To4444(color32);
						Buffer[y*width+x] = color16;
					}
					else if(ColorMode == GU_PSM_5650)
					{
						color16 = Color8888To5650(color32);
						Buffer[y*width+x] = color16;
					}
					else
					{
						Buffer32[y*width+x] = color32;
					}
				}
			}

			free(line);
			png_read_end(png_ptr, info_ptr);
			png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
			fclose(fp);

			Image *Image1 = (Image*)malloc(sizeof(Image));

			Image1->Width = width;
			Image1->Height = height;
			Image1->power2Width = Power2Width;
			Image1->power2Height = Power2Height;
			Image1->vRam = Vram;
			Image1->ColorMode = ColorMode;
			Image1->Swizzle = Swizzle;

			if (Vram == 1)
			{
				swizzled_pixels = (unsigned short*)getStaticVramTexture(Power2Width,Power2Height,ColorMode);//valloc(Image1->power2Height*Image1->power2Width*OutBytesPerPixel);

			}else
			{
				swizzled_pixels = (unsigned short*)memalign(16,Image1->power2Height*Image1->power2Width*OutBytesPerPixel);
			}

			if(Swizzle == 1)
			{
				swizzle_fast((u8*)swizzled_pixels,(const u8*)Buffer,Image1->power2Width*OutBytesPerPixel,Image1->power2Height);

				// 512*2 because swizzle operates in bytes, and each pixel in a 16-bit texture is 2 bytes
				sceKernelDcacheWritebackAll();

				Image1->ImageData = swizzled_pixels;
				free(Buffer);
			}else
			{
				Image1->ImageData = Buffer;
			}

			return Image1;
		}

		Image* TextureManager::CreateImage(int width,int height,int ColorMode,int Vram)
		{
			Image *Image1 = (Image*)malloc(sizeof(Image));

			Image1->Width = width;
			Image1->Height = height;
			Image1->power2Width = width;
			Image1->power2Height = height;
			Image1->vRam = Vram;
			Image1->ColorMode = ColorMode;

			int OutBytesPerPixel = 0;

			if(ColorMode == GU_PSM_4444 || ColorMode == GU_PSM_5650 || ColorMode == GU_PSM_5551)
				OutBytesPerPixel = 2;
			else
				OutBytesPerPixel = 4;

			if (Vram == 1)
			{
				Image1->ImageData = (unsigned short*)getStaticVramTexture(width,height,ColorMode);

			}else
			{
				Image1->ImageData = (unsigned short*)memalign(16,Image1->power2Height*Image1->power2Width*OutBytesPerPixel);
			}

			return Image1;*/
		}
示例#5
0
void SetRecoParam( void )

{

  int dim,i,size,ftSize[3];

  DB_MSG(("-->SetRecoParam\n"));

  /* set baselevel reconstruction parameter */
  /* default initialization of reco based on acqp pars allready set */
  
  ATB_InitDefaultReco();

  for(i=0; i<PTB_GetSpatDim(); i++)
    ftSize[i] = (int)(PVM_Matrix[i]*PVM_AntiAlias[i]);

  if(PVM_EncUseMultiRec == Yes || PVM_EncPftAccel1 > 1.0)
  {
    int k=0;

   /* select method specific reconstruction method */
   RECO_mode = USER_MODE;
   ParxRelsParRelations("RECO_mode",Yes);
   ATB_InitUserModeReco(ACQ_dim, PVM_EncMatrix, ftSize, 
		     PVM_EncSteps1, PVM_EncSteps2,
		     PVM_EncNReceivers, PVM_EncPpiAccel1, PVM_EncPpiRefLines1,
		     NI, ACQ_obj_order, ACQ_phase_factor, PVM_EchoPosition);
   
   /* set scaling values for phased array coils */
   for(k=0; k<PVM_EncNReceivers;k++)
     RecoScaleChan[k] = PVM_EncChanScaling[k]; 
  }
  
  dim = PTB_GetSpatDim();

 /* set reco sizes and ft_mode (dim 2&3) */
  /* (dim 1 is kept as it was set by ATB_InitDefaultReco) */
  for(i=0; i<dim; i++)
  {
    size = (int)(PVM_Matrix[i]*PVM_AntiAlias[i]);
    RECO_ft_mode[i] = (size == PowerOfTwo(size)) ?  COMPLEX_FFT:COMPLEX_FT;
    RECO_ft_size[i] = size;
    RECO_size[i] = PVM_Matrix[i];
  }
  
  ParxRelsParRelations("RECO_ft_mode",Yes);
  ParxRelsParRelations("RECO_ft_size",Yes);
  ParxRelsParRelations("RECO_size",Yes);
  
  /* set reco rotate according to phase offsets     */
  ATB_SetRecoRotate(PVM_EffPhase1Offset,
                    PVM_Fov[1]*PVM_AntiAlias[1],
                    NSLICES,    
                    PVM_NEchoImages,
                    1) ;         /* phase1 direction*/
  
  if(dim==3)
  {
    ATB_SetRecoRotate(PVM_EffPhase2Offset,      
		      PVM_Fov[2]*PVM_AntiAlias[2],
		      NSLICES,    
		      PVM_NEchoImages,           
		      2) ;         /* phase2 direction*/
  }
  
  
  /* set reco offset */
  
  ATB_SetRecoOffset(RECO_ft_size,
		    PVM_AntiAlias,
		    NI,  
		    dim);
  for(i=0;i<dim;i++)
    RECO_fov[i]= PVM_FovCm[i];
  
  ParxRelsParRelations("RECO_fov",Yes);
  
  ATB_SetRecoTransposition(PtrType3x3 ACQ_grad_matrix[0],
			   PVM_NSPacks,
			   NSLICES,
			   ACQ_ns_list_size,
			   ACQ_obj_order) ;

  DB_MSG(("<--SetRecoParam\n"));
  
}
示例#6
0
void init_display()
{
   int surface_type_id, result, i, value, scrn, num_fbconfigs;
   GLXFBConfig *fbconfigs;
   XVisualInfo *visInfo;
   XSetWindowAttributes attributes;
   Window root;
   int attrib_pbuffer[6] = {GLX_PBUFFER_WIDTH, 0, 
                            GLX_PBUFFER_HEIGHT, 0,
                            GLX_PRESERVED_CONTENTS,
                            0};

   width = horizontal_size;
   height = vertical_size;

   if(chroma_format != CHROMA420)
      error("we only support 4:2:0 chroma formats\n");

   display = XOpenDisplay(NULL);
   root = XDefaultRootWindow(display);
   scrn = XDefaultScreen(display);

   if(!GetPortId(display, &portNum, &surface_type_id))
      error("couldn't find a suitable port\n");



#ifdef USE_DLOPEN
   if(!ResolveFunctions(DLFILENAME))
      error("couldn't resolve necessary functions\n");
#endif

   result = XvMCCreateContext(display, portNum, surface_type_id, 
                              coded_picture_width, coded_picture_height,
                              XVMC_DIRECT, &context);

   if(result != Success)
      error("couldn't create XvMCContext\n");

   for(i = 0; i < numsurfaces; i++) {
      result = XvMCCreateSurface(display, &context, &surfaces[i]);
      if(result != Success) {
          if(i < 4) {
             XvMCDestroyContext(display, &context);
             error("couldn't create enough XvMCSurfaces\n");
          } else {
             numsurfaces = i;
             printf("could only allocate %i surfaces\n", numsurfaces);
          }
      } 
      surface_info[i].reference = 0;
      surface_info[i].sequence_number = 0;      
   }

   slices = slices * mb_width;

   XvMCCreateBlocks(display, &context, slices * 6, &blocks);
   XvMCCreateMacroBlocks(display, &context, slices, &macro_blocks);

   fbconfigs = glXChooseFBConfig(display, scrn, attr_fbconfig, &num_fbconfigs);

   gl_fbconfig = *fbconfigs;

   /* find the first one with no depth buffer */
   for(i = 0; i < num_fbconfigs; i++) {
      glXGetFBConfigAttrib(display, fbconfigs[i], GLX_DEPTH_SIZE, &value);
      if(value == 0) {
         gl_fbconfig = fbconfigs[i];
         break;
      }
   }

   PrintVisual();
 
   visInfo = glXGetVisualFromFBConfig(display, gl_fbconfig);

   attrib_pbuffer[1] = width;
   attrib_pbuffer[3] = bob ? (height/2) : height;
   gl_pbuffer = glXCreatePbuffer(display, gl_fbconfig, attrib_pbuffer);

   gl_context = glXCreateNewContext(display, gl_fbconfig, GLX_RGBA_TYPE, 
                                    NULL, 1);

   attributes.colormap = XCreateColormap(display, root, visInfo->visual,
                                         AllocNone);

   window = XCreateWindow(display, root, 0, 0, width, height, 0,
                          visInfo->depth, InputOutput,
                          visInfo->visual, CWColormap, &attributes);

   gl_window = glXCreateWindow(display, gl_fbconfig, window, NULL);

   XSelectInput(display, window, KeyPressMask | StructureNotifyMask |
                                 Button1MotionMask | ButtonPressMask);
   XMapWindow(display, window);

   glXMakeContextCurrent(display, gl_window, gl_pbuffer, gl_context);
   glDrawBuffer(GL_BACK);
   glReadBuffer(GL_FRONT_LEFT);

   tex_w =  1 << PowerOfTwo(width);
   tex_h = 1 << PowerOfTwo(bob ? (height/2) : height);

   printf("%i x %i texture\n", tex_w, tex_h);

   glClearColor (0.0, 0.0, 0.0, 0.0);

   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
   glTexImage2D(GL_TEXTURE_2D, 0, 3, tex_w, tex_h,
                0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
   glEnable(GL_TEXTURE_2D);
   glShadeModel(GL_FLAT);

   glViewport(0, 0, width, height);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glFrustum(-1.0, 1.0, -1.0, 1.0, 2, 18.0);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
   glTranslatef(0.0, 0.0, -8);

#ifdef USE_NV_FENCE
   glGenFencesNV(1, &nvFence);
   glSetFenceNV(&nvFence, GL_ALL_COMPLETED_NV);
#endif

   XSync(display, 0);

   uiclp = uiclip+512;
   for (i= -512; i<512; i++)
      uiclp[i] = (i<-128) ? 0 : ((i>127) ? 255 : i+128);

   iclp = iclip+512;
   for (i= -512; i<512; i++)
      iclp[i] = (i<-128) ? -128 : ((i>127) ? 127 : i);

   niclp = niclip+512;
   for (i= -512; i<512; i++)
   niclp[i] = (i<-256) ? -256 : ((i>255) ? 255 : i);

}
示例#7
0
BString* StringManager::_InternalMakeString(bool isScale, const vector2f& fontsize, const Color& clr, const wstring& wstr)
{
    if (!m_isLoad)
    {
        DEBUGLOG("Font not loaded!!\n",1);
        return NULL;
    }
    BCharList list;
    vector< pair<int,int> > LineElements;
    vector2d TexSize;//文本图片的吃村

    //第一步,读取全部的字体,找到最大的BeginY
    {
        int tempWidth = 0;
        int CharCount = 0; //每行文字的个数
        int MaxBeginY = 0; //最大的BeginY
        FT_Matrix scale;
        if( isScale )
        {   //这个缩放是在按像素比例来所放的,
            scale.xx = fontsize.m_x / m_FaceSize.m_x * 0x10000L;
            scale.xy = 0.0;
            scale.yy = fontsize.m_y / m_FaceSize.m_y * 0x10000L;
            scale.yx = 0.0;
        }

        for( int i = 0 ; i < wstr.size() ; )//制造全部的char
        {
            if( L'\n' != wstr[i] && L'#' != wstr[i] )
            {
                if( isScale )
                {
                    FT_Set_Transform( m_FT_Face, &scale, NULL );
                }
                CharCount++;
                BChar* pBChar = _LoadChar(wstr[i]);
                MaxBeginY = MaxBeginY < pBChar->m_nBeginY ? pBChar->m_nBeginY : MaxBeginY;
                tempWidth += pBChar->m_PixelSize.m_x;
                list.push_back(pBChar);
            }
            i++;//自增
            if( L'\n' == wstr[i] || L'#' == wstr[i] || wstr.size() == i )//如果是换行或者文本结束
            {
                TexSize.m_x = TexSize.m_x < tempWidth ? tempWidth : TexSize.m_x;
                LineElements.push_back(make_pair(CharCount, MaxBeginY));//每行文字的个数,这行最大的beginY
                CharCount = 0;
                MaxBeginY = 0;
                tempWidth = 0;
            }
        }

    }
    //第二部,找到每一行最大的高度,高度等于,最大BeginY减去当前字体的BeginY再加上当前字体的高度
    {
        int LineIndex = 0; //行数的索引
        int CharIndex = 0; //每行字的索引
        int maxHeight = 0; //每行最大高度
        for( BCharList::iterator it = list.begin();
                it != list.end();
                it ++)
        {
            CharIndex ++;
            int height = LineElements[LineIndex].second - (*it)->m_nBeginY + (*it)->m_PixelSize.m_y;
            maxHeight = maxHeight < height ? height : maxHeight;
            if( CharIndex == LineElements[LineIndex].first )//如果这行已经完了
            {
                TexSize.m_y = TexSize.m_y < maxHeight ? maxHeight : TexSize.m_y;
                maxHeight = 0;
                CharIndex = 0;
                LineIndex ++;
            }
        }

    }
    //第三步,填充图像了
    vector2d orgiSize;//原始的内用尺寸
    BString* Result;
    int ResultHeight;//行高
    {
        int NewHeight = TexSize.m_y * 1.2;
        ResultHeight = NewHeight;
        int SkyEarth = ( NewHeight - TexSize.m_y ) * 0.5 ;//上下预留0.2做留天留地
        TexSize.m_y = NewHeight;
        int Height = TexSize.m_y;
        TexSize.m_y *= LineElements.size();
        //将TextSize改变之power of two
        orgiSize = TexSize;
        TexSize.m_x = PowerOfTwo( TexSize.m_x );
        TexSize.m_y = PowerOfTwo( TexSize.m_y );
        byte* pBuffer = NEW byte[ TexSize.m_x * TexSize.m_y ];
        int LineIndex = 0; //行数的索引
        int CharIndex = 0; //每行字的索引
        vector2d offset; //读取每一个字体放在新的buffer中的偏移
        memset(pBuffer,0,TexSize.m_x * TexSize.m_y );
        for ( BCharList::iterator it = list.begin();
                it != list.end();
                it++)
        {
            int horBeginPos = LineElements[LineIndex].second - (*it)->m_nBeginY + SkyEarth ; //在图片数组中绘制文本的其实竖直起始位置
            int horEndPos = horBeginPos + (*it)->m_PixelSize.m_y;//在图片数组中绘制文本的其实竖直起结束的位置
            FT_BitmapGlyph bitmap_glyph = (FT_BitmapGlyph)(*it)->m_pGlyph;
            for(int j=0; j  < Height ; j++) //根据图片的宽度,进行中线对其文本,竖着读第一个文字的像素
            {
                if( j < horBeginPos || j >= horEndPos || (*it)->m_isSpace ) //如果不在需要绘制的范围内
                {
                    continue;
                }
                for(int i=0; i < (*it)->m_PixelSize.m_x; i++)
                {
                    int index = (i + offset.m_x) + ( j + offset.m_y ) * TexSize.m_x;
                    //bite alpha = j >= horBeginPos && j < horEndPos && !(*it)->m_isSpace //当在绘制范围之内时,且不是空格的时候
                    //	? bitmap_glyph->bitmap.buffer[ i + (*it)->m_PixelSize.m_x* ( j - horBeginPos ) ] : 0;
                    byte alpha = bitmap_glyph->bitmap.buffer[ i + (*it)->m_PixelSize.m_x* ( j - horBeginPos ) ];
                    pBuffer[index] = alpha;
                }
            }
            offset.m_x += (*it)->m_PixelSize.m_x;//计算x位置的offset
            SAFE_DELETE(*it);//顺便就清理了
            CharIndex++;//读完一个
            if( CharIndex >= LineElements[LineIndex].first ) //如果这行已经读完了
            {
                offset.m_y += Height;
                offset.m_x = 0; //水平的偏移重置
                CharIndex = 0;
                LineIndex ++;
            }
        }
        Result = NEW BString( this, wstr, ResultHeight, fontsize, orgiSize, clr, this->_AllocBuffer() , TexSize, pBuffer);
        SAFE_DELETE_ARRAY(pBuffer);
        m_StringList.push_back(Result);
        Result->SetIterator( (++m_StringList.rbegin()).base() );
    }
    //DEBUGLOG("String size %d, %d\n", TextSize.m_x, TextSize.m_y);
    return Result;
}