LibreOffice Module comphelper (master) 1
graphicmimetype.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
21
22#include <com/sun/star/beans/PropertyValue.hpp>
23#include <com/sun/star/beans/XPropertySet.hpp>
24#include <com/sun/star/graphic/GraphicProvider.hpp>
25#include <com/sun/star/graphic/XGraphicProvider.hpp>
26#include <com/sun/star/io/XInputStream.hpp>
27#include <com/sun/star/uno/Reference.hxx>
28
31
32using namespace css;
33using namespace css::beans;
34using namespace css::graphic;
35using namespace css::io;
36using namespace css::uno;
37
38namespace comphelper
39{
41{
42 struct XMLGraphicMimeTypeMapper
43 {
44 const char* pExt;
45 const char* pMimeType;
46 };
47
48 static const XMLGraphicMimeTypeMapper aMapper[]
49 = { { "gif", "image/gif" }, { "png", "image/png" }, { "jpg", "image/jpeg" },
50 { "tif", "image/tiff" }, { "svg", "image/svg+xml" }, { "pdf", "application/pdf" },
51 { "wmf", "image/x-wmf" }, { "emf", "image/x-emf" }, { "eps", "image/x-eps" },
52 { "bmp", "image/bmp" }, { "pct", "image/x-pict" }, { "svm", "image/x-svm" } };
53
54 OUString aMimeType;
55
56 size_t const nCount = std::size(aMapper);
57 for (size_t i = 0; (i < nCount) && aMimeType.isEmpty(); ++i)
58 {
59 if (rExt == aMapper[i].pExt)
60 aMimeType = OUString(aMapper[i].pMimeType, strlen(aMapper[i].pMimeType),
61 RTL_TEXTENCODING_ASCII_US);
62 }
63
64 return aMimeType;
65}
66
67OUString GraphicMimeTypeHelper::GetMimeTypeForXGraphic(const Reference<XGraphic>& xGraphic)
68{
69 OUString aSourceMimeType;
70 Reference<XPropertySet> const xGraphicPropertySet(xGraphic, UNO_QUERY);
71 if (xGraphicPropertySet.is() && // it's null if it's an external link
72 (xGraphicPropertySet->getPropertyValue("MimeType") >>= aSourceMimeType))
73 {
74 return aSourceMimeType;
75 }
76 return "";
77}
78
79OUString
80GraphicMimeTypeHelper::GetMimeTypeForImageStream(const Reference<XInputStream>& xInputStream)
81{
82 // Create the graphic to retrieve the mimetype from it
83 Reference<XGraphicProvider> xProvider
84 = css::graphic::GraphicProvider::create(comphelper::getProcessComponentContext());
85 Sequence<PropertyValue> aMediaProperties{ comphelper::makePropertyValue("InputStream",
86 xInputStream) };
87 Reference<XGraphic> xGraphic(xProvider->queryGraphic(aMediaProperties));
88
89 return GetMimeTypeForXGraphic(xGraphic);
90}
91
93{
94 switch (convertDataFormat)
95 {
96 case ConvertDataFormat::BMP:
97 return "image/bmp";
98 case ConvertDataFormat::GIF:
99 return "image/gif";
100 case ConvertDataFormat::JPG:
101 return "image/jpeg";
102 case ConvertDataFormat::PCT:
103 return "image/x-pict";
104 case ConvertDataFormat::PNG:
105 return "image/png";
106 case ConvertDataFormat::SVM:
107 return "image/x-svm";
108 case ConvertDataFormat::TIF:
109 return "image/tiff";
110 case ConvertDataFormat::WMF:
111 return "image/x-wmf";
112 case ConvertDataFormat::EMF:
113 return "image/x-emf";
114 case ConvertDataFormat::SVG:
115 return "image/svg+xml";
116 case ConvertDataFormat::MET: // What is this?
117 case ConvertDataFormat::Unknown:
118 default:
119 return "";
120 }
121}
122
124{
125 char const* pExt = nullptr;
126 // create extension
127 if (nFormat != ConvertDataFormat::Unknown)
128 {
129 switch (nFormat)
130 {
131 case ConvertDataFormat::BMP:
132 pExt = ".bmp";
133 break;
134 case ConvertDataFormat::GIF:
135 pExt = ".gif";
136 break;
137 case ConvertDataFormat::JPG:
138 pExt = ".jpg";
139 break;
140 case ConvertDataFormat::MET:
141 pExt = ".met";
142 break;
143 case ConvertDataFormat::PCT:
144 pExt = ".pct";
145 break;
146 case ConvertDataFormat::PNG:
147 pExt = ".png";
148 break;
149 case ConvertDataFormat::SVM:
150 pExt = ".svm";
151 break;
152 case ConvertDataFormat::TIF:
153 pExt = ".tif";
154 break;
155 case ConvertDataFormat::WMF:
156 pExt = ".wmf";
157 break;
158 case ConvertDataFormat::EMF:
159 pExt = ".emf";
160 break;
161
162 default:
163 pExt = ".grf";
164 break;
165 }
166 }
167 return pExt;
168}
169}
170
171/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static OUString GetMimeTypeForXGraphic(const css::uno::Reference< css::graphic::XGraphic > &xGraphic)
static OUString GetMimeTypeForImageStream(const css::uno::Reference< css::io::XInputStream > &xInputStream)
static char const * GetExtensionForConvertDataFormat(ConvertDataFormat nFormat)
static OUString GetMimeTypeForConvertDataFormat(ConvertDataFormat convertDataFormat)
static OUString GetMimeTypeForExtension(std::string_view rExt)
int nCount
Reference< XComponentContext > getProcessComponentContext()
This function gets the process service factory's default component context.
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
Creates a beans::PropertyValue easily, i.e.
int i
ConvertDataFormat
const sal_Unicode *const aMimeType[]
const char * pExt