Exemple #1
0
// MW-2011-09-13: [[ Masks ]] Updated to store data in an MCWindowMask struct.
MCWindowShape *MCImage::makewindowshape(void)
{	
	bool t_success = true;
	
	MCWindowShape *t_mask = nil;
	CGImageRef t_mask_image = nil;
	MCImageBitmap *t_bitmap = nil;
	uint8_t *t_alpha = nil;
	uindex_t t_alpha_stride = 0;
	uindex_t t_width, t_height;
	
	t_success = lockbitmap(t_bitmap, true);
	
	if (t_success)
		t_success = MCImageBitmapHasTransparency(t_bitmap);
	
	if (t_success)
	{
		t_width = t_bitmap->width;
		t_height = t_bitmap->height;
		
		t_alpha_stride = (t_width + 3) & ~3;
		t_success = MCMemoryAllocate(t_alpha_stride * t_height, t_alpha);
	}
	
	if (t_success)
	{
		surface_extract_alpha(t_bitmap->data, t_bitmap->stride, t_alpha, t_alpha_stride, t_width, t_height);
		
		t_success = MCAlphaToCGImage(t_width, t_height, t_alpha, t_alpha_stride, t_mask_image);
	}
	
	if (t_success)
		t_success = MCMemoryNew(t_mask);
	
	unlockbitmap(t_bitmap);
	
	
	if (!t_success)
	{
		CGImageRelease(t_mask_image);
		MCMemoryDeallocate(t_mask);
		MCMemoryDeallocate(t_alpha);
		
		return nil;
	}
	
	t_mask->width = t_width;
	t_mask->height = t_height;
	t_mask->is_sharp = false;
	
	t_mask->data = (char*)t_alpha;
	t_mask->stride = t_alpha_stride;
	
	t_mask->handle = t_mask_image;
	
	return t_mask;
}
Exemple #2
0
void MCTileCacheSoftwareCompositor_Cleanup(void *p_context)
{
	MCTileCacheSoftwareCompositorContext *self;
	self = (MCTileCacheSoftwareCompositorContext *)p_context;
	MCMemoryDeallocate(self -> tile_row);
	MCMemoryDelete(self);
}
Exemple #3
0
int MCR_exec(regexp *prog, MCStringRef string, MCRange p_range)
{
    // AL-2014-06-25: [[ Bug 12676 ]] Ensure string is not unnativized by MCR_exec
    int status;
	int flags = 0;
    
    if (MCStringIsNative(string))
    {
        uindex_t t_length;
        unichar_t *t_string_chars;
        t_string_chars = nil;
        
        MCMemoryAllocate(MCStringGetLength(string) * sizeof(unichar_t), t_string_chars);
        t_length = MCStringGetChars(string, p_range, t_string_chars);
        status = regexec(&prog->rexp, t_string_chars, t_length, NSUBEXP, prog->matchinfo, flags);
        MCMemoryDeallocate(t_string_chars);
    }
    else
        status = regexec(&prog->rexp, MCStringGetCharPtr(string) + p_range . offset, p_range . length, NSUBEXP, prog->matchinfo, flags);

	if (status != REG_OKAY)
	{
		if (status == REG_NOMATCH)
		{
			return (0);
		}
		//MCValueRelease(regexperror);
		regerror(status, NULL, regexperror);
		return(0);
	}
	return (1);
}
Exemple #4
0
MCStreamCache::~MCStreamCache()
{
	if (m_cache_file)
		MCS_close(m_cache_file);
	if (m_cache_buffer)
		MCMemoryDeallocate(m_cache_buffer);
}
Exemple #5
0
bool MCServerStopSession()
{
	bool t_success = true;
	
	if (s_current_session == NULL)
		return true;
	
	char *t_data = NULL;
	uint32_t t_data_length = 0;
	
	MCVariable *t_session_var;
	t_session_var = MCVariable::lookupglobal_cstring("$_SESSION");

	if (t_session_var != NULL)
		t_success = t_session_var->getvalue().encode((void*&)t_data, t_data_length);

	if (t_success)
	{
		MCMemoryDeallocate(s_current_session->data);
		s_current_session->data = t_data;
		s_current_session->data_length = t_data_length;
		
		t_success = MCSessionCommit(s_current_session);
		s_current_session = NULL;
	}

	return t_success;
}
MCReferencedImageRep::~MCReferencedImageRep()
{
	MCCStringFree(m_file_name);
	MCCStringFree(m_search_key);
	
	MCMemoryDeallocate(m_url_data);
}
MCReferencedImageRep::~MCReferencedImageRep()
{
	MCValueRelease(m_file_name);
	MCValueRelease(m_search_key);

	MCMemoryDeallocate(m_url_data);
}
Exemple #8
0
void MCSessionDisposeSession(MCSession *p_session)
{
	if (p_session == NULL)
		return;
	
	MCCStringFree(p_session->id);
	MCCStringFree(p_session->ip);
	MCCStringFree(p_session->filename);

	MCMemoryDeallocate(p_session->data);
	
	MCMemoryDelete(p_session);
}
static bool WindowsGetModuleDescription(MCStringRef p_path, MCStringRef& r_description)
{
	bool t_success;
	t_success = true;

    MCAutoStringRefAsWString t_path;
    /* UNCHECKED */ t_path.Lock(p_path);
    
	DWORD t_size;
	t_size = 0;
	if (t_success)
	{
		t_size = GetFileVersionInfoSizeW(*t_path, nil);
		if (t_size == 0)
			t_success = false;
	}

	void *t_data;
	t_data = nil;
	if (t_success)
		t_success = MCMemoryAllocate(t_size, t_data);

	if (t_success &&
		!GetFileVersionInfoW(*t_path, 0, t_size, t_data))
		t_success = false;

	UINT t_desc_length;
	WCHAR *t_desc;
	t_desc_length = 0;
	t_desc = nil;
	if (t_success &&
		!VerQueryValueW(t_data, L"\\StringFileInfo\\040904b0\\FileDescription", (void **)&t_desc, &t_desc_length))
		t_success = false;

    if (t_success)
        t_success = MCStringCreateWithWString(t_desc, r_description);
    
	MCMemoryDeallocate(t_data);
	
    // Make sure a description gets set
    if (!t_success)
        r_description = MCValueRetain(kMCEmptyString);
    
	return t_success;
}
Exemple #10
0
bool MCTileCacheCoreGraphicsCompositor_AllocateTile(void *p_context, int32_t p_size, const void *p_bits, uint32_t p_stride, void*& r_tile)
{
	MCTileCacheCoreGraphicsCompositorContext *self;
	self = (MCTileCacheCoreGraphicsCompositorContext *)p_context;
	
	// If the stride is exactly one tile wide, we don't need a copy.
	void *t_data;
	t_data = nil;
	if (p_stride == p_size * sizeof(uint32_t))
		t_data = (void *)p_bits;
	else if (MCMemoryAllocate(p_size * p_size * sizeof(uint32_t), t_data))
	{
		// Copy across each scanline of the tile into the buffer.
		for(int32_t y = 0; y < p_size; y++)
			memcpy((uint8_t *)t_data + y * p_size * sizeof(uint32_t), (uint8_t *)p_bits + p_stride * y, p_size * sizeof(uint32_t));
	}
	
	CGImageRef t_tile;
	t_tile = nil;
	if (t_data != nil)
	{
		// IM-2013-08-21: [[ RefactorGraphics ]] Refactor CGImage creation code to be pixel-format independent
		CGBitmapInfo t_bm_info;
		t_bm_info = MCGPixelFormatToCGBitmapInfo(kMCGPixelFormatNative, true);
		
		CGContextRef t_cgcontext;
		t_cgcontext = CGBitmapContextCreate((void *)t_data, p_size, p_size, 8, p_size * sizeof(uint32_t), self -> colorspace, t_bm_info);
		if (t_cgcontext != nil)
		{
			t_tile = CGBitmapContextCreateImage(t_cgcontext);
			CGContextRelease(t_cgcontext);
		}
	}
	
	if (t_data != p_bits)
		MCMemoryDeallocate(t_data);
		
	if (t_tile == nil)
		return false;

	r_tile = t_tile;
	
	return true;
}
Exemple #11
0
bool read_binary(MCSystemFileHandle *p_file, void *&r_data, uint32_t &r_length)
{
	if (!read_uint32(p_file, r_length))
		return false;
	
	uint32_t t_read;
	void *t_data = NULL;
	
	if (!MCMemoryAllocate(r_length, t_data))
		return false;
	
	if (p_file->Read(t_data, r_length, t_read) && t_read == r_length)
	{
		r_data = t_data;
		return true;
	}
	else
	{
		MCMemoryDeallocate(t_data);
		return false;
	}
}
Exemple #12
0
bool read_all(IO_handle p_stream, uint8_t *&r_data, uindex_t &r_data_size)
{
	bool t_success = true;

	uint8_t *t_buffer = nil;
	uindex_t t_size = 0;

	t_size = MCS_fsize(p_stream) - MCS_tell(p_stream);

	t_success = MCMemoryAllocate(t_size, t_buffer);
	if (t_success)
		t_success = IO_NORMAL == MCS_readfixed(t_buffer, t_size, p_stream);

	if (t_success)
	{
		r_data = t_buffer;
		r_data_size = t_size;
	}
	else
		MCMemoryDeallocate(t_buffer);

	return t_success;
}
Exemple #13
0
bool MCStreamCache::AppendToCache(void *p_buffer, uint32_t p_length, uint32_t &r_written)
{
	bool t_success = true;
	
	if (m_cache_length + p_length > m_buffer_limit)
	{
		if (m_cache_file == NULL)
		{
			t_success = MCMultiPartCreateTempFile(cgi_get_upload_temp_dir(), m_cache_file, m_cache_filename);
			if (t_success && m_cache_buffer != NULL)
				t_success = (IO_NORMAL == MCS_write(m_cache_buffer, 1, m_cache_length, m_cache_file));
			
			MCMemoryDeallocate(m_cache_buffer);
			m_cache_buffer = NULL;
		}
		
		if (t_success)
			t_success = (IO_NORMAL == MCS_write(p_buffer, 1, p_length, m_cache_file));
		
		m_cache_length += p_length;
		r_written = p_length;
	}
	else
	{
		if (m_cache_buffer == NULL)
			t_success = MCMemoryAllocate(m_buffer_limit, m_cache_buffer);
		if (t_success)
		{
			MCMemoryCopy((uint8_t*)m_cache_buffer + m_cache_length, p_buffer, p_length);
			m_cache_length += p_length;
			
			r_written = p_length;
		}
	}
		
	return t_success;
}
Exemple #14
0
bool MCTileCacheSoftwareCompositor_BeginFrame(void *p_context, MCStackSurface *p_surface, MCRegionRef p_dirty)
{
	MCTileCacheSoftwareCompositorContext *self;
	self = (MCTileCacheSoftwareCompositorContext *)p_context;
	
	MCGRaster t_raster;
	if (!p_surface -> LockPixels(p_dirty, t_raster))
		return false;
	
	self -> bits = t_raster . pixels;
	self -> stride = t_raster . stride;

	MCMemoryDeallocate(self -> tile_row);
	self -> tile_row = nil;
	self -> tile_row_color = 0;
	
	self -> tile_size = MCTileCacheGetTileSize(self -> tilecache);
	self -> dirty = MCRegionGetBoundingBox(p_dirty);
	self -> clip = self -> dirty;
	self -> combiner = s_surface_combiners_nda[GXcopy];
	self -> opacity = 255;
	
	return true;
}
Exemple #15
0
bool read_cstring(MCSystemFileHandle *p_file, char *&r_string)
{
	uint32_t t_strlen;
	if (!read_uint32(p_file, t_strlen))
		return false;
	
	uint32_t t_read;
	char *t_str = NULL;
	
	if (!MCMemoryAllocate(t_strlen + 1, t_str))
		return false;
	
	if (p_file->Read(t_str, t_strlen, t_read) && t_read == t_strlen)
	{
		t_str[t_strlen] = '\0';
		r_string = t_str;
		return true;
	}
	else
	{
		MCMemoryDeallocate(t_str);
		return false;
	}
}
Exemple #16
0
bool MCStreamCache::Ensure(uint32_t p_offset)
{
	if (p_offset <= m_cache_length)
		return true;
	
	bool t_success = true;
	
	void *t_buffer;
	
	t_success = MCMemoryAllocate(m_buffer_limit, t_buffer);
	
	while (t_success && p_offset > m_cache_length)
	{
		uint32_t t_to_read;
		uint32_t t_read;
		
		t_to_read = MCMin(p_offset - m_cache_length, m_buffer_limit);
		t_success = Read(t_buffer, m_cache_length, t_to_read, t_read) && (t_read == t_to_read);
	}
	
	MCMemoryDeallocate(t_buffer);
	
	return t_success;
}
Exemple #17
0
static bool cgi_native_from_encoding(MCSOutputTextEncoding p_encoding, const char *p_text, uint32_t p_text_length, char *&r_native, uint32_t &r_native_length)
{
	bool t_success = true;

	uint8_t *t_native = NULL;
	uint32_t t_native_length = 0;

	if (p_encoding == kMCSOutputTextEncodingUTF8)
	{
		int32_t t_unicode_length;
		t_unicode_length = UTF8ToUnicode(p_text, p_text_length, NULL, 0);
		
		uint16_t *t_unicode = NULL;
		t_success = MCMemoryAllocate(t_unicode_length, t_unicode);
		if (t_success)
		{
			UTF8ToUnicode(p_text, p_text_length, t_unicode, t_unicode_length);
			t_success = MCConvertNativeFromUTF16(t_unicode, t_unicode_length / 2, t_native, t_native_length);
		}
		MCMemoryDeallocate(t_unicode);
	}
	else if (p_encoding == kMCSOutputTextEncodingWindows1252)
		t_success = MCConvertNativeFromWindows1252((uint8_t*)p_text, p_text_length, t_native, t_native_length);
	else if (p_encoding == kMCSOutputTextEncodingMacRoman)
		t_success = MCConvertNativeFromMacRoman((uint8_t*)p_text, p_text_length, t_native, t_native_length);
	else if (p_encoding == kMCSOutputTextEncodingISO8859_1)
		t_success = MCConvertNativeFromISO8859_1((uint8_t*)p_text, p_text_length, t_native, t_native_length);

	if (t_success)
	{
		r_native = (char*)t_native;
		r_native_length = t_native_length;
	}

	return t_success;
}
MCResidentImageRep::~MCResidentImageRep()
{
	MCMemoryDeallocate(m_data);
}
Exemple #19
0
void MCTileCacheSoftwareCompositor_DeallocateTile(void *p_context, void *p_tile)
{
	MCMemoryDeallocate(p_tile);
}
Exemple #20
0
bool MCCrypt_rsa_op(bool p_encrypt, RSA_KEYTYPE p_key_type, const char *p_message_in, uint32_t p_message_in_length,
			const char *p_key, uint32_t p_key_length, const char *p_passphrase,
			char *&r_message_out, uint32_t &r_message_out_length, char *&r_result, uint32_t &r_error)
{
	bool t_success = true;
	EVP_PKEY *t_key = NULL;
	RSA *t_rsa = NULL;
	int32_t t_rsa_size;
	uint8_t *t_output_buffer = NULL;
	int32_t t_output_length;

	if (!InitSSLCrypt())
	{
		t_success = false;
		MCCStringClone("error: ssl library initialization failed", r_result);
	}

	if (t_success)
	{
		if (!load_pem_key(p_key, p_key_length, p_key_type, p_passphrase, t_key))
		{
			t_success = false;
			MCCStringClone("error: invalid key", r_result);
		}
	}

	if (t_success)
	{
		t_rsa = EVP_PKEY_get1_RSA(t_key);
		if (t_rsa == NULL)
		{
			t_success = false;
			MCCStringClone("error: not an RSA key", r_result);
		}
	}

	if (t_success)
	{
		t_rsa_size = RSA_size(t_rsa);
		if (!MCMemoryAllocate(t_rsa_size, t_output_buffer))
		{
			t_success = false;
			r_error = EE_NO_MEMORY;
		}
	}
	int (*t_rsa_func)(int, const unsigned char*, unsigned char*, RSA*, int) = NULL;
	if (t_success)
	{
		if (p_encrypt)
		{
			if (p_key_type == RSAKEY_PRIVKEY)
				t_rsa_func = RSA_private_encrypt;
			else
				t_rsa_func = RSA_public_encrypt;
			if (p_message_in_length >= unsigned(t_rsa_size - 11))
			{
				t_success = false;
				MCCStringClone("error: message too large", r_result);
			}
		}
		else
		{
			if (p_key_type == RSAKEY_PRIVKEY)
				t_rsa_func = RSA_private_decrypt;
			else
				t_rsa_func = RSA_public_decrypt;
			if (p_message_in_length != t_rsa_size)
			{
				t_success = false;
				MCCStringClone("error: invalid message size", r_result);
			}
		}
	}
	if (t_success)
	{
		t_output_length = t_rsa_func(p_message_in_length, (const uint8_t*)p_message_in, t_output_buffer, t_rsa, RSA_PKCS1_PADDING);
		if (t_output_length < 0)
		{
			t_success = false;
			MCCStringClone("error: SSL operation failed", r_result);
		}
	}

	if (t_rsa != NULL)
		RSA_free(t_rsa);
	if (t_key != NULL)
		EVP_PKEY_free(t_key);

	if (t_success)
	{
		r_message_out = (char*)t_output_buffer;
		r_message_out_length = t_output_length;
	}
	else
	{
		uint32_t t_err;
		t_err = ERR_get_error();
		if (t_err)
		{
			const char *t_ssl_error = ERR_reason_error_string(t_err);
			MCCStringAppendFormat(r_result, " (SSL error: %s)", t_ssl_error);
		}
		MCMemoryDeallocate(t_output_buffer);
	}

	return t_success;
}
Exemple #21
0
void __CGDataProviderDeallocate(void *info, const void *data, size_t size)
{
	MCMemoryDeallocate(const_cast<void*>(data));
}
Exemple #22
0
bool MCTextLayout(const unichar_t *p_chars, uint32_t p_char_count, MCFontStruct *p_font, MCTextLayoutCallback p_callback, void *p_context)
{
	bool t_success;
	t_success = true;

	// The state structure we use to record the list of runs and the dc we use
	// for processing.
	MCTextLayoutState self;
	MCMemoryClear(&self, sizeof(MCTextLayoutState));

	if (t_success)
	{
		self . dc = CreateCompatibleDC(nil);
		if (self . dc == nil)
			t_success = false;
	}

	// Fetch a layout font for the provided HFONT.
	if (t_success)
		t_success = MCTextLayoutFontFromHFONT(p_font -> fid, self . primary_font);

	// First thing we need to do is itemize the input string. The ScriptItemize
	// function splits up the chars into runs, each run being potentially
	// processed differently by Uniscribe.
	// Unfortunately, there is no way to predict for an arbitrary string how
	// many items might be generated, nor is the ScriptItemize function
	// incremental, thus we must loop with an every increasing buffer until
	// we have enough room.
	SCRIPT_ITEM *t_items;
	uint32_t t_item_limit, t_item_count;
	SCRIPT_STATE t_script_state;
	SCRIPT_CONTROL t_script_control;
	t_items = nil;
	t_item_limit = 0;
	t_item_count = 0;
	MCMemoryClear(&t_script_state, sizeof(SCRIPT_STATE));
	MCMemoryClear(&t_script_control, sizeof(SCRIPT_CONTROL));
	while(t_success)
	{
		// Increase the item array by 32 each time
		if (t_success)
			t_success = MCMemoryResizeArray(t_item_limit + 32, t_items, t_item_limit);
		
		// Attempt to itemize
		HRESULT t_result;
		if (t_success)
		{
			t_result = ScriptItemize(p_chars, p_char_count, t_item_limit, &t_script_control, &t_script_state, t_items, (int *)&t_item_count);
			if (t_result != S_OK && t_result != E_OUTOFMEMORY)
				t_success = false;
		}

		if (t_success && t_result == S_OK)
			break;
	}

	// Next we loop through the items one by one, processing them as we go, this
	// process is slightly recursive - LayoutItem may recurse to fill in any
	// 'holes' caused by glyphs not in the primary font.
	for(uint32_t i = 0; i < t_item_count && t_success; i++)
		t_success = MCTextLayoutStyleItem(
						self,
						t_items[i] . a,
						p_chars + t_items[i] . iCharPos,
						t_items[i + 1] . iCharPos - t_items[i] . iCharPos,
						self . primary_font);

	// At this point we should have an array of runs to render. First though we
	// need to compute the visual to logical mapping.
	uint8_t *t_levels;
	int *t_map;
	t_levels = nil;
	t_map = nil;
	if (t_success)
		t_success =
			MCMemoryNewArray(self . run_count, t_levels) &&
			MCMemoryNewArray(self . run_count, t_map);

	// Work out the run mapping, but only if we have runs to map!
	if (t_success && self . run_count > 0)
	{
		for(uint32_t i = 0; i < self . run_count; i++)
			t_levels[i] = self . runs[i] . embedding_level;

		if (ScriptLayout(self . run_count, t_levels, t_map, nil) != S_OK)
			t_success = false;
	}

	// Now we have the mapping we loop through the runs in the correct order
	// dispatching them to the callback.
	if (t_success && p_callback != nil)
	{
		double t_x;
		t_x = 0.0;
		for(uint32_t i = 0; i < self . run_count && t_success; i++)
		{
			MCTextLayoutRun *t_run;
			t_run = &self . runs[t_map[i]];

			// Allocate a temporary array for the glyph structures
			MCTextLayoutGlyph *t_glyphs;
			t_glyphs = nil;
			if (t_success)
				t_success = MCMemoryNewArray(t_run -> glyph_count, t_glyphs);

			if (t_success)
			{
				// Compute the position for each glyph, keeping a running
				// total of the advance width.
				for(uint32_t i = 0; i < t_run -> glyph_count; i++)
				{
					t_glyphs[i] . index = t_run -> glyphs[i];
					t_glyphs[i] . x = t_x + t_run -> goffsets[i] . du;
					t_glyphs[i] . y = t_run -> goffsets[i] . dv;
					t_x += t_run -> advances[i];
				}

				// Dispatch the span to the callback.
				MCTextLayoutSpan t_span;
				t_span . chars = t_run -> chars;
				t_span . clusters = t_run -> clusters;
				t_span . char_count = t_run -> char_count;
				t_span . glyphs = t_glyphs;
				t_span . glyph_count = t_run -> glyph_count;
				t_span . font = t_run -> font -> handle;
				t_success = p_callback(p_context, &t_span);
			}

			// Free the temporary array.
			MCMemoryDeleteArray(t_glyphs);
		}
	}

	// Free all the arrays and other resources that have been allocated.
	MCMemoryDeleteArray(t_map);
	MCMemoryDeleteArray(t_levels);

	for(uint32_t i = 0; i < self . run_count; i++)
	{
		MCMemoryDeallocate(self . runs[i] . chars);
		MCMemoryDeallocate(self . runs[i] . clusters);
		MCMemoryDeallocate(self . runs[i] . glyphs);
		MCMemoryDeallocate(self . runs[i] . advances);
		MCMemoryDeallocate(self . runs[i] . goffsets);
	}

	MCMemoryDeleteArray(self . runs);
	
	MCMemoryDeleteArray(t_items);

	if (self . dc != nil)
		DeleteDC(self . dc);

	return t_success;
}
Exemple #23
0
bool MCImageEncodePNG(MCImageIndexedBitmap *p_indexed, IO_handle p_stream, uindex_t &r_bytes_written)
{
	bool t_success = true;

	MCPNGWriteContext t_context;
	t_context.stream = p_stream;
	t_context.byte_count = 0;

	png_structp t_png_ptr = nil;
	png_infop t_info_ptr = nil;
	png_color *t_png_palette = nil;
	png_byte *t_png_transparency = nil;

	png_bytep t_data_ptr = nil;
	uindex_t t_stride = 0;

	/*init png stuff*/
	if (t_success)
	{
		t_success = nil != (t_png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
			(png_voidp)NULL, (png_error_ptr)NULL,
			(png_error_ptr)NULL));
	}

	if (t_success)
		t_success = nil != (t_info_ptr = png_create_info_struct(t_png_ptr));

	/*in case of png error*/
	if (setjmp(png_jmpbuf(t_png_ptr)))
		t_success = false;

	if (t_success)
		png_set_write_fn(t_png_ptr,(png_voidp)&t_context,fakewrite,fakeflush);

	if (t_success)
	{
		png_set_IHDR(t_png_ptr, t_info_ptr, p_indexed->width, p_indexed->height, 8,
					 PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE,
					 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
		png_set_gAMA(t_png_ptr, t_info_ptr, 1/MCgamma);
	}

	if (t_success)
		t_success = MCMemoryNewArray(p_indexed->palette_size, t_png_palette);

	/*create palette for 8 bit*/
	if (t_success)
	{
		for (uindex_t i = 0; i < p_indexed->palette_size ; i++)
		{
			t_png_palette[i].red   = p_indexed->palette[i].red >> 8;
			t_png_palette[i].green = p_indexed->palette[i].green >> 8;
			t_png_palette[i].blue  = p_indexed->palette[i].blue >> 8;
		}

		png_set_PLTE(t_png_ptr, t_info_ptr, t_png_palette, p_indexed->palette_size);
	}

	if (MCImageIndexedBitmapHasTransparency(p_indexed))
	{
		if (t_success)
			t_success = MCMemoryAllocate(p_indexed->palette_size, t_png_transparency);
		if (t_success)
		{
			memset(t_png_transparency, 0xFF, p_indexed->palette_size);
			t_png_transparency[p_indexed->transparent_index] = 0x00;
			png_set_tRNS(t_png_ptr, t_info_ptr, t_png_transparency, p_indexed->palette_size, NULL);
		}
	}
	if (t_success)
		png_write_info(t_png_ptr, t_info_ptr);

	if (t_success)
	{
		t_data_ptr = (png_bytep)p_indexed->data;
		t_stride = p_indexed->stride;
	}

	if (t_success)
	{
		for (uindex_t i = 0; i < p_indexed->height; i++)
		{
			png_write_row(t_png_ptr, t_data_ptr);
			t_data_ptr += t_stride;
		}
	}

	if (t_success)
		png_write_end(t_png_ptr, t_info_ptr);

	if (t_png_ptr != nil)
		png_destroy_write_struct(&t_png_ptr, &t_info_ptr);
	if (t_png_palette != nil)
		MCMemoryDeleteArray(t_png_palette);
	if (t_png_transparency != nil)
		MCMemoryDeallocate(t_png_transparency);

	if (t_success)
		r_bytes_written = t_context.byte_count;

	return t_success;
}
MCVectorImageRep::~MCVectorImageRep()
{
	MCMemoryDeallocate(m_data);
}
Exemple #25
0
bool MCGRasterToCGImage(const MCGRaster &p_raster, MCGRectangle p_src_rect, CGColorSpaceRef p_colorspace, bool p_copy, bool p_invert, CGImageRef &r_image)
{
	bool t_success = true;
	
	int32_t t_x, t_y;
	uint32_t t_width, t_height;
	t_x = p_src_rect.origin.x;
	t_y = p_src_rect.origin.y;
	t_width = p_src_rect.size.width;
	t_height = p_src_rect.size.height;
	
	/* OVERHAUL - REVISIT: pixel formats */
	const uint8_t *t_src_ptr = (uint8_t*)p_raster.pixels;
	t_src_ptr += t_y * p_raster.stride + t_x * sizeof(uint32_t);
	
	uint32_t t_dst_stride;
	
	if (p_invert)
		p_copy = true;
	
	CGImageRef t_image = nil;
	CGDataProviderRef t_data_provider = nil;
	
	if (!p_copy)
	{
		t_dst_stride = p_raster.stride;
		t_success = nil != (t_data_provider = CGDataProviderCreateWithData(nil, t_src_ptr, t_height * p_raster.stride, nil));
	}
	else
	{
		uint8_t* t_buffer = nil;
		
		t_dst_stride = t_width * sizeof(uint32_t);
		uindex_t t_buffer_size = t_height * t_dst_stride;
		t_success = MCMemoryAllocate(t_buffer_size, t_buffer);
		
		if (t_success)
		{
			int32_t t_src_stride;
			
			uint8_t* t_dst_ptr = t_buffer;
			if (!p_invert)
			{
				t_src_stride = p_raster.stride;
			}
			else
			{
				t_src_ptr += ((int32_t)t_height - 1) * p_raster.stride;
				t_src_stride = -p_raster.stride;
			}
			
			for (uindex_t y = 0; y < t_height; y++)
			{
				MCMemoryCopy(t_dst_ptr, t_src_ptr, t_dst_stride);
				t_dst_ptr += t_dst_stride;
				t_src_ptr += t_src_stride;
			}
		}
		if (t_success)
			t_success = nil != (t_data_provider = CGDataProviderCreateWithData(nil, t_buffer, t_buffer_size, __CGDataProviderDeallocate));
		
		if (!t_success)
			MCMemoryDeallocate(t_buffer);
		
	}
	
	// IM-2013-08-21: [[ RefactorGraphics ]] Refactor CGImage creation code to be pixel-format independent
	CGBitmapInfo t_bm_info;
	t_bm_info = MCGPixelFormatToCGBitmapInfo(kMCGPixelFormatNative, true);
	
	if (t_success)
		t_success = nil != (t_image = CGImageCreate(t_width, t_height, 8, 32, t_dst_stride, p_colorspace, t_bm_info, t_data_provider, nil, true, kCGRenderingIntentDefault));
	
	CGDataProviderRelease(t_data_provider);
	
	if (t_success)
		r_image = t_image;
	
	return t_success;
}
Exemple #26
0
bool MCFileSystemListEntries(const char *p_folder, uint32_t p_options, MCFileSystemListCallback p_callback, void *p_context)
{
	bool t_success;
	t_success = true;

	char *t_pattern;
	t_pattern = nil;
	if (t_success)
		t_success = MCCStringFormat(t_pattern, "%s%s", p_folder, MCCStringEndsWith(p_folder, "/") ? "*" : "/*");

	void *t_native_pattern;
	t_native_pattern = nil;
	if (t_success)
		t_success = MCFileSystemPathToNative(t_pattern, t_native_pattern);

	HANDLE t_find_handle;
	WIN32_FIND_DATAW t_find_data;
	t_find_handle = INVALID_HANDLE_VALUE;
	if (t_success)
	{
		t_find_handle = FindFirstFileW((LPCWSTR)t_native_pattern, &t_find_data);
		if (t_find_handle == INVALID_HANDLE_VALUE)
			t_success = false;
	}

	while(t_success)
	{
		char *t_entry_filename;
		if (t_success)
			t_success = MCFileSystemPathFromNative(t_find_data . cFileName, t_entry_filename);

		MCFileSystemEntry t_entry;
		if (t_success)
		{
			t_entry . type = (t_find_data . dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0 ? kMCFileSystemEntryFolder : kMCFileSystemEntryFile;
			t_entry . filename = t_entry_filename;

			t_success = p_callback(p_context, t_entry);
		}

		MCCStringFree(t_entry_filename);

		////

		if (!FindNextFileW(t_find_handle, &t_find_data))
		{
			if (GetLastError() == ERROR_NO_MORE_FILES)
				break;

			t_success = false;
		}
	}

	if (t_find_handle != INVALID_HANDLE_VALUE)
		FindClose(t_find_handle);

	MCMemoryDeallocate(t_native_pattern);
	MCCStringFree(t_pattern);

	return t_success;
}
Exemple #27
0
bool MCGRasterCreateCGDataProvider(const MCGRaster &p_raster, const MCGIntegerRectangle &p_src_rect, bool p_copy, bool p_invert, CGDataProviderRef &r_data_provider, uint32_t &r_stride)
{
	MCAssert(p_src_rect.origin.x >= 0 && p_src_rect.origin.y >= 0);
	MCAssert(p_src_rect.origin.x + p_src_rect.size.width <= p_raster.width);
	MCAssert(p_src_rect.origin.y + p_src_rect.size.height <= p_raster.height);
	
	bool t_success = true;
	
	int32_t t_x, t_y;
	uint32_t t_width, t_height;
	t_x = p_src_rect.origin.x;
	t_y = p_src_rect.origin.y;
	t_width = p_src_rect.size.width;
	t_height = p_src_rect.size.height;
	
	uint8_t *t_src_ptr = (uint8_t*)MCGRasterGetPixelPtr(p_raster, t_x, t_y);
	
	uint32_t t_dst_stride;
	
	if (p_invert)
		p_copy = true;
	
	CGDataProviderRef t_data_provider = nil;
	if (!p_copy)
	{
		t_dst_stride = p_raster.stride;
		t_data_provider = CGDataProviderCreateWithData(nil, t_src_ptr, t_height * p_raster.stride, __CGDataProviderDeallocate);
		t_success = t_data_provider != nil;
        if (!t_success)
            MCMemoryDeallocate(t_src_ptr);
	}
	else
	{
		uint8_t* t_buffer = nil;
		
		t_dst_stride = t_width * sizeof(uint32_t);
		uindex_t t_buffer_size = t_height * t_dst_stride;
		t_success = MCMemoryAllocate(t_buffer_size, t_buffer);
		
		if (t_success)
		{
			int32_t t_src_stride;
			
			uint8_t* t_dst_ptr = t_buffer;
			if (!p_invert)
			{
				t_src_stride = p_raster.stride;
			}
			else
			{
				t_src_ptr += ((int32_t)t_height - 1) * p_raster.stride;
				t_src_stride = -p_raster.stride;
			}
			
			for (uindex_t y = 0; y < t_height; y++)
			{
				MCMemoryCopy(t_dst_ptr, t_src_ptr, t_dst_stride);
				t_dst_ptr += t_dst_stride;
				t_src_ptr += t_src_stride;
			}
		}
		if (t_success)
		{
			t_data_provider = CGDataProviderCreateWithData(nil, t_buffer, t_buffer_size, __CGDataProviderDeallocate);
			t_success = t_data_provider != nil;
		}
		
		if (!t_success)
			MCMemoryDeallocate(t_buffer);
	}
	
	if (t_success)
	{
		r_data_provider = t_data_provider;
		r_stride = t_dst_stride;
	}
	
	return t_success;
}
Exemple #28
0
bool MCImageEncodePNG(MCImageBitmap *p_bitmap, IO_handle p_stream, uindex_t &r_bytes_written)
{
	bool t_success = true;

	MCPNGWriteContext t_context;
	t_context.stream = p_stream;
	t_context.byte_count = 0;

	png_structp t_png_ptr = nil;
	png_infop t_info_ptr = nil;
	png_color *t_png_palette = nil;
	png_byte *t_png_transparency = nil;

	png_bytep t_data_ptr = nil;
	uindex_t t_stride = 0;

	MCImageIndexedBitmap *t_indexed = nil;
	if (MCImageConvertBitmapToIndexed(p_bitmap, false, t_indexed))
	{
		t_success = MCImageEncodePNG(t_indexed, p_stream, r_bytes_written);
		MCImageFreeIndexedBitmap(t_indexed);
		return t_success;
	}

	/*init png stuff*/
	if (t_success)
	{
		t_success = nil != (t_png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
			(png_voidp)NULL, (png_error_ptr)NULL,
			(png_error_ptr)NULL));
	}

	if (t_success)
		t_success = nil != (t_info_ptr = png_create_info_struct(t_png_ptr));

	/*in case of png error*/
	if (setjmp(png_jmpbuf(t_png_ptr)))
		t_success = false;

	if (t_success)
		png_set_write_fn(t_png_ptr,(png_voidp)&t_context,fakewrite,fakeflush);

	bool t_fully_opaque = true;
	if (t_success)
	{
		t_fully_opaque = !MCImageBitmapHasTransparency(p_bitmap);

		png_set_IHDR(t_png_ptr, t_info_ptr, p_bitmap->width, p_bitmap->height, 8,
			t_fully_opaque ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
			PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
		png_set_gAMA(t_png_ptr, t_info_ptr, 1/MCgamma);
	}

	if (t_success)
	{
		png_write_info(t_png_ptr, t_info_ptr);

		//32 bit compensate for byte swapped systems
		if (t_fully_opaque)
			png_set_filler(t_png_ptr, 0, MCswapbytes ? PNG_FILLER_AFTER : PNG_FILLER_BEFORE);
		if (MCswapbytes)
			png_set_bgr(t_png_ptr);
		else
			png_set_swap_alpha(t_png_ptr);
	}

	if (t_success)
	{
		t_data_ptr = (png_bytep)p_bitmap->data;
		t_stride = p_bitmap->stride;
	}

	if (t_success)
	{
		for (uindex_t i = 0; i < p_bitmap->height; i++)
		{
			png_write_row(t_png_ptr, t_data_ptr);
			t_data_ptr += t_stride;
		}
	}

	if (t_success)
		png_write_end(t_png_ptr, t_info_ptr);

	if (t_png_ptr != nil)
		png_destroy_write_struct(&t_png_ptr, &t_info_ptr);
	if (t_png_palette != nil)
		MCMemoryDeleteArray(t_png_palette);
	if (t_png_transparency != nil)
		MCMemoryDeallocate(t_png_transparency);

	if (t_success)
		r_bytes_written = t_context.byte_count;

	return t_success;
}
Exemple #29
0
static bool MCRegistryListValues(HKEY p_root, const char *p_key, MCRegistryListValuesCallback p_callback, void *p_context)
{
	bool t_success;
	t_success = true;

	// Attempt to open the given key.
	HKEY t_handle;
	t_handle = nil;
	if (t_success)
		if (RegOpenKeyExA(p_root, p_key, 0, KEY_QUERY_VALUE, &t_handle) != ERROR_SUCCESS)
			t_success = false;

	// Next determine the maximum length of the value names.
	DWORD t_max_name_length;
	if (t_success)
		if (RegQueryInfoKeyA(t_handle, nil, nil, nil, nil, nil, nil, nil, &t_max_name_length, nil, nil, nil) != ERROR_SUCCESS)
			t_success = false;

	// Allocate a buffer big enough for the name
	char *t_name_buffer;
	t_name_buffer = nil;
	if (t_success)
		t_success = MCMemoryNewArray(t_max_name_length + 1, t_name_buffer);

	if (t_success)
	{
		DWORD t_index;
		t_index = 0;
		while(t_success)
		{
			DWORD t_name_length, t_value_length;
			t_name_length = t_max_name_length + 1;
			t_value_length = 0;

			LSTATUS t_result;
			if (t_success)
			{
				t_result = RegEnumValueA(t_handle, t_index, t_name_buffer, &t_name_length, nil, nil, nil, &t_value_length);
				if (t_result == ERROR_NO_MORE_ITEMS)
					break;
				if (t_result != ERROR_SUCCESS)
					t_success = false;
			}

			void *t_value_buffer;
			t_value_buffer = nil;
			if (t_success)
				t_success = MCMemoryAllocate(t_value_length, t_value_buffer);

			DWORD t_type;
			if (t_success)
			{
				t_name_length = t_max_name_length + 1;
				if (RegEnumValueA(t_handle, t_index, t_name_buffer, &t_name_length, nil, &t_type, (LPBYTE)t_value_buffer, &t_value_length) != ERROR_SUCCESS)
					t_success = false;
			}

			if (t_success && p_callback != nil)
				p_callback(p_context, t_name_buffer, t_type, t_value_buffer, t_value_length);

			MCMemoryDeallocate(t_value_buffer);

			t_index++;
		}
	}

	MCMemoryDeleteArray(t_name_buffer);

	if (t_handle != nil)
		RegCloseKey(t_handle);

	return t_success;
}