diff --git a/android/jni/com/mapswithme/maps/Framework.cpp b/android/jni/com/mapswithme/maps/Framework.cpp index b1086ee788..9591e5cbf8 100644 --- a/android/jni/com/mapswithme/maps/Framework.cpp +++ b/android/jni/com/mapswithme/maps/Framework.cpp @@ -41,6 +41,8 @@ using namespace storage; using platform::CountryFile; using platform::LocalCountryFile; +static_assert(sizeof(jint) >= 4, "Size of jint in less than 4 bytes."); + namespace { ::Framework * frm() @@ -901,21 +903,17 @@ Java_com_mapswithme_maps_Framework_nativeGenerateRouteAltitudeChartBits(JNIEnv * if (!fr->GenerateRouteAltitudeChart(width, height, imageRGBAData)) { - LOG(LWARNING, ("Cann't generate route altitude image.")); + LOG(LWARNING, ("Can't generate route altitude image.")); return nullptr; } size_t const imageRGBADataSize = imageRGBAData.size(); - if (imageRGBADataSize == 0) - { - LOG(LWARNING, ("GenerateRouteAltitudeChart returns true but the vector with altitude image bits is empty.")); - return nullptr; - } + ASSERT_NOT_EQUAL(imageRGBADataSize, 0, ("GenerateRouteAltitudeChart returns true but the vector with altitude image bits is empty.")); size_t const pxlCount = width * height; - if (maps::kAlitudeChartBPP * pxlCount != imageRGBAData.size()) + if (maps::kAltitudeChartBPP * pxlCount != imageRGBADataSize) { - LOG(LWARNING, ("Wrong size of vector with altitude image bits. Expected size:", pxlCount, ". Real size:", imageRGBAData.size())); + LOG(LWARNING, ("Wrong size of vector with altitude image bits. Expected size:", pxlCount, ". Real size:", imageRGBADataSize)); return nullptr; } @@ -926,11 +924,11 @@ Java_com_mapswithme_maps_Framework_nativeGenerateRouteAltitudeChartBits(JNIEnv * for (size_t i = 0; i < imageRGBADataSize; ++i) { - size_t const shiftInBytes = i * maps::kAlitudeChartBPP; - arrayElements[i] = (imageRGBAData[shiftInBytes + 3] << 24) /* alpha */ - | (imageRGBAData[shiftInBytes] << 16) /* red */ - | (imageRGBAData[shiftInBytes + 1] << 8) /* green */ - | (imageRGBAData[shiftInBytes + 2]); /* blue */ + size_t const shiftInBytes = i * maps::kAltitudeChartBPP; + arrayElements[i] = (static_cast(imageRGBAData[shiftInBytes + 3]) << 24) /* alpha */ + | (static_cast(imageRGBAData[shiftInBytes]) << 16) /* red */ + | (static_cast(imageRGBAData[shiftInBytes + 1]) << 8) /* green */ + | (static_cast(imageRGBAData[shiftInBytes + 2])); /* blue */ } env->ReleaseIntArrayElements(imageRGBADataArray, arrayElements, 0); diff --git a/android/src/com/mapswithme/maps/Framework.java b/android/src/com/mapswithme/maps/Framework.java index cd9c5437b9..2cb6e01f9d 100644 --- a/android/src/com/mapswithme/maps/Framework.java +++ b/android/src/com/mapswithme/maps/Framework.java @@ -73,7 +73,7 @@ public class Framework * Generates Bitmap with route altitude image chart taking into account current map style. * @param width is width of the image. * @param height is height of the image. - * @return Bitmap if there's pedestrian of bicycle route and null otherwise. + * @return Bitmap if there's pedestrian or bicycle route and null otherwise. */ @Nullable public static Bitmap GenerateRouteAltitudeChart(int width, int height) @@ -165,7 +165,7 @@ public class Framework public static native RoutingInfo nativeGetRouteFollowingInfo(); @Nullable - public static native final int [] nativeGenerateRouteAltitudeChartBits(int width, int height); + public static native final int[] nativeGenerateRouteAltitudeChartBits(int width, int height); // When an end user is going to a turn he gets sound turn instructions. // If C++ part wants the client to pronounce an instruction nativeGenerateTurnNotifications returns diff --git a/map/chart_generator.cpp b/map/chart_generator.cpp index 665f774ca2..55c5468a12 100644 --- a/map/chart_generator.cpp +++ b/map/chart_generator.cpp @@ -174,12 +174,12 @@ bool GenerateYAxisChartData(uint32_t height, double minMetersPerPxl, return true; } -void GenerateChartByPoints(uint32_t width, uint32_t height, vector const & geometry, +bool GenerateChartByPoints(uint32_t width, uint32_t height, vector const & geometry, MapStyle mapStyle, vector & frameBuffer) { frameBuffer.clear(); if (width == 0 || height == 0) - return; + return false; agg::rgba8 const kBackgroundColor = agg::rgba8(255, 255, 255, 0); agg::rgba8 const kLineColor = GetLineColor(mapStyle); @@ -198,9 +198,9 @@ void GenerateChartByPoints(uint32_t width, uint32_t height, vector c TPixelFormat pixelFormat(renderBuffer, agg::comp_op_src_over); TBaseRenderer baseRenderer(pixelFormat); - frameBuffer.assign(width * kAlitudeChartBPP * height, 0); + frameBuffer.assign(width * kAltitudeChartBPP * height, 0); renderBuffer.attach(&frameBuffer[0], static_cast(width), - static_cast(height), static_cast(width * kAlitudeChartBPP)); + static_cast(height), static_cast(width * kAltitudeChartBPP)); // Background. baseRenderer.reset_clipping(true); @@ -213,7 +213,7 @@ void GenerateChartByPoints(uint32_t width, uint32_t height, vector c rasterizer.clip_box(0, 0, width, height); if (geometry.empty()) - return; /* No chart line to draw. */ + return true; /* No chart line to draw. */ // Polygon under chart line. agg::path_storage underChartGeometryPath; @@ -237,6 +237,7 @@ void GenerateChartByPoints(uint32_t width, uint32_t height, vector c rasterizer.add_path(stroke); agg::render_scanlines_aa_solid(rasterizer, scanline, baseRenderer, kLineColor); + return true; } bool GenerateChart(uint32_t width, uint32_t height, vector const & distanceDataM, @@ -269,7 +270,6 @@ bool GenerateChart(uint32_t width, uint32_t height, vector const & dista geometry[i] = m2::PointD(i * oneSegLenPix, yAxisDataPxl[i]); } - GenerateChartByPoints(width, height, geometry, mapStyle, frameBuffer); - return true; + return GenerateChartByPoints(width, height, geometry, mapStyle, frameBuffer); } } // namespace maps diff --git a/map/chart_generator.hpp b/map/chart_generator.hpp index 31bfa9f2eb..34e56dc92f 100644 --- a/map/chart_generator.hpp +++ b/map/chart_generator.hpp @@ -10,7 +10,7 @@ namespace maps { -uint32_t constexpr kAlitudeChartBPP = 4; +uint32_t constexpr kAltitudeChartBPP = 4; /// \brief fills uniformAltitudeDataM with altitude data which evenly distributed by /// |resultPointCount| points. |distanceDataM| and |altitudeDataM| form a curve of route altitude. @@ -36,7 +36,7 @@ bool GenerateYAxisChartData(uint32_t height, double minMetersPerPxl, /// \param mapStyle is a current map style. /// \param frameBuffer is a vector for a result image. It's resized in this method. /// It's filled with RGBA(8888) image date. -void GenerateChartByPoints(uint32_t width, uint32_t height, vector const & geometry, +bool GenerateChartByPoints(uint32_t width, uint32_t height, vector const & geometry, MapStyle mapStyle, vector & frameBuffer); bool GenerateChart(uint32_t width, uint32_t height, vector const & distanceDataM, diff --git a/map/framework.hpp b/map/framework.hpp index 5e181f1b94..afdc5b4f45 100644 --- a/map/framework.hpp +++ b/map/framework.hpp @@ -729,7 +729,9 @@ public: /// false otherwise. bool HasRouteAltitude() const; /// \brief Generates 4 bytes per point image (RGBA) and put the data to |imageRGBAData|. - /// \returns If there is valid route info and returns true and false otherwise. + /// \returns If there is valid route info and the chart was generated returns true + /// and false otherwise. If the method returns true it is guaranteed that the size of + /// |imageRGBAData| is more than zero. /// \note If HasRouteAltitude() method returns true, GenerateRouteAltitudeChart(...) /// could return false if route was deleted or rebuilt between the calls. bool GenerateRouteAltitudeChart(uint32_t width, uint32_t height, diff --git a/map/map_tests/chart_generator_tests.cpp b/map/map_tests/chart_generator_tests.cpp index 32ef9d14d7..510e9aa517 100644 --- a/map/map_tests/chart_generator_tests.cpp +++ b/map/map_tests/chart_generator_tests.cpp @@ -28,7 +28,7 @@ bool AlmostEqualAbs(vector const & v1, vector const & v2) bool IsColor(vector const & frameBuffer, size_t startColorIdx, uint8_t expectedR, uint8_t expectedG, uint8_t expectedB, uint8_t expectedA) { - CHECK_LESS_OR_EQUAL(startColorIdx + kAlitudeChartBPP, frameBuffer.size(), ()); + CHECK_LESS_OR_EQUAL(startColorIdx + kAltitudeChartBPP, frameBuffer.size(), ()); return frameBuffer[startColorIdx] == expectedR && frameBuffer[startColorIdx + 1] == expectedG && frameBuffer[startColorIdx + 2] == expectedB && frameBuffer[startColorIdx + 3] == expectedA; @@ -37,13 +37,13 @@ bool IsColor(vector const & frameBuffer, size_t startColorIdx, uint8_t void TestAngleColors(size_t width, size_t height, vector const & frameBuffer, uint8_t expectedR, uint8_t expectedG, uint8_t expectedB, uint8_t expectedA) { - TEST_EQUAL(frameBuffer.size(), width * height * kAlitudeChartBPP, ()); + TEST_EQUAL(frameBuffer.size(), width * height * kAltitudeChartBPP, ()); TEST(IsColor(frameBuffer, 0 /* startColorIdx */, expectedR, expectedG, expectedB, expectedA), ()); - TEST(IsColor(frameBuffer, kAlitudeChartBPP * (width - 1) /* startColorIdx */, expectedR, + TEST(IsColor(frameBuffer, kAltitudeChartBPP * (width - 1) /* startColorIdx */, expectedR, expectedG, expectedB, expectedA), ()); - TEST(IsColor(frameBuffer, kAlitudeChartBPP * height * (width - 1) /* startColorIdx */, + TEST(IsColor(frameBuffer, kAltitudeChartBPP * height * (width - 1) /* startColorIdx */, expectedR, expectedG, expectedB, expectedA), ()); - TEST(IsColor(frameBuffer, kAlitudeChartBPP * height * width - kAlitudeChartBPP /* startColorIdx */, + TEST(IsColor(frameBuffer, kAltitudeChartBPP * height * width - kAltitudeChartBPP /* startColorIdx */, expectedR, expectedG, expectedB, expectedA), ()); } @@ -134,7 +134,7 @@ UNIT_TEST(GenerateChartByPoints_NoGeometryTest) size_t constexpr height = 40; vector frameBuffer; - maps::GenerateChartByPoints(width, height, geometry, MapStyleLight /* mapStyle */, frameBuffer); + TEST(maps::GenerateChartByPoints(width, height, geometry, MapStyleLight /* mapStyle */, frameBuffer), ()); TestAngleColors(width, height, frameBuffer, 255 /* expectedR */, 255 /* expectedG */, 255 /* expectedB */, 0 /* expectedA */); } @@ -146,7 +146,7 @@ UNIT_TEST(GenerateChartByPoints_OnePointTest) size_t constexpr height = 40; vector frameBuffer; - maps::GenerateChartByPoints(width, height, geometry, MapStyleLight /* mapStyle */, frameBuffer); + TEST(maps::GenerateChartByPoints(width, height, geometry, MapStyleLight /* mapStyle */, frameBuffer), ()); TestAngleColors(width, height, frameBuffer, 255 /* expectedR */, 255 /* expectedG */, 255 /* expectedB */, 0 /* expectedA */); } @@ -159,13 +159,13 @@ UNIT_TEST(GenerateChartByPoints_Test) size_t constexpr height = 40; vector frameBuffer; - maps::GenerateChartByPoints(width, height, geometry, MapStyleLight /* mapStyle */, frameBuffer); + TEST(maps::GenerateChartByPoints(width, height, geometry, MapStyleLight /* mapStyle */, frameBuffer), ()); - TEST_EQUAL(frameBuffer.size(), width * height * kAlitudeChartBPP, ()); + TEST_EQUAL(frameBuffer.size(), width * height * kAltitudeChartBPP, ()); TEST(IsColor(frameBuffer, 0 /* startColorIdx */, 30 /* expectedR */, 150 /* expectedG */, 240 /* expectedB */, 255 /* expectedA */), ()); - TEST(IsColor(frameBuffer, kAlitudeChartBPP * (width - 1) /* startColorIdx */, 255 /* expectedR */, + TEST(IsColor(frameBuffer, kAltitudeChartBPP * (width - 1) /* startColorIdx */, 255 /* expectedR */, 255 /* expectedG */, 255 /* expectedB */, 0 /* expectedA */), ()); } @@ -195,10 +195,10 @@ UNIT_TEST(GenerateChart_OnePointTest) TEST(maps::GenerateChart(width, height, distanceDataM, altitudeDataM, MapStyleDark /* mapStyle */, frameBuffer), ()); - TEST_EQUAL(frameBuffer.size(), width * height * kAlitudeChartBPP, ()); + TEST_EQUAL(frameBuffer.size(), width * height * kAltitudeChartBPP, ()); TEST(IsColor(frameBuffer, 0 /* startColorIdx */, 255 /* expectedR */, 255 /* expectedG */, 255 /* expectedB */, 0 /* expectedA */), ()); - TEST(IsColor(frameBuffer, kAlitudeChartBPP * (width - 1) /* startColorIdx */, 255 /* expectedR */, + TEST(IsColor(frameBuffer, kAltitudeChartBPP * (width - 1) /* startColorIdx */, 255 /* expectedR */, 255 /* expectedG */, 255 /* expectedB */, 0 /* expectedA */), ()); } @@ -209,8 +209,8 @@ UNIT_TEST(GenerateChart_EmptyRectTest) feature::TAltitudes const & altitudeDataM = {}; vector frameBuffer; - TEST(maps::GenerateChart(width, 50 /* height */, distanceDataM, altitudeDataM, MapStyleDark /* mapStyle */, - frameBuffer), + TEST(!maps::GenerateChart(width, 50 /* height */, distanceDataM, altitudeDataM, MapStyleDark /* mapStyle */, + frameBuffer), ()); TEST(frameBuffer.empty(), ()); } @@ -228,8 +228,8 @@ UNIT_TEST(GenerateChart_Test) TEST(IsColor(frameBuffer, 0 /* startColorIdx */, 255 /* expectedR */, 255 /* expectedG */, 255 /* expectedB */, 0 /* expectedA */), ()); - TEST(IsColor(frameBuffer, kAlitudeChartBPP * 3 * width - - kAlitudeChartBPP /* startColorIdx */, 255 /* expectedR */, + TEST(IsColor(frameBuffer, kAltitudeChartBPP * 3 * width - + kAltitudeChartBPP /* startColorIdx */, 255 /* expectedR */, 230 /* expectedG */, 140 /* expectedB */, 255 /* expectedA */), ()); }