Revert the sync.
[reactos.git] / dll / win32 / windowscodecs / bmpdecode.c
index 854b337..5c805c8 100644 (file)
@@ -58,14 +58,12 @@ typedef struct {
     DWORD bc2AppData;
 } BITMAPCOREHEADER2;
 
-struct BmpDecoder;
-typedef HRESULT (*ReadDataFunc)(struct BmpDecoder* This);
+struct BmpFrameDecode;
+typedef HRESULT (*ReadDataFunc)(struct BmpFrameDecode* This);
 
-typedef struct BmpDecoder {
-    const IWICBitmapDecoderVtbl *lpVtbl;
-    const IWICBitmapFrameDecodeVtbl *lpFrameVtbl;
+typedef struct BmpFrameDecode {
+    const IWICBitmapFrameDecodeVtbl *lpVtbl;
     LONG ref;
-    BOOL initialized;
     IStream *stream;
     BITMAPFILEHEADER bfh;
     BITMAPV5HEADER bih;
@@ -75,17 +73,12 @@ typedef struct BmpDecoder {
     INT stride;
     BYTE *imagedata;
     BYTE *imagedatastart;
-    CRITICAL_SECTION lock; /* must be held when initialized/imagedata is set or stream is accessed */
-} BmpDecoder;
-
-static inline BmpDecoder *impl_from_frame(IWICBitmapFrameDecode *iface)
-{
-    return CONTAINING_RECORD(iface, BmpDecoder, lpFrameVtbl);
-}
+} BmpFrameDecode;
 
 static HRESULT WINAPI BmpFrameDecode_QueryInterface(IWICBitmapFrameDecode *iface, REFIID iid,
     void **ppv)
 {
+    BmpFrameDecode *This = (BmpFrameDecode*)iface;
     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
 
     if (!ppv) return E_INVALIDARG;
@@ -94,7 +87,7 @@ static HRESULT WINAPI BmpFrameDecode_QueryInterface(IWICBitmapFrameDecode *iface
         IsEqualIID(&IID_IWICBitmapSource, iid) ||
         IsEqualIID(&IID_IWICBitmapFrameDecode, iid))
     {
-        *ppv = iface;
+        *ppv = This;
     }
     else
     {
@@ -108,22 +101,35 @@ static HRESULT WINAPI BmpFrameDecode_QueryInterface(IWICBitmapFrameDecode *iface
 
 static ULONG WINAPI BmpFrameDecode_AddRef(IWICBitmapFrameDecode *iface)
 {
-    BmpDecoder *This = impl_from_frame(iface);
+    BmpFrameDecode *This = (BmpFrameDecode*)iface;
+    ULONG ref = InterlockedIncrement(&This->ref);
+
+    TRACE("(%p) refcount=%u\n", iface, ref);
 
-    return IUnknown_AddRef((IUnknown*)This);
+    return ref;
 }
 
 static ULONG WINAPI BmpFrameDecode_Release(IWICBitmapFrameDecode *iface)
 {
-    BmpDecoder *This = impl_from_frame(iface);
+    BmpFrameDecode *This = (BmpFrameDecode*)iface;
+    ULONG ref = InterlockedDecrement(&This->ref);
+
+    TRACE("(%p) refcount=%u\n", iface, ref);
+
+    if (ref == 0)
+    {
+        IStream_Release(This->stream);
+        HeapFree(GetProcessHeap(), 0, This->imagedata);
+        HeapFree(GetProcessHeap(), 0, This);
+    }
 
-    return IUnknown_Release((IUnknown*)This);
+    return ref;
 }
 
 static HRESULT WINAPI BmpFrameDecode_GetSize(IWICBitmapFrameDecode *iface,
     UINT *puiWidth, UINT *puiHeight)
 {
-    BmpDecoder *This = impl_from_frame(iface);
+    BmpFrameDecode *This = (BmpFrameDecode*)iface;
     TRACE("(%p,%p,%p)\n", iface, puiWidth, puiHeight);
 
     if (This->bih.bV5Size == sizeof(BITMAPCOREHEADER))
@@ -143,7 +149,7 @@ static HRESULT WINAPI BmpFrameDecode_GetSize(IWICBitmapFrameDecode *iface,
 static HRESULT WINAPI BmpFrameDecode_GetPixelFormat(IWICBitmapFrameDecode *iface,
     WICPixelFormatGUID *pPixelFormat)
 {
-    BmpDecoder *This = impl_from_frame(iface);
+    BmpFrameDecode *This = (BmpFrameDecode*)iface;
     TRACE("(%p,%p)\n", iface, pPixelFormat);
 
     memcpy(pPixelFormat, This->pixelformat, sizeof(GUID));
@@ -174,7 +180,7 @@ static HRESULT BmpHeader_GetResolution(BITMAPV5HEADER *bih, double *pDpiX, doubl
 static HRESULT WINAPI BmpFrameDecode_GetResolution(IWICBitmapFrameDecode *iface,
     double *pDpiX, double *pDpiY)
 {
-    BmpDecoder *This = impl_from_frame(iface);
+    BmpFrameDecode *This = (BmpFrameDecode*)iface;
     TRACE("(%p,%p,%p)\n", iface, pDpiX, pDpiY);
 
     return BmpHeader_GetResolution(&This->bih, pDpiX, pDpiY);
@@ -184,15 +190,13 @@ static HRESULT WINAPI BmpFrameDecode_CopyPalette(IWICBitmapFrameDecode *iface,
     IWICPalette *pIPalette)
 {
     HRESULT hr;
-    BmpDecoder *This = impl_from_frame(iface);
+    BmpFrameDecode *This = (BmpFrameDecode*)iface;
     int count;
     WICColor *wiccolors=NULL;
     RGBTRIPLE *bgrcolors=NULL;
 
     TRACE("(%p,%p)\n", iface, pIPalette);
 
-    EnterCriticalSection(&This->lock);
-
     if (This->bih.bV5Size == sizeof(BITMAPCOREHEADER))
     {
         BITMAPCOREHEADER *bch = (BITMAPCOREHEADER*)&This->bih;
@@ -234,8 +238,7 @@ static HRESULT WINAPI BmpFrameDecode_CopyPalette(IWICBitmapFrameDecode *iface,
         }
         else
         {
-            hr = WINCODEC_ERR_PALETTEUNAVAILABLE;
-            goto end;
+            return WINCODEC_ERR_PALETTEUNAVAILABLE;
         }
     }
     else
@@ -253,11 +256,7 @@ static HRESULT WINAPI BmpFrameDecode_CopyPalette(IWICBitmapFrameDecode *iface,
 
             tablesize = sizeof(WICColor) * count;
             wiccolors = HeapAlloc(GetProcessHeap(), 0, tablesize);
-            if (!wiccolors)
-            {
-                hr = E_OUTOFMEMORY;
-                goto end;
-            }
+            if (!wiccolors) return E_OUTOFMEMORY;
 
             offset.QuadPart = sizeof(BITMAPFILEHEADER) + This->bih.bV5Size;
             hr = IStream_Seek(This->stream, offset, STREAM_SEEK_SET, NULL);
@@ -276,18 +275,13 @@ static HRESULT WINAPI BmpFrameDecode_CopyPalette(IWICBitmapFrameDecode *iface,
         }
         else
         {
-            hr = WINCODEC_ERR_PALETTEUNAVAILABLE;
-            goto end;
+            return WINCODEC_ERR_PALETTEUNAVAILABLE;
         }
     }
 
-end:
-
-    LeaveCriticalSection(&This->lock);
-
-    if (SUCCEEDED(hr))
-        hr = IWICPalette_InitializeCustom(pIPalette, wiccolors, count);
+    hr = IWICPalette_InitializeCustom(pIPalette, wiccolors, count);
 
+end:
     HeapFree(GetProcessHeap(), 0, wiccolors);
     HeapFree(GetProcessHeap(), 0, bgrcolors);
     return hr;
@@ -296,18 +290,16 @@ end:
 static HRESULT WINAPI BmpFrameDecode_CopyPixels(IWICBitmapFrameDecode *iface,
     const WICRect *prc, UINT cbStride, UINT cbBufferSize, BYTE *pbBuffer)
 {
-    BmpDecoder *This = impl_from_frame(iface);
-    HRESULT hr=S_OK;
+    BmpFrameDecode *This = (BmpFrameDecode*)iface;
+    HRESULT hr;
     UINT width, height;
     TRACE("(%p,%p,%u,%u,%p)\n", iface, prc, cbStride, cbBufferSize, pbBuffer);
 
-    EnterCriticalSection(&This->lock);
     if (!This->imagedata)
     {
         hr = This->read_data_func(This);
+        if (FAILED(hr)) return hr;
     }
-    LeaveCriticalSection(&This->lock);
-    if (FAILED(hr)) return hr;
 
     hr = BmpFrameDecode_GetSize(iface, &width, &height);
     if (FAILED(hr)) return hr;
@@ -338,7 +330,7 @@ static HRESULT WINAPI BmpFrameDecode_GetThumbnail(IWICBitmapFrameDecode *iface,
     return WINCODEC_ERR_CODECNOTHUMBNAIL;
 }
 
-static HRESULT BmpFrameDecode_ReadUncompressed(BmpDecoder* This)
+static HRESULT BmpFrameDecode_ReadUncompressed(BmpFrameDecode* This)
 {
     UINT bytesperrow;
     UINT width, height;
@@ -395,7 +387,7 @@ fail:
     return hr;
 }
 
-static HRESULT BmpFrameDecode_ReadRLE8(BmpDecoder* This)
+static HRESULT BmpFrameDecode_ReadRLE8(BmpFrameDecode* This)
 {
     UINT bytesperrow;
     UINT width, height;
@@ -501,7 +493,7 @@ fail:
     return hr;
 }
 
-static HRESULT BmpFrameDecode_ReadRLE4(BmpDecoder* This)
+static HRESULT BmpFrameDecode_ReadRLE4(BmpFrameDecode* This)
 {
     UINT bytesperrow;
     UINT width, height;
@@ -622,7 +614,7 @@ fail:
     return hr;
 }
 
-static HRESULT BmpFrameDecode_ReadUnsupported(BmpDecoder* This)
+static HRESULT BmpFrameDecode_ReadUnsupported(BmpFrameDecode* This)
 {
     return E_FAIL;
 }
@@ -645,7 +637,7 @@ static const struct bitfields_format bitfields_formats[] = {
     {0}
 };
 
-static const IWICBitmapFrameDecodeVtbl BmpDecoder_FrameVtbl = {
+static const IWICBitmapFrameDecodeVtbl BmpFrameDecode_Vtbl = {
     BmpFrameDecode_QueryInterface,
     BmpFrameDecode_AddRef,
     BmpFrameDecode_Release,
@@ -659,6 +651,19 @@ static const IWICBitmapFrameDecodeVtbl BmpDecoder_FrameVtbl = {
     BmpFrameDecode_GetThumbnail
 };
 
+typedef struct {
+    const IWICBitmapDecoderVtbl *lpVtbl;
+    LONG ref;
+    BOOL initialized;
+    IStream *stream;
+    BITMAPFILEHEADER bfh;
+    BITMAPV5HEADER bih;
+    BmpFrameDecode *framedecode;
+    const WICPixelFormatGUID *pixelformat;
+    int bitsperpixel;
+    ReadDataFunc read_data_func;
+} BmpDecoder;
+
 static HRESULT BmpDecoder_ReadHeaders(BmpDecoder* This, IStream *stream)
 {
     HRESULT hr;
@@ -868,9 +873,7 @@ static ULONG WINAPI BmpDecoder_Release(IWICBitmapDecoder *iface)
     if (ref == 0)
     {
         if (This->stream) IStream_Release(This->stream);
-        HeapFree(GetProcessHeap(), 0, This->imagedata);
-        This->lock.DebugInfo->Spare[0] = 0;
-        DeleteCriticalSection(&This->lock);
+        if (This->framedecode) IUnknown_Release((IUnknown*)This->framedecode);
         HeapFree(GetProcessHeap(), 0, This);
     }
 
@@ -883,9 +886,7 @@ static HRESULT WINAPI BmpDecoder_QueryCapability(IWICBitmapDecoder *iface, IStre
     HRESULT hr;
     BmpDecoder *This = (BmpDecoder*)iface;
 
-    EnterCriticalSection(&This->lock);
     hr = BmpDecoder_ReadHeaders(This, pIStream);
-    LeaveCriticalSection(&This->lock);
     if (FAILED(hr)) return hr;
 
     if (This->read_data_func == BmpFrameDecode_ReadUnsupported)
@@ -902,7 +903,6 @@ static HRESULT WINAPI BmpDecoder_Initialize(IWICBitmapDecoder *iface, IStream *p
     HRESULT hr;
     BmpDecoder *This = (BmpDecoder*)iface;
 
-    EnterCriticalSection(&This->lock);
     hr = BmpDecoder_ReadHeaders(This, pIStream);
 
     if (SUCCEEDED(hr))
@@ -910,7 +910,6 @@ static HRESULT WINAPI BmpDecoder_Initialize(IWICBitmapDecoder *iface, IStream *p
         This->stream = pIStream;
         IStream_AddRef(pIStream);
     }
-    LeaveCriticalSection(&This->lock);
 
     return hr;
 }
@@ -993,8 +992,25 @@ static HRESULT WINAPI BmpDecoder_GetFrame(IWICBitmapDecoder *iface,
 
     if (!This->stream) return WINCODEC_ERR_WRONGSTATE;
 
-    *ppIBitmapFrame = (IWICBitmapFrameDecode*)&This->lpFrameVtbl;
-    IWICBitmapDecoder_AddRef(iface);
+    if (!This->framedecode)
+    {
+        This->framedecode = HeapAlloc(GetProcessHeap(), 0, sizeof(BmpFrameDecode));
+        if (!This->framedecode) return E_OUTOFMEMORY;
+
+        This->framedecode->lpVtbl = &BmpFrameDecode_Vtbl;
+        This->framedecode->ref = 1;
+        This->framedecode->stream = This->stream;
+        IStream_AddRef(This->stream);
+        This->framedecode->bfh = This->bfh;
+        This->framedecode->bih = This->bih;
+        This->framedecode->pixelformat = This->pixelformat;
+        This->framedecode->bitsperpixel = This->bitsperpixel;
+        This->framedecode->read_data_func = This->read_data_func;
+        This->framedecode->imagedata = NULL;
+    }
+
+    *ppIBitmapFrame = (IWICBitmapFrameDecode*)This->framedecode;
+    IWICBitmapFrameDecode_AddRef((IWICBitmapFrameDecode*)This->framedecode);
 
     return S_OK;
 }
@@ -1031,13 +1047,10 @@ HRESULT BmpDecoder_CreateInstance(IUnknown *pUnkOuter, REFIID iid, void** ppv)
     if (!This) return E_OUTOFMEMORY;
 
     This->lpVtbl = &BmpDecoder_Vtbl;
-    This->lpFrameVtbl = &BmpDecoder_FrameVtbl;
     This->ref = 1;
     This->initialized = FALSE;
     This->stream = NULL;
-    This->imagedata = NULL;
-    InitializeCriticalSection(&This->lock);
-    This->lock.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": BmpDecoder.lock");
+    This->framedecode = NULL;
 
     ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
     IUnknown_Release((IUnknown*)This);