165f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch/*
265f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch * Copyright (C) 2011 Apple Inc. All rights reserved.
365f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch *
465f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch * Redistribution and use in source and binary forms, with or without
565f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch * modification, are permitted provided that the following conditions
665f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch * are met:
765f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch * 1. Redistributions of source code must retain the above copyright
865f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch *    notice, this list of conditions and the following disclaimer.
965f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch * 2. Redistributions in binary form must reproduce the above copyright
1065f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch *    notice, this list of conditions and the following disclaimer in the
1165f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch *    documentation and/or other materials provided with the distribution.
1265f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch *
1365f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
1465f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
1565f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1665f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
1765f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1865f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
1965f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2065f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2165f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2265f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
2365f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch * THE POSSIBILITY OF SUCH DAMAGE.
2465f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch */
2565f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch
262fc2651226baac27029e38c9d6ef883fa32084dbSteve Block#include "config.h"
2765f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch#include "CGUtilities.h"
2865f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch
2965f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch#include <wtf/RetainPtr.h>
3065f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch
3165f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdochnamespace WebKit {
3265f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch
332bde8e466a4451c7319e3a072d118917957d6554Steve Blockvoid paintImage(CGContextRef context, CGImageRef image, CGPoint destination, CGRect source)
342bde8e466a4451c7319e3a072d118917957d6554Steve Block{
3565f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch    CGContextSaveGState(context);
3665f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch
3765f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch    CGContextClipToRect(context, CGRectMake(destination.x, destination.y, source.size.width, source.size.height));
3865f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch    CGContextScaleCTM(context, 1, -1);
3965f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch
402bde8e466a4451c7319e3a072d118917957d6554Steve Block    size_t imageHeight = CGImageGetHeight(image);
412bde8e466a4451c7319e3a072d118917957d6554Steve Block    size_t imageWidth = CGImageGetWidth(image);
422bde8e466a4451c7319e3a072d118917957d6554Steve Block
4365f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch    CGFloat destX = destination.x - source.origin.x;
4465f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch    CGFloat destY = -static_cast<CGFloat>(imageHeight) - destination.y + source.origin.y;
4565f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch
462bde8e466a4451c7319e3a072d118917957d6554Steve Block    CGContextDrawImage(context, CGRectMake(destX, destY, imageWidth, imageHeight), image);
4765f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch    CGContextRestoreGState(context);
4865f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch}
492bde8e466a4451c7319e3a072d118917957d6554Steve Block
502bde8e466a4451c7319e3a072d118917957d6554Steve Blockvoid paintBitmapContext(CGContextRef context, CGContextRef bitmapContext, CGPoint destination, CGRect source)
512bde8e466a4451c7319e3a072d118917957d6554Steve Block{
522bde8e466a4451c7319e3a072d118917957d6554Steve Block    RetainPtr<CGImageRef> image(AdoptCF, CGBitmapContextCreateImage(bitmapContext));
532bde8e466a4451c7319e3a072d118917957d6554Steve Block    paintImage(context, image.get(), destination, source);
542bde8e466a4451c7319e3a072d118917957d6554Steve Block}
552bde8e466a4451c7319e3a072d118917957d6554Steve Block
5665f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch} // namespace WebKit
5765f03d4f644ce73618e5f4f50dd694b26f55ae12Ben Murdoch
58