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 
30 
31 using namespace css;
32 using namespace css::beans;
33 using namespace css::graphic;
34 using namespace css::io;
35 using namespace css::uno;
36 
37 namespace comphelper
38 {
39 OUString GraphicMimeTypeHelper::GetMimeTypeForExtension(std::string_view rExt)
40 {
41  struct XMLGraphicMimeTypeMapper
42  {
43  const char* pExt;
44  const char* pMimeType;
45  };
46 
47  static const XMLGraphicMimeTypeMapper aMapper[]
48  = { { "gif", "image/gif" }, { "png", "image/png" }, { "jpg", "image/jpeg" },
49  { "tif", "image/tiff" }, { "svg", "image/svg+xml" }, { "pdf", "application/pdf" },
50  { "wmf", "image/x-wmf" }, { "emf", "image/x-emf" }, { "eps", "image/x-eps" },
51  { "bmp", "image/bmp" }, { "pct", "image/x-pict" }, { "svm", "image/x-svm" } };
52 
53  OUString aMimeType;
54 
55  tools::Long const nCount = SAL_N_ELEMENTS(aMapper);
56  for (tools::Long i = 0; (i < nCount) && aMimeType.isEmpty(); ++i)
57  {
58  if (rExt == aMapper[i].pExt)
59  aMimeType = OUString(aMapper[i].pMimeType, strlen(aMapper[i].pMimeType),
60  RTL_TEXTENCODING_ASCII_US);
61  }
62 
63  return aMimeType;
64 }
65 
66 OUString GraphicMimeTypeHelper::GetMimeTypeForXGraphic(const Reference<XGraphic>& xGraphic)
67 {
68  OUString aSourceMimeType;
69  Reference<XPropertySet> const xGraphicPropertySet(xGraphic, UNO_QUERY);
70  if (xGraphicPropertySet.is() && // it's null if it's an external link
71  (xGraphicPropertySet->getPropertyValue("MimeType") >>= aSourceMimeType))
72  {
73  return aSourceMimeType;
74  }
75  return "";
76 }
77 
78 OUString
79 GraphicMimeTypeHelper::GetMimeTypeForImageStream(const Reference<XInputStream>& xInputStream)
80 {
81  // Create the graphic to retrieve the mimetype from it
82  Reference<XGraphicProvider> xProvider
83  = css::graphic::GraphicProvider::create(comphelper::getProcessComponentContext());
84  Sequence<PropertyValue> aMediaProperties(1);
85  aMediaProperties[0].Name = "InputStream";
86  aMediaProperties[0].Value <<= xInputStream;
87  Reference<XGraphic> xGraphic(xProvider->queryGraphic(aMediaProperties));
88 
89  return GetMimeTypeForXGraphic(xGraphic);
90 }
91 
92 OUString GraphicMimeTypeHelper::GetMimeTypeForConvertDataFormat(ConvertDataFormat convertDataFormat)
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 
123 char const* GraphicMimeTypeHelper::GetExtensionForConvertDataFormat(ConvertDataFormat nFormat)
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: */
long Long
int nCount
#define SAL_N_ELEMENTS(arr)
int i
ConvertDataFormat
const char *const aMimeType[]
Reference< XComponentContext > getProcessComponentContext()
This function gets the process service factory's default component context.
const char * pExt