Home | History | Annotate | Download | only in wx
      1 /*
      2  * Copyright (C) 2006 Apple Computer, Inc.  All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #include "config.h"
     27 #include "ImageDecoder.h"
     28 
     29 // FIXME: Are all these needed?
     30 #include <wx/defs.h>
     31 #include <wx/bitmap.h>
     32 #if USE(WXGC)
     33 #include <wx/graphics.h>
     34 #endif
     35 #include <wx/image.h>
     36 #include <wx/rawbmp.h>
     37 
     38 namespace WebCore {
     39 
     40 NativeImagePtr RGBA32Buffer::asNewNativeImage() const
     41 {
     42     wxBitmap* bmp = new wxBitmap(width(), height(), 32);
     43 
     44     {
     45         typedef wxPixelData<wxBitmap, wxAlphaPixelFormat> WxPixelData;
     46         WxPixelData data(*bmp);
     47 
     48         // NB: It appears that the data is in BGRA format instead of RGBA format.
     49         // This code works properly on both ppc and intel, meaning the issue is
     50         // likely not an issue of byte order getting mixed up on different archs.
     51         const unsigned char* bytes = (const unsigned char*)m_bytes.data();
     52         int rowCounter = 0;
     53         long pixelCounter = 0;
     54         WxPixelData::Iterator p(data);
     55         WxPixelData::Iterator rowStart = p;
     56         for (size_t i = 0; i < m_bytes.size() * sizeof(PixelData); i += sizeof(PixelData)) {
     57                 p.Red() = bytes[i + 2];
     58                 p.Green() = bytes[i + 1];
     59                 p.Blue() = bytes[i + 0];
     60                 p.Alpha() = bytes[i + 3];
     61 
     62             p++;
     63 
     64             pixelCounter++;
     65             if ((pixelCounter % width()) == 0) {
     66                 rowCounter++;
     67                 p = rowStart;
     68                 p.MoveTo(data, 0, rowCounter);
     69             }
     70         }
     71 #if !wxCHECK_VERSION(2,9,0)
     72         bmp->UseAlpha();
     73 #endif
     74     } // ensure that WxPixelData is destroyed as it unlocks the bitmap data in
     75       // its dtor and we can't access it (notably in CreateBitmap() below)
     76       // before this is done
     77 
     78     ASSERT(bmp->IsOk());
     79 
     80 #if USE(WXGC)
     81     wxGraphicsBitmap* bitmap = new wxGraphicsBitmap(wxGraphicsRenderer::GetDefaultRenderer()->CreateBitmap(*bmp));
     82     delete bmp;
     83     return bitmap;
     84 #else
     85     return bmp;
     86 #endif
     87 }
     88 
     89 } // namespace WebCore
     90