Пример #1
0
psd_status psd_layer_effects_blend_gradient_overlay(psd_context * context, psd_layer_record * layer, psd_layer_effects * data)
{
	psd_layer_effects_gradient_overlay * gradient_overlay = &data->gradient_overlay;
	psd_bitmap src_bmp, dst_bmp;
	psd_layer_mask_info layer_mask_info;
	psd_int width, height, center_x, center_y, radius_x, radius_y, radius_corner;
	psd_int corner_angle, angle;
	psd_int sign_x = 1, sign_y = 1;

	data->left[psd_layer_effects_type_gradient_overlay] = 0;
	data->top[psd_layer_effects_type_gradient_overlay] = 0;
	data->right[psd_layer_effects_type_gradient_overlay] = layer->width;
	data->bottom[psd_layer_effects_type_gradient_overlay] = layer->height;
	data->blend_mode[psd_layer_effects_type_gradient_overlay] = gradient_overlay->blend_mode;
	data->opacity[psd_layer_effects_type_gradient_overlay] = gradient_overlay->opacity;
	data->width[psd_layer_effects_type_gradient_overlay] = layer->width;
	data->height[psd_layer_effects_type_gradient_overlay] = layer->height;

	if(data->image_data[psd_layer_effects_type_gradient_overlay] == NULL)
	{
		data->image_data[psd_layer_effects_type_gradient_overlay] = (psd_argb_color *)psd_malloc(layer->width * layer->height * 4);
		if(data->image_data[psd_layer_effects_type_gradient_overlay] == NULL)
			return psd_status_malloc_failed;
	}

	src_bmp.width = layer->width;
	src_bmp.height = layer->height;
	src_bmp.image_data = layer->image_data;
	dst_bmp.width = layer->width;
	dst_bmp.height = layer->height;
	dst_bmp.image_data = data->image_data[psd_layer_effects_type_gradient_overlay];

	if(gradient_overlay->align_width_layer == psd_false)
	{
		center_x = context->width / 2 + gradient_overlay->horz_offset * context->width / 100;
		center_y = context->height / 2 + gradient_overlay->vert_offset * context->height / 100;
		center_x -= layer->left;
		center_y -= layer->top;
		corner_angle = (psd_int)(atan((psd_float)context->height / context->width) * 180 / PSD_PI + 0.5);
		angle = gradient_overlay->angle;
		width = (context->width * gradient_overlay->scale + 100) / 200;
		height = (context->height * gradient_overlay->scale + 100) / 200;
	}
	else
	{
		center_x = layer->width / 2 + gradient_overlay->horz_offset * layer->width / 100;
		center_y = layer->height / 2 + gradient_overlay->vert_offset * layer->height / 100;
		corner_angle = (psd_int)(atan((psd_float)layer->height / layer->width) * 180 / PSD_PI + 0.5);
		angle = gradient_overlay->angle;
		width = (layer->width * gradient_overlay->scale + 100) / 200;
		height = (layer->height * gradient_overlay->scale + 100) / 200;
	}
	if(angle < 0)
		angle += 360;
	if(angle >= 90 && angle < 180)
	{
		angle = 180 - angle;
		sign_x = -1;
	}
	else if(angle >= 180 && angle < 270)
	{
		angle = angle - 180;
		sign_x = -1;
		sign_y = -1;
	}
	else if(angle >= 270 && angle <= 360)
	{
		angle = 360 - angle;
		sign_y = -1;
	}
	if(angle <= corner_angle)
	{
		radius_x = width;
		radius_y = (psd_int)(radius_x * PSD_TAN(angle) + 0.5);
	}
	else
	{
		radius_y = height;
		radius_x = (psd_int)(radius_y / PSD_TAN(angle) + 0.5);
	}
	radius_corner = (psd_int)(psd_carm_sqrt((psd_float)(radius_x * radius_x + radius_y * radius_y)) + 0.5);

	switch(gradient_overlay->style)
	{
		case psd_gradient_style_linear:
			psd_gradient_fill_linear(&dst_bmp, &gradient_overlay->gradient_color, gradient_overlay->reverse, 
				center_x - sign_x * radius_x, center_y + sign_y * radius_y, 
				center_x + sign_x * radius_x, center_y - sign_y * radius_y);
			break;
		case psd_gradient_style_radial:
			psd_gradient_fill_radial(&dst_bmp, &gradient_overlay->gradient_color, gradient_overlay->reverse, 
				center_x, center_y, radius_corner);
			break;
		case psd_gradient_style_angle:
			psd_gradient_fill_angle(&dst_bmp, &gradient_overlay->gradient_color, gradient_overlay->reverse, 
				center_x, center_y, gradient_overlay->angle);
			break;
		case psd_gradient_style_reflected:
			psd_gradient_fill_reflected(&dst_bmp, &gradient_overlay->gradient_color, gradient_overlay->reverse, 
				center_x - sign_x * radius_x, center_y + sign_y * radius_y, 
				center_x + sign_x * radius_x, center_y - sign_y * radius_y);
			break;
		case psd_gradient_style_diamond:
			psd_gradient_fill_diamond(&dst_bmp, &gradient_overlay->gradient_color, gradient_overlay->reverse, 
				center_x, center_y, radius_corner, gradient_overlay->angle);
			break;
		default:
			psd_assert(0);
			break;
	}
	
	psd_bitmap_mix_alpha_channel(&dst_bmp, &src_bmp);
	memcpy(&layer_mask_info, &layer->layer_mask_info, sizeof(psd_layer_mask_info));
	if(layer_mask_info.disabled == psd_false && (layer_mask_info.default_color != 255 || layer_mask_info.mask_data != NULL))
	{
		layer_mask_info.left -= layer->left;
		layer_mask_info.top -= layer->top;
		layer_mask_info.right -= layer->left;
		layer_mask_info.bottom -= layer->top;
		psd_bitmap_blend_mask(&dst_bmp, &layer_mask_info);
	}

	data->valid[psd_layer_effects_type_gradient_overlay] = psd_false;
	
	return psd_status_done;
}
Пример #2
0
psd_status psd_layer_effects_blend_stroke(psd_context * context, psd_layer_record * layer, psd_layer_effects * data)
{
	psd_layer_effects_stroke * stroke = &data->stroke;
	psd_int i, width, height, center_x, center_y, radius_x, radius_y, radius_corner;
	psd_int corner_angle, angle;
	psd_int sign_x = 1, sign_y = 1;
	psd_bitmap layer_bmp, src_bmp, dst_bmp;
	psd_layer_mask_info layer_mask_info;

	width = layer->width + stroke->size * 2;
	height = layer->height + stroke->size * 2;
	data->left[psd_layer_effects_type_stroke] = -stroke->size;
	data->top[psd_layer_effects_type_stroke] = -stroke->size;
	data->right[psd_layer_effects_type_stroke] = layer->width + stroke->size;
	data->bottom[psd_layer_effects_type_stroke] = layer->height + stroke->size;
	data->blend_mode[psd_layer_effects_type_stroke] = stroke->blend_mode;
	data->opacity[psd_layer_effects_type_stroke] = stroke->opacity;

	if(data->image_data[psd_layer_effects_type_stroke] != NULL)
	{
		if(data->width[psd_layer_effects_type_stroke] != width ||
			data->height[psd_layer_effects_type_stroke] != height)
		{
			psd_free(data->image_data[psd_layer_effects_type_stroke]);
			data->image_data[psd_layer_effects_type_stroke] = (psd_argb_color *)psd_malloc(width * height * 4);
			if(data->image_data[psd_layer_effects_type_stroke] == NULL)
				return psd_status_malloc_failed;
		}
	}
	else
	{
		data->image_data[psd_layer_effects_type_stroke] = (psd_argb_color *)psd_malloc(width * height * 4);
		if(data->image_data[psd_layer_effects_type_stroke] == NULL)
			return psd_status_malloc_failed;
	}
	data->width[psd_layer_effects_type_stroke] = width;
	data->height[psd_layer_effects_type_stroke] = height;

	layer_bmp.width = layer->width;
	layer_bmp.height = layer->height;
	layer_bmp.image_data = layer->image_data;
	psd_create_bitmap(&src_bmp, width, height);
	psd_inflate_bitmap(&src_bmp, &layer_bmp, stroke->size, stroke->size);
	dst_bmp.width = width;
	dst_bmp.height = height;
	dst_bmp.image_data = data->image_data[psd_layer_effects_type_stroke];

	psd_fill_bitmap(&dst_bmp, psd_color_clear);
	switch(stroke->position)
	{
		case psd_stroke_outside:
			psd_draw_stroke(&dst_bmp, &src_bmp, stroke->size * 2);
			psd_bitmap_find_edge(&src_bmp, psd_true);
			psd_bitmap_knock_out(&dst_bmp, &src_bmp);
			break;
		case psd_stroke_inside:
			psd_draw_stroke(&dst_bmp, &src_bmp, stroke->size * 2);
			psd_bitmap_find_edge(&src_bmp, psd_true);
			psd_bitmap_reverse_alpha_channel(&src_bmp);
			psd_bitmap_knock_out(&dst_bmp, &src_bmp);
			break;
		case psd_stroke_center:
			psd_draw_stroke(&dst_bmp, &src_bmp, stroke->size);
			break;
		default:
			psd_assert(0);
			break;
	}

	psd_inflate_bitmap(&src_bmp, &layer_bmp, stroke->size, stroke->size);
	memcpy(&layer_mask_info, &layer->layer_mask_info, sizeof(psd_layer_mask_info));
	if(layer_mask_info.disabled == psd_false && (layer_mask_info.default_color != 255 || layer_mask_info.mask_data != NULL))
	{
		layer_mask_info.left -= layer->left - stroke->size;
		layer_mask_info.top -= layer->top - stroke->size;
		layer_mask_info.right -= layer->left - stroke->size;
		layer_mask_info.bottom -= layer->top - stroke->size;
		psd_bitmap_blend_mask(&src_bmp, &layer_mask_info);
	}
	psd_bitmap_reverse_mixed_alpha_channel(&src_bmp);
	psd_bitmap_blend_alpha_channel(&dst_bmp, &src_bmp);

	switch(stroke->fill_type)
	{
		case psd_fill_solid_color:
			psd_fill_bitmap_without_alpha_channel(&dst_bmp, stroke->fill_color);
			break;
			
		case psd_fill_gradient:
			if(stroke->gradient_align == psd_false)
			{
				center_x = context->width / 2 + stroke->gradient_horz_offset * context->width / 100;
				center_y = context->height / 2 + stroke->gradient_vert_offset * context->height / 100;
				center_x -= layer->left;
				center_y -= layer->top;
				corner_angle = (psd_int)(atan((psd_float)context->height / context->width) * 180 / PSD_PI + 0.5);
				angle = stroke->gradient_angle;
				width = (context->width * stroke->gradient_scale + 100) / 200;
				height = (context->height * stroke->gradient_scale + 100) / 200;
			}
			else
			{
				center_x = width / 2 + stroke->gradient_horz_offset * layer->width / 100;
				center_y = height / 2 + stroke->gradient_vert_offset * layer->height / 100;
				switch(stroke->position)
				{
					case psd_stroke_outside:
						width = layer->width + stroke->size * 2;
						height = layer->height + stroke->size * 2;
						break;
					case psd_stroke_inside:
						width = layer->width;
						height = layer->height;
						break;
					case psd_stroke_center:
						width = layer->width + stroke->size;
						height = layer->height + stroke->size;
						break;
				}
				center_x += stroke->gradient_horz_offset * width / 100;
				center_y += stroke->gradient_vert_offset * height / 100;
				corner_angle = (psd_int)(atan((psd_float)height / width) * 180 / PSD_PI + 0.5);
				angle = stroke->gradient_angle;
				width = (width * stroke->gradient_scale + 100) / 200;
				height = (height * stroke->gradient_scale + 100) / 200;
			}
			if(angle < 0)
				angle += 360;
			if(angle >= 90 && angle < 180)
			{
				angle = 180 - angle;
				sign_x = -1;
			}
			else if(angle >= 180 && angle < 270)
			{
				angle = angle - 180;
				sign_x = -1;
				sign_y = -1;
			}
			else if(angle >= 270 && angle <= 360)
			{
				angle = 360 - angle;
				sign_y = -1;
			}
			if(angle <= corner_angle)
			{
				radius_x = width;
				radius_y = (psd_int)(radius_x * PSD_TAN(angle) + 0.5);
			}
			else
			{
				radius_y = height;
				radius_x = (psd_int)(radius_y / PSD_TAN(angle) + 0.5);
			}
			radius_corner = (psd_int)(psd_carm_sqrt((psd_float)(radius_x * radius_x + radius_y * radius_y)) + 0.5);

			switch(stroke->gradient_style)
			{
				case psd_gradient_style_linear:
					psd_gradient_fill_linear(&src_bmp, &stroke->gradient_color, stroke->gradient_reverse, 
						center_x - sign_x * radius_x, center_y + sign_y * radius_y, 
						center_x + sign_x * radius_x, center_y - sign_y * radius_y);
					break;
				case psd_gradient_style_radial:
					psd_gradient_fill_radial(&src_bmp, &stroke->gradient_color, stroke->gradient_reverse, 
						center_x, center_y, radius_corner);
					break;
				case psd_gradient_style_angle:
					psd_gradient_fill_angle(&src_bmp, &stroke->gradient_color, stroke->gradient_reverse, 
						center_x, center_y, stroke->gradient_angle);
					break;
				case psd_gradient_style_reflected:
					psd_gradient_fill_reflected(&src_bmp, &stroke->gradient_color, stroke->gradient_reverse, 
						center_x - sign_x * radius_x, center_y + sign_y * radius_y, 
						center_x + sign_x * radius_x, center_y - sign_y * radius_y);
					break;
				case psd_gradient_style_diamond:
					psd_gradient_fill_diamond(&src_bmp, &stroke->gradient_color, stroke->gradient_reverse, 
						center_x, center_y, radius_corner, stroke->gradient_angle);
					break;
				default:
					psd_assert(0);
					break;
			}

			psd_bitmap_copy_without_alpha_channel(&dst_bmp, &src_bmp);
			break;
			
		case psd_fill_pattern:
			for(i = 0; i < context->pattern_count; i ++)
			{
				if(strcmp(context->patterns[i].unique_id, stroke->pattern_info.identifier) == 0)
				{
					psd_pattern_fill(&src_bmp, &context->patterns[i], stroke->pattern_scale, 
						stroke->pattern_horz_phase + stroke->size, 
						stroke->pattern_vert_phase + stroke->size);
					break;
				}
			}
			
			psd_bitmap_copy_without_alpha_channel(&dst_bmp, &src_bmp);
			break;
			
		default:
			psd_assert(0);
			break;
	}

	psd_free_bitmap(&src_bmp);

	data->valid[psd_layer_effects_type_stroke] = psd_false;
	
	return psd_status_done;
}