Exemple #1
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;
}
Exemple #2
0
psd_status psd_layer_effects_blend_drop_shadow(psd_context * context, psd_layer_record * layer, psd_layer_effects * data)
{
	psd_layer_effects_drop_shadow * drop_shadow = &data->drop_shadow;
	psd_int width, height;
	psd_int angle;
	psd_int distance_x, distance_y;
	psd_bitmap src_bmp, dst_bmp, knock_bmp;
	psd_layer_mask_info layer_mask_info;
	psd_int spread_size, blur_size;
	
	if(drop_shadow->use_global_light == psd_true)
		angle = context->global_angle;
	else
		angle = drop_shadow->angle;
	distance_x = -(psd_int)(drop_shadow->distance * cos(PSD_PI * angle / 180) + 0.5);
	distance_y = (psd_int)(drop_shadow->distance * sin(PSD_PI * angle / 180) + 0.5);
	
	data->left[psd_layer_effects_type_drop_shadow] = -drop_shadow->size + distance_x;
	data->top[psd_layer_effects_type_drop_shadow] = -drop_shadow->size + distance_y;
	width = layer->width + drop_shadow->size * 2;
	height = layer->height + drop_shadow->size * 2;
	data->right[psd_layer_effects_type_drop_shadow] = data->left[psd_layer_effects_type_drop_shadow] + width;
	data->bottom[psd_layer_effects_type_drop_shadow] = data->top[psd_layer_effects_type_drop_shadow] + height;
	data->blend_mode[psd_layer_effects_type_drop_shadow] = drop_shadow->blend_mode;
	data->opacity[psd_layer_effects_type_drop_shadow] = drop_shadow->opacity;

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

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

	if(drop_shadow->size == 0)
	{
		psd_fill_bitmap(&dst_bmp, drop_shadow->color);
		psd_bitmap_copy_alpha_channel(&dst_bmp, &src_bmp);
	}
	else
	{
		psd_inflate_bitmap(&dst_bmp, &src_bmp, drop_shadow->size, drop_shadow->size);
		psd_fill_bitmap_without_alpha_channel(&dst_bmp, drop_shadow->color);
	}

	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 - drop_shadow->size;
		layer_mask_info.top -= layer->top - drop_shadow->size;
		layer_mask_info.right -= layer->left - drop_shadow->size;
		layer_mask_info.bottom -= layer->top - drop_shadow->size;
		psd_bitmap_blend_mask(&dst_bmp, &layer_mask_info);
	}

	if(drop_shadow->knocks_out == psd_true)
	{
		psd_create_bitmap(&knock_bmp, width, height);
		psd_copy_bitmap(&knock_bmp, &dst_bmp);
		psd_offset_bitmap(&knock_bmp, distance_x, distance_y, psd_color_clear);
	}
	
	spread_size = (drop_shadow->spread * drop_shadow->size + 50) / 100;
	blur_size = drop_shadow->size - spread_size;
	if(spread_size != 0)
	{
		psd_bitmap_gaussian_blur_alpha_channel(&dst_bmp, spread_size);
		psd_bitmap_find_edge(&dst_bmp, psd_true);
	}
	if(blur_size != 0)
	{
		psd_bitmap_gaussian_blur_alpha_channel(&dst_bmp, blur_size);
	}

	psd_bitmap_contour_alpha_channel(&dst_bmp, drop_shadow->contour_lookup_table, 
		drop_shadow->anti_aliased, psd_true);

	if(drop_shadow->noise > 0)
	{
		psd_effects_add_noise(&dst_bmp, drop_shadow->noise, 
			data->left[psd_layer_effects_type_drop_shadow] + layer->left,
			data->top[psd_layer_effects_type_drop_shadow] + layer->top, context);
	}

	if(drop_shadow->knocks_out == psd_true)
	{
		psd_bitmap_knock_out(&dst_bmp, &knock_bmp);
		psd_free_bitmap(&knock_bmp);
	}
	
	data->valid[psd_layer_effects_type_drop_shadow] = psd_false;
	
	return psd_status_done;
}
Exemple #3
0
psd_status psd_layer_effects_blend_inner_glow(psd_context * context, psd_layer_record * layer, psd_layer_effects * data)
{
	psd_layer_effects_inner_glow * inner_glow = &data->inner_glow;
	psd_int width, height;
	psd_bitmap src_bmp, dst_bmp, knock_bmp;
	psd_layer_mask_info layer_mask_info;
	psd_int choke_size, blur_size;
	psd_argb_color gradient_table[256];
	
	data->left[psd_layer_effects_type_inner_glow] = -inner_glow->size;
	data->top[psd_layer_effects_type_inner_glow] = -inner_glow->size;
	width = layer->width + inner_glow->size * 2;
	height = layer->height + inner_glow->size * 2;
	data->right[psd_layer_effects_type_inner_glow] = data->left[psd_layer_effects_type_inner_glow] + width;
	data->bottom[psd_layer_effects_type_inner_glow] = data->top[psd_layer_effects_type_inner_glow] + height;
	data->blend_mode[psd_layer_effects_type_inner_glow] = inner_glow->blend_mode;
	data->opacity[psd_layer_effects_type_inner_glow] = inner_glow->opacity;

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

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

	if(inner_glow->size == 0)
	{
		psd_fill_bitmap(&dst_bmp, inner_glow->color);
		psd_bitmap_copy_alpha_channel(&dst_bmp, &src_bmp);
	}
	else
	{
		psd_inflate_bitmap(&dst_bmp, &src_bmp, inner_glow->size, inner_glow->size);
		psd_fill_bitmap_without_alpha_channel(&dst_bmp, inner_glow->color);
	}

	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 - inner_glow->size;
		layer_mask_info.top -= layer->top - inner_glow->size;
		layer_mask_info.right -= layer->left - inner_glow->size;
		layer_mask_info.bottom -= layer->top - inner_glow->size;
		psd_bitmap_blend_mask(&dst_bmp, &layer_mask_info);
	}

	psd_bitmap_reverse_alpha_channel(&dst_bmp);

	psd_create_bitmap(&knock_bmp, width, height);
	psd_copy_bitmap(&knock_bmp, &dst_bmp);

	if(inner_glow->technique == psd_technique_precise)
		psd_bitmap_find_edge(&dst_bmp, psd_false);
	
	choke_size = (inner_glow->choke * inner_glow->size + 50) / 100;
	blur_size = inner_glow->size - choke_size;
	if(choke_size != 0)
	{
		psd_bitmap_gaussian_blur_alpha_channel(&dst_bmp, choke_size);
		psd_bitmap_find_edge(&dst_bmp, psd_false);
	}
	if(blur_size != 0)
	{
		psd_bitmap_gaussian_blur_alpha_channel(&dst_bmp, blur_size);
	}

	psd_bitmap_ajust_range(&dst_bmp, inner_glow->range);

	if(inner_glow->source == psd_glow_center)
		psd_bitmap_reverse_alpha_channel(&dst_bmp);

	psd_bitmap_contour_alpha_channel(&dst_bmp, inner_glow->contour_lookup_table, 
		inner_glow->anti_aliased, psd_false);

	if(inner_glow->noise > 0)
	{
		psd_effects_add_noise(&dst_bmp, inner_glow->noise, 
			data->left[psd_layer_effects_type_inner_glow] + layer->left,
			data->top[psd_layer_effects_type_inner_glow] + layer->top, context);
	}

	if(inner_glow->fill_type == psd_fill_gradient)
	{
		psd_gradient_color_get_table(&inner_glow->gradient_color, gradient_table, 256, psd_false);
		psd_effects_apply_gradient(&dst_bmp, gradient_table, psd_false, inner_glow->jitter, 
			data->left[psd_layer_effects_type_inner_glow] + layer->left,
			data->top[psd_layer_effects_type_inner_glow] + layer->top, context);
	}

	psd_bitmap_knock_out(&dst_bmp, &knock_bmp);
	psd_free_bitmap(&knock_bmp);
	
	data->valid[psd_layer_effects_type_inner_glow] = psd_false;
	
	return psd_status_done;
}